chewbapoclypse chewbapoclypse - 1 year ago 65
SQL Question

How to add the Connection Driver to my Project

I am using NetBeans to test a JSP project. I'm simply trying to establish a connection to the MySQL database that is running on my local machine.


<%@ page import="java.sql.*"%>
<title>Database connection</title>
String dbUrl = "jdbc:mysql://localhost:3306/database";
String user = "root";
String pass = "root";

Connection myConn = DriverManager.getConnection(dbUrl,user,pass);

NetBeans was able to connect to the server and all of its databases. As seen in the picture below. Is there a way to use the connector driver that Netbeans is using to connect to the database in my project?

enter image description here

enter image description here

enter image description here

Answer Source

You have to specify the driver name with appropriate package name. Because the mysql-connector.jar file contains lots Driver class implementations. So,

Try this code

 try {
         Connection con = DriverManager.getConnection("jdbc:mysql://localhost/database", "root", "root");
         Statement stmt = con.createStatement();

        // perform required operation

 } catch (Exception e) {