How to connect MySQL with Java?

前端 未结 1 613
臣服心动
臣服心动 2021-01-26 05:35

I have installed MYSQLSERVER 5.1.Then I installed mysql-connector-java-3.0.8-stable-bin.jar and put in drive c with folder core that is C:\\core.Then in propert

相关标签:
1条回答
  • 2021-01-26 06:06

    You should be getting NPE. As you are executing your query on dbCon and not on dbcon

    // initialize here
    Connection dbcon = DriverManager.getConnection(  
                    "jdbc:mysql://localhost:3306/EMPLOYEE", "root", "root");  
    
    String query ="select count(*) from EMPLOYEE4 ";
    
    // Null here
    Connection dbCon = null;
    
    // on dbCon which is null 
    stmt = dbCon.prepareStatement(query);
    

    EDIT

    This how your code suppose to look like.

    Connection dbcon = DriverManager.getConnection(  
                        "jdbc:mysql://localhost:3306/EMPLOYEE", "root", "root"); 
    String query = "select count(*) from EMPLOYEE4 ";
    Statement stmt = dbcon.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    
    0 讨论(0)
提交回复
热议问题