2

As part of my project I am trying to connect it with database. I searched in google for the code and I got the following code. In that I don't understand 2 things - "import com.mysql.jdbc.Driver;" and "new Driver". What do these 2 mean ?

package javasql;

import com.mysql.jdbc.Driver;
import java.sql.*;

public class Connect {
public Connect() throws SQLException{
    makeConnection();
} 

private Connection koneksi;  

 public  Connection makeConnection() throws SQLException {
    if (koneksi == null) {
         new Driver();
        // buat koneksi
         koneksi = DriverManager.getConnection(
                   "jdbc:mysql://localhost:3306/mysql","root","virus");
     }
     return koneksi;
 }  

 public static void main(String args[]) {
     try {
         Connect c = new Connect();
         System.out.println("Connection established");
     }
     catch (SQLException e) {
         e.printStackTrace();
         System.err.println("Connection Failure");
     }  

}
}

package javasql;

import java.sql.*;

public class SqlStatement {
private Statement statement;
public SqlStatement() throws SQLException{
    makeStatement();
}
public Statement makeStatement() throws SQLException{
    Connect c = new Connect();
    Connection conn = c.makeConnection();
    statement = conn.createStatement();
    return statement;
}
public void insert(String name,int npm)throws SQLException{
    statement.execute("insert into Student values(\""+name+"\","+npm+");");
}
public static void main(String arg[]){
    try {
        SqlStatement s = new SqlStatement();
        s.insert("Ferdi2",3);
        s.insert("Anca2",3);
        System.out.println("Success");
    }
    catch(SQLException e){
        System.out.println("Failed");
        e.printStackTrace();
    }
}
}

I use NetBeans IDE to develop my project. When I used these codes I made it as a new project. Then it worked fine. But whenever I tried to include these codes in another projects errors are showing at "import com.mysql.jdbc.Driver;". Why is it so ? Can I use these 2 codes in another projects ?

eltabo
  • 3,749
  • 1
  • 21
  • 33
TomJ
  • 1,803
  • 14
  • 37
  • 57
  • 1
    You have to include a driver library to be able to interact with an database management system. `import com.mysql.jdbc.Driver` is the class of a mysql driver that yoyu must have in your class path. http://dev.mysql.com/downloads/connector/j/ – dooxe Feb 05 '14 at 14:51
  • You need to include the MYSQL java library with your project. The import statement tells the compiler you are using a class called `Driver` from package `com.mysql.jdbc`. – initramfs Feb 05 '14 at 14:51
  • Thanks... When I added the MySQL JDBC Driver in NetBeans the problem is solved. – TomJ Feb 05 '14 at 14:59
  • Note that the new Driver() line is just wrong; I would remove it. – Gimby Feb 05 '14 at 15:01

3 Answers3

3

The driver serves as an interface between your application and the database.

Are you using MySQL? If so, you can find the MySQl Java drivers here.

dev_feed
  • 689
  • 2
  • 7
  • 25
0

Using import com.mysql.jdbc.Driver; in JDBC code is not a good practice and you need to import only java.sql.* and javax.sql.*. The reason is to detach the code from the specific driver implementation.

See here for more information how to make JDBC connections. And DriverManager.getConnection(...) is enough to get connection.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
0

All you need is

// This will load the MySQL driver, each DB has its own driver Class.forName("com.mysql.jdbc.Driver")

This acts like class loader and load your driver class for you. For that you need to add the corresponding jar file.

Kick
  • 4,823
  • 3
  • 22
  • 29