我只是最近才开始用Java进行编码,我一直试图将phpadmin数据库连接到netbeans中的Java登录表单。我已经尝试过自己,并按照解决方案和教程进行尝试并修复,但似乎无法弄清楚问题出在哪里。我已经创建了两种形式,一种具有GUI,另一种没有GUI,并且都遇到相同的错误。我已经读到Null指针是未给变量赋值并要求执行某些操作时发生的,另一个错误是因为库中没有驱动程序,但是,我已经在库中和netbeans中安装了驱动程序8.2在预编程的库中还附带了一个jbdc驱动程序。
这是我第一次登录表单的连接代码。
public class MySqlConnect {
Connection conn=null;
public static Connection ConnectDB(){
try{
Class.forName(".com.mysql.jbdc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/3306/test","root", "");
JOptionPane.showMessageDialog(null, "connected to database");
return conn;
} catch(HeadlessException | ClassNotFoundException | SQLException e){
JOptionPane.showMessageDialog(null, e);
return null;
}
} }
第二次尝试是
public class DBConnect {
private Connection con;
private Statement st;
private ResultSet rs;
public DBConnect(){
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jbdc:mysql://localhost:330/test","root","Smackdown1");
st = con.createStatement();
}catch(ClassNotFoundException | SQLException ex){
System.out.println(ex);
}
}
public void getData(){
try{
String query =" select * from persons";
st = con.createStatement();
rs = st.executeQuery(query);
System.out.println("Records from persons");
while(rs.next()){
String name= rs.getString("name");
String password= rs.getString("password");
System.out.println("Name:" +name+" "+"Password" +password);
}
}catch(Exception ex){
System.out.println(ex);
}
}
}