ploofah ploofah - 5 months ago 43
Java Question

mysql database connection in java netbeans?

im trying to develop a simple database web application in netbeans, but im 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!!

Answer

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

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

before Your conn= statement.

Comments