Mysql
 sql >> Database >  >> RDS >> Mysql

Come connettere MySQL con Java?

Dovresti prendere NPE. Mentre stai eseguendo la tua query su dbCon e non su 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);

MODIFICA

Ecco come dovrebbe apparire il tuo codice.

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