如何在Netbeans中连接到我的数据库

时间:2016-03-10 08:48:02

标签: java mysql swing netbeans

我无法连接到我的数据库。我已经激活了我的Xampp和MySQL。这是我的DriverManager.getConnection:

package Genos;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;

public class MySQLConnection
{
    public static Connection con;

    public static void connect()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:derby://localhost:1527/Project","root","root");
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Database not found");
        }
    }
}

这是我的登录表格:

String sql = "SELECT * FROM USER "
                + "WHERE USERNAME = ? AND PASSWORD = ?";

        try 
        {
            MySQLConnection.connect();
            PreparedStatement pst = MySQLConnection.con.prepareStatement(sql);
            pst.setString(1,jTextField1_user.getText());
            pst.setString(2,jPasswordField1_user.getText());
            ResultSet rs = pst.executeQuery();

            if(rs.next())
            {
                JOptionPane.showMessageDialog(null,"Successful");
            }
            else
            {
                JOptionPane.showMessageDialog(null,"Incorrect username or password");
            }
        }
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(null, ex + "SQL failed");
            //System.out.println(ex.);
        }

    }                    

我不知道该怎么做。我的数据库有什么问题吗?我正在使用Netbeans btw。

提供的数据库

1 个答案:

答案 0 :(得分:0)

检查是否在构建路径设置中包含了mysqlconnector.jar。