Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:10:15+00:00 2026-06-10T03:10:15+00:00

I don’t know much about SQL, but I’m fine with Java, I just wanted

  • 0

I don’t know much about SQL, but I’m fine with Java, I just wanted to know how I can retrieve a variable from my SQL database: ‘EasyDirectory’. Like:

String test = con.getQuery(query1).get(username);

Obviously that doesn’t work but I would like a snippet of code that does that. Heres all of my code:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

public class Directory {
  public static void main(String args[]) throws IOException, SQLException {
      Connection con = null;
      try {
          // Load the JDBC driver
          String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
          Class.forName(driverName);

          // Create a connection to the database
          String serverName = "www.freesql.org";
          String mydatabase = "EasyDirectory";
          String url = "jdbc:mysql://" + serverName +  "/" + mydatabase; // a JDBC url
          String username = "*********";
          String password = "*********";
          con = DriverManager.getConnection(url, username, password);
      } catch (ClassNotFoundException e) {
          // Could not find the database driver
      } catch (SQLException e) {
          // Could not connect to the database
      }


    final JFrame frame = new JFrame("Directory");
    frame.setPreferredSize(new Dimension(300, 300));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JProgressBar searchprogress = new JProgressBar();
    final JPanel panel = new JPanel();
    final JButton searchbutton = new JButton("Search");
    final JTextField searchfield = new JTextField();
    searchfield.setPreferredSize(new Dimension(200, 30));
    searchprogress.setPreferredSize(new Dimension(280, 30));
    searchbutton.setLocation(100, 100);

    /* Start Buffered Reader */
    final List<String> housetypes = new ArrayList<String>();
    String line = "";
    BufferedReader br = new BufferedReader(new FileReader("Index.txt"));
    while (line != null) {
        line = br.readLine();
        housetypes.add(line);
        String seperation = br.readLine();

    }

    /* Finish Buffered Reader */

    /* Start Content Code */
    final JButton done = new JButton("Done");
    done.setVisible(false);
    JLabel housetype_label = new JLabel();
    JLabel housenumber_label = new JLabel();
    JLabel housestreet_label = new JLabel();
    JLabel housepostal_label = new JLabel();
    JLabel houseplace_label = new JLabel();
    /* Finish Content Code */

    /* Start Button Code */
    done.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            searchfield.setEnabled(true);
            done.setVisible(false);
            searchbutton.setVisible(true);
            searchprogress.setValue(0);
        }
    });
    searchbutton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {

            searchprogress.setValue(100);
            String searchquery = searchfield.getText();
            searchprogress.setValue(100);
            searchfield.setEnabled(false);
            done.setVisible(true);
           searchbutton.setVisible(false);
            for (String housetype : housetypes) {
                if (searchquery.equals(housetype)) {
                    String housepath = housetype + "/" + housetype + ".txt";
                    System.out.println(housepath);
                    try {
                        BufferedReader housebr = new BufferedReader(new FileReader(housepath));
                        String housename_query = housebr.readLine();
                        String housenumber_query = housebr.readLine();
                        String housestreet_query = housebr.readLine();
                        String houselocality_query = housebr.readLine();
                        String housepostal_query = housebr.readLine();
                        System.out.println(housepostal_query);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }

        }
    });

    /* Finish Button Code */
    /* Test Field */


    /* End Test Field */

    panel.add(searchfield);
    panel.add(done);
    panel.add(searchbutton);
    panel.add(searchprogress);

    frame.setResizable(false);
    frame.add(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(false);

    /* Start Login Window */
    int passtimes = 3;
    final JFrame login = new JFrame("Login");
    JPanel login_panel = new JPanel();
    JLabel userlabel = new JLabel("Username: ");
    JLabel passlabel = new JLabel(" Password: ");
    JButton loginuser = new JButton("Login");
    JButton cancel = new JButton("Cancel");
    final JTextField user_field = new JTextField();
    user_field.setPreferredSize(new Dimension(100,30));
    final JPasswordField pass_field = new JPasswordField();
    pass_field.setPreferredSize(new Dimension(100,30));
    ImageIcon icon = new ImageIcon("Images/Logo.png");
    ImageIcon space = new ImageIcon("Images/Spacing.png");
    JLabel logo = new JLabel();
    JLabel spacing = new JLabel();
    logo.setIcon(icon);
    login.setPreferredSize(new Dimension(200,212));
    login_panel.add(logo);
    login_panel.add(userlabel);
    login_panel.add(user_field);
    login_panel.add(passlabel);
    login_panel.add(pass_field);
    login_panel.add(spacing);
    login_panel.add(loginuser);
    login_panel.add(cancel);
    login.add(login_panel);
    login.pack();
    login.setVisible(true);
    login.setLocationRelativeTo(null);


    loginuser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String user_input = user_field.getText();
            String pass_input = pass_field.getText();
            String username = "Tom";
            String password = "******";
                if(user_input.equals(username)){
                    if(pass_input.equals(password)){
                        user_field.setEnabled(false);
                        pass_field.setEnabled(false);
                        frame.setVisible(true);
                        login.setVisible(false);
                    }
                    else{//If Password AND Username is incorrect
                        JOptionPane.showMessageDialog(panel, "Password and/or Username Is Incorrect.", "Failed Login", JOptionPane.ERROR_MESSAGE);
                    }
                }
                else{ //If Username is incorrect
                    JOptionPane.showMessageDialog(panel, "Password and/or Username Is Incorrect.", "Failed Login", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    cancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            System.exit(0);
            }
        });
  }
}

Thanks, Helping is greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:10:17+00:00Added an answer on June 10, 2026 at 3:10 am

    By reading the API of Connection (here: http://docs.oracle.com/javase/6/docs/api/java/sql/Connection.html) I would assume that it would have to be something like:

     final Statement statement = con.createStatement();
     final ResultSet result = statement.executeQuery(query1);
     //do stuff with the resultset
     //result.getString(something), see http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html
    

    On a side-note. I don’t really like the fact that you have put both the GUI and the database logic in the same class. You really should separate concerns, by applying the MVC pattern or something similar. For instance, you could create one class for the GUI, one class for to the connection the database, and one class for starting the application and tying the two others together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know if the title describes anything about what I'm trying to say but
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't know where else to ask, but from one day to the other my
Don't know much about encryption... Say I'm preparing a SAML request to submit to
Don't know whether I'm having a "thick day" - but I just wondered what
Don't know why but I can't find a solution to this. I have 3
Don't know much about running a function on every item in an array, still
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.