2012-08-24 75 views
-3

我对SQL不太了解,但是我对Java很好,我只是想知道如何从我的SQL数据库中检索变量:'EasyDirectory'。像:将Java连接到SQL

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

谢谢你,帮助是非常感谢!

+1

问题是什么?无论哪种方式,这是最好的起点:http://docs.oracle.com/javase/tutorial/jdbc/basics/ – biziclop

+3

而不是发布代码墙,你应该正确描述情况。你使用的是哪个数据库?你试图使用哪些库?什么不工作?你的数据库模式是什么?你想从数据库中获取什么数据? – toniedzwiedz

回答

0

通过阅读连接(在这里:http://docs.oracle.com/javase/6/docs/api/java/sql/Connection.html)的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创建一个类,为数据库连接一个类,以及启动应用程序并将其他两个类绑定在一起的一个类。

+0

I_Am_A_Noob。我只想学习 – user1543053

+0

不用担心,google MVC,如果你想从一本书中学到很多,你应该阅读Robert Martin的Clean Code。另请参阅http://en.wikipedia.org/wiki/Separation_of_concerns – Tobb