Java 什么是import com.mysql.jdbc.Driver;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21580499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is import com.mysql.jdbc.Driver;
提问by TomJ
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 ?
作为我项目的一部分,我试图将它与数据库连接。我在谷歌搜索代码,我得到了以下代码。我不明白两件事 - “import com.mysql.jdbc.Driver;” 和“新司机”。这2个是什么意思?
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 ?
我使用 NetBeans IDE 来开发我的项目。当我使用这些代码时,我将其作为一个新项目。然后它工作得很好。但是每当我尝试将这些代码包含在另一个项目中时,错误都会显示在“import com.mysql.jdbc.Driver;”中。为什么会这样?我可以在其他项目中使用这两个代码吗?
采纳答案by dev_feed
回答by Ashot Karakhanyan
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.
import com.mysql.jdbc.Driver;
在JDBC
代码中使用不是一个好习惯,您只需要导入java.sql.*
和javax.sql.*
。原因是将代码与特定的驱动程序实现分离。
See herefor more information how to make JDBC connections. And DriverManager.getConnection(...)
is enough to get connection.
有关如何建立 JDBC 连接的更多信息,请参见此处。并且DriverManager.getConnection(...)
足以获得连接。
回答by Kick
All you need is
所有你需要的是
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver")
// 这将加载 MySQL 驱动程序,每个 DB 都有自己的驱动程序
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.
这就像类加载器并为您加载驱动程序类。为此,您需要添加相应的 jar 文件。