Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Java连接到SQL_Java_Sql_Variables - Fatal编程技术网

将Java连接到SQL

将Java连接到SQL,java,sql,variables,Java,Sql,Variables,我对SQL不太了解,但我对Java很在行,我只是想知道如何从SQL数据库中检索变量:“EasyDirectory”。比如: String test = con.getQuery(query1).get(username); 显然,这不起作用,但我想要一段这样做的代码。这是我所有的代码: import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; impor

我对SQL不太了解,但我对Java很在行,我只是想知道如何从SQL数据库中检索变量:“EasyDirectory”。比如:

String test = con.getQuery(query1).get(username);
显然,这不起作用,但我想要一段这样做的代码。这是我所有的代码:

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);
            }
        });
  }
}

谢谢,非常感谢您的帮助

通过阅读这里的连接API:我假设它必须是这样的:

 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

旁注。我不喜欢将GUI和数据库逻辑放在同一个类中。您真的应该通过应用MVC模式或类似的方式来分离关注点。例如,您可以创建一个用于GUI的类,一个用于连接数据库的类,以及一个用于启动应用程序并将其他两个类绑定在一起的类。

问题是什么?无论哪种方式,这都是最好的起点:与其张贴一堆代码,不如正确地描述情况。你在使用什么数据库?您正在尝试使用哪些库?到底是什么不起作用?您的数据库模式是什么?你想从数据库中获取什么数据?我是诺布。我只想学习无忧,谷歌MVC,如果你想从一本书中学到很多东西,你应该读罗伯特·马丁的《干净的代码》。另外,请参见