0

I've only recently started coding in java, I have been trying to connect my phpadmin database to my java log in form in netbeans. I have tried on my own and followed solutions and tutorials to try and fix but can't seem to figure out what the issue is. I've created two forms, one with a GUI the other without and both get the same errors. I've read that the Null Pointer is when the variable hasn't been assigned a value and asked to do something and the other error is because there's no driver in the library, however, I have installed a driver into the library and in netbeans 8.2 it also comes with a jbdc driver in the pre-programmed library.

Here is my connection code for the first Log In form either way.

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;
}

} }

and my second attempt was

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);

}
}

}

  • Add the trace of the exception in order to have better context about the problem – sirandy Nov 22 '18 at 18:05
  • although it probably isn't the issue, you used port 330 in the second attempt – 4dc0 Nov 22 '18 at 18:10
  • Also the string in the first example starts with a "." which is not correct: `".com.mysql.jbdc.Driver"`. – markspace Nov 22 '18 at 18:21
  • I've changed both the port number on the second one and removed the "." on the first line and still, the errors occur. – Matthew Robinson Nov 26 '18 at 12:44
  • The stack trace when i print says this is my error st = con.createStatement(); and also public static void main(String[] args){ DBConnect connect = new DBConnect(); connect.getData(); the last line of this code – Matthew Robinson Nov 26 '18 at 13:04

0 Answers0