2

我使用 netbeans 创建了一个 java 登录框架,并使用 MySQL Connector/J 将其连接到 MySQL,jar 文件已添加到项目库中。我还创建了一个名为 login 的表,其中包含所有登录详细信息。以下代码应该允许登录,但我不断收到错误,好像没有建立与数据库的连接一样。

package Lightapp;
import java.sql.* ;
import javax.swing.* ;

public class AbbeyLog extends javax.swing.JFrame
{
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    /**
     * Creates new form AbbeyLog
     */
    public AbbeyLog()
    {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void textuserActionPerformed(java.awt.event.ActionEvent evt)
    {
        // TODO add your handling code here:
        String sql = "select * from login where username = ? and password = ?";
        try
        {
            pst = conn.prepareStatement(sql);
            pst.setString(1, textuser.getText());
            pst.setString(2, textpass.getText());
            rs = pst.executeQuery();
            if (rs.next())
            {
                JOptionPane.showMessageDialog(null, "Username and Password correct");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "invalid username and password");
            }
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    private void textuserMouseClicked(java.awt.event.MouseEvent evt)
    {
        // TODO add your handling code here:
    }


    public static void main(String args[])
    {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new AbbeyLog().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField textpass;
    private javax.swing.JButton textuser;
    // End of variables declaration
}
4

2 回答 2

2

您需要在启动时创建一个 jdbc 连接:

//load the driver
Class.forName("com.mysql.jdbc.Driver").newInstance();
// create a connection         
conn = DriverManager.getConnection("jdbc:mysql://"+hostName+":"
                + dbPort+"/"+databaseName+"?"+"user="+dbUser+"&password=" + dbPassword);
于 2012-12-20T14:18:42.880 回答
0

一个好的方法是创建一个名为 dbConnection 的单独文件,例如。该文件将包含连接到数据库的方法。通常它看起来像这样:

public class DBConnection{

  Connection con = null;
  Statement stmt = null;
  ResultSet rs = null;

public DBConnection() {}

public void connect() {
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:jtds:sqlserver://adress:port;DatabaseName=dbName", "login", "passwd");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

然后在你的主要课程中你做:

dbConnection db=new dbConnection();
db.connect;
于 2012-12-20T14:58:52.350 回答