无法从MySql数据库将数据检索到Jtextfield

时间:2016-08-02 21:13:21

标签: java mysql mysql-workbench netbeans-8

 try
                    {
                        st=cn.createStatement();
                        String strUser="";
                        rs=st.executeQuery("SELECT * FROM chsl_db WHERE name='"+nametxt.getText()+"'");
                        while(rs.next())
                        {
                            strUser=rs.getString(1);
                        }
                        if(strUser.equals(nametxt.getText()))
                        {
                        st= cn.createStatement();
                        rs = st.executeQuery("SELECT * FROM chsl_db WHERE name ='" + nametxt.getText() + "'");
                        while (rs.next()) 
                        {
                            titletxt.setText(rs.getString(1));
                            nametxt.setText(rs.getString(2));
                            flatnum.setText(rs.getString(3));
                            areatxt.setText(rs.getString(4));
                            emailtxt.setText(rs.getString(5));
                            deletebutton.setEnabled(true);
                        }

                        st.close();
                        }
                            else
                            {
                                JOptionPane.showMessageDialog(null,"No Data Found!","Security Warning",JOptionPane.WARNING_MESSAGE);
                                    }

                                    }

    catch(SQLException s)
    {
        System.out.println("No record found!\n\n\n");
        System.out.println("SQL Error" + s.toString() + " " + s.getErrorCode() + " " + s.getSQLState());
    }
catch(Exception x)
    {
        System.out.println("Error" + x.toString()+" " + x.getMessage());
    }   

}                        

使用MySql Workbench作为数据库的Netbeans项目。将数据插入数据库正在工作,但无法检索相同的数据。它抛出错误“找不到数据!”。检查连接和它的确定。以下是我用来建立连接的代码。请帮忙 !!

try
    {
        Class.forName("java.sql.Driver");
        cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/avinash?useSSL=false","root","password");
    }
            catch(ClassNotFoundException e)  
            {
        System.err.println("Failed to load driver");
        e.printStackTrace();
    }
    catch(SQLException e)
            {
        System.err.println("Unable to connect");
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

在下面的行后面的连接中添加st=cn.createStatement();

cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/avinash?useSSL=false","root","password");

我不知道你需要做什么。

但我认为一开始就是

try {
   nm = nametxt.getText();
if(nm != '') {
   rs = st.executeQuery("SELECT * FROM chsl_db WHERE name ='" + nm + "'");
while (rs.next()) {
   titletxt.setText(rs.getString(1));
   nametxt.setText(rs.getString(2));
   flatnum.setText(rs.getString(3));
   areatxt.setText(rs.getString(4));
   emailtxt.setText(rs.getString(5));
   deletebutton.setEnabled(true);
}
 st.close();
 }
 else {
JOptionPane.showMessageDialog(null,"No Data Found!","Security Warning",JOptionPane.WARNING_MESSAGE);
 }
}

我有一段时间没有使用Netbeans。所以告诉我,如果我错了。