什么是import com.mysql.jdbc.Driver; [英] What is import com.mysql.jdbc.Driver;

查看:286
本文介绍了什么是import com.mysql.jdbc.Driver;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的项目的一部分,我正在尝试将其与数据库连接.我在google中搜索了代码,并得到了以下代码.因为我不明白两件事-"import com.mysql.jdbc.Driver;"和新驱动程序".这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();
    }
}
}

我使用NetBeans IDE来开发我的项目.当我使用这些代码时,我将其作为一个新项目.然后工作正常.但是,每当我尝试将这些代码包含在另一个项目中时,都会在"import com.mysql.jdbc.Driver;"处显示错误.为什么会这样呢?我可以在其他项目中使用这两个代码吗?

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 ?

推荐答案

驱动程序充当应用程序和数据库之间的接口.

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

您正在使用MySQL吗?如果是这样,您可以在此处

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

这篇关于什么是import com.mysql.jdbc.Driver;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆