简体   繁体   中英

mysql database connection in java netbeans?

I am trying to develop a simple database web application in netbeans, but I am getting a problem at database connection , my database is not being connected for some reason, the username password and hostname are correct and the mysql workbench is registered with my netbeans as well.

Is there something wrong with this code?

public class test {    
public static void main(String args[]) {        
Connection conn=null;    
try{    `conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/crud_news", "root","zunairaabira");`    
if(conn!=null){    
System.out.println("successful");   
 }    }
catch(Exception e){    System.out.println("notconnected");    
}}}

please help!!

Looks like you haven't loaded the MYSQL JDBC Driver.

Add this:

Class.forName("com.mysql.jdbc.driver").newInstance();

You need to call instance of the mysql driver too. Add

Class.forName("com.mysql.jdbc.Driver").newInstance();

before Your conn= statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM