MySQL连接到java app

时间:2013-11-13 07:09:00

标签: java mysql

我已经在名为fbaccount的phpadmin中创建了一个表。我仍然不知道这有什么问题。任何帮助将不胜感激。

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.ImageIcon.*;
    import java.sql.*;
    public class Fb extends JFrame{

       Icon icon = new ImageIcon("images/signup.png");
       Icon icon2 = new ImageIcon("images/login.jpg");
       JLabel signupicon = new JLabel(icon);
       JLabel loginicon = new JLabel(icon2); 
       JTextField user = new JTextField();
       JTextField fname =new JTextField("First name");
       JTextField lname = new JTextField("Last name");
       JTextField email = new JTextField("Email");
       JTextField remail = new JTextField("Re-enter email");
       JTextField password =new JTextField("Password");
       JPasswordField pass = new JPasswordField();
       Container c = new Container();
       Connection con;
        Statement st;
        ResultSet rs;
        int ctr=0;
       public Fb()//constuctor
       {

        this.setSize(947,486);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        setContentPane(new JLabel(new ImageIcon("images/fb.jpg")));
        c = this.getContentPane();
        c.setLayout(null);
        c.add(signupicon);
        c.add(loginicon);
        c.add(user);
        c.add(pass);
        c.add(fname);
        c.add(lname);
        c.add(email);
        c.add(remail);
        c.add(password);
        loginicon.setBounds(875,18,48,21);
        signupicon.setBounds(526,405,195,40);
        user.setBounds(547,18,151,21);
        pass.setBounds(710,18,151,21);
        fname.setBounds(524,179,195,40);
        lname.setBounds(729,179,195,40);
        email.setBounds(524,229,400,40);
        remail.setBounds(524,279,400,40);
        password.setBounds(524,329,400,40);


       }
      //for sql codes 
       public void actionPerformed(ActionEvent e)
    {
    //for login 
    Object j= e.getSource();
    if(j==loginicon){
             String u = user.getText();
             String p = pass.getText();
         try{
                String driver="com.mysql.jdbc.Driver";
                String db="jdbc:mysql://localhost:3306/fbaccount";
                String user="root";
                String pass="";
                Class.forName(driver);
                con=DriverManager.getConnection(db,user,pass);
                st=con.createStatement();

               String QueryString = "SELECT username, password from fbaccount WHERE username = '"+u+"' AND password = '"+p+"'  ";
                    rs = st.executeQuery(QueryString);

                    int count = 0;
                    while(rs.next()){
                    count = count +1;
                    }
                        if (count==1)
                        {
                 Opening ms = new Opening();
                 ms.setVisible(true);
                 dispose(); 


                        }
                        else if (count>1){                
     JOptionPane.showMessageDialog(this,"Can't Login");


                        } 

                        else if (count==0)
                          JOptionPane.showMessageDialog(this,"No record Exist");  
            }catch(Exception ex){
                JOptionPane.showMessageDialog(this, "Error in database connection");
                ex.printStackTrace();
                System.exit(0);
            }

           }
    //for the signup
    else if (j==signupicon)
    {
             String u = user.getText();
             String p = pass.getText();
         String nemail=email.getText();
         String n= fname.getText();
         String l= lname.getText();

              try{ 
    int updateQuery = 0;  
                String driver="com.mysql.jdbc.Driver";
                String db="jdbc:mysql://localhost:3306/fbaccount";
                String user="root";
                String pass="";
                Class.forName(driver);
                con=DriverManager.getConnection(db,user,pass);
                st=con.createStatement();   

    String QueryString = "SELECT username, password from fbaccount WHERE username = '"+u+"'   ";
                    rs = st.executeQuery(QueryString);

                    int count = 0;
                    while(rs.next()){
                    count = count +1;
                    }
                        if (count>0)
                        {
                        JOptionPane.showMessageDialog(this,"Username already Exist");
                        }
                        else{                
            QueryString = "INSERT INTO fbaccount(lastname, firstname,username ,new_email,password) VALUES ('"+l+"','"+n+"','"+u+"' ,'"+nemail+"','"+p+"')"; 

    updateQuery = st.executeUpdate(QueryString);  
    { 
    JOptionPane.showMessageDialog(this,"Account created successfully");  
    }  
                        }

              }
    catch (Exception ex) 
    { 
         JOptionPane.showMessageDialog(this, "Unable to connect to database");
                ex.printStackTrace();
                System.exit(0);
     }   
    }
    }
       public static void main(String[] args) 
        {

            Fb f = new Fb();
            f.setVisible(true);
        }



    }

我该怎么办?这是如何连接到mysql或我错过了什么?

2 个答案:

答案 0 :(得分:0)

您尚未在申请中说明问题,但请允许我指出代码中存在的几个明显问题。

  1. 您不仅要在每个用户操作上打开连接,而且每次都要加载JDBC驱动程序,这是不推荐的。您应该加载驱动程序并在应用程序启动时打开连接。

  2. 当您使用PreparedStatement时,您正在使用强制连接SQL语句以将数据插入数据库表。它使您的代码容易受到 SQL Injection 攻击,但是当字符串参数包含像撇号这样的保留字符时,您首先要担心非工作的SQL语句。

    String sql =“INSERT INTO fbaccount(lastname,firstname,username,new_email,password)VALUES(?,?,?,?,?)”;   PreparedStatement ps = connection.prepareStatement(sql);   ps.setString(1,l);   ps.setString(2,n);   ps.setString(3,u);   ps.setString(4,nemail);   ps.setString(5,p);   ps.execute();

  3. 您的数据库是否也称为fbaccount,与数据库表相同? JDBC连接URL应包含数据库名称,而不是数据库表的名称:

    JDBC:MySQL的://本地主机:3306 / fbaccount

答案 1 :(得分:0)

查找有关Hibernate的任何教程并应用该框架,它为您提供了许多更好的解决方案。