java 如何为 Oracle SQL Developer 加载 JDBC 驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34410083/
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
How to load JDBC driver for Oracle SQL Developer
提问by Mariel
My professor asks us to make a javaprogram that asks the user to input the data and the data inputted should automatically be inserted into a table in sql developer.
我的教授让我们做一个java程序,要求用户输入数据,输入的数据应该自动插入到sql developer的表中。
I have a background in Java and database but i'm not familiar with creating JDBC applications.
我有 Java 和数据库方面的背景,但我不熟悉创建 JDBC 应用程序。
I searched a tutorial online and it said that i need to register the JDBC driver first. I ran the code in my compiler but it outputs Error: unable to load driver class!
.
我在网上搜索了一个教程,它说我需要先注册JDBC驱动程序。我在编译器中运行了代码,但它输出Error: unable to load driver class!
.
What should i do for it to register?
我应该怎么做才能注册?
I still don't know the concept of Class.forName();
我还是不知道这个概念 Class.forName();
Secondly, it errors a SQLException: No suitable drivers found
其次,它错误地 SQLException: No suitable drivers found
I don't know what's wrong in my code but the connection details in my SQL developer is this:
我不知道我的代码有什么问题,但我的 SQL 开发人员的连接细节是这样的:
Connection name: mariel
Connection details: mariel@//localhost:1521/XEXDB
连接名称:mariel
连接详细信息:mariel@//localhost:1521/XEXDB
The code:
代码:
import java.sql.* ;
public class employeeMode{
public static void main(String args[]) throws SQLException{
String URL = "jdbc:oracle:thin:@localhost:1521:XEXDB";
String USER = "mariel";
String PASS = "1234";
Connection conn = DriverManager.getConnection(URL, USER, PASS);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
}
}
EDIT:
I fixed it! Thank you everyone! here's the working code:
编辑:
我修好了!谢谢大家!这是工作代码:
import java.sql.* ;
public class employeeMode{
public static void main(String args[]) throws SQLException{
String URL = "jdbc:oracle:thin:mariel@//localhost:1521/XEXDB";
String USER = "mariel";
String PASS = "1234";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(URL, USER, PASS);
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
}
}
回答by Deedar Ali Brohi
@Mariel, have you installed Oracle 10g or any version Database, i think Class.forName("oracle.jdbc.driver.OracleDriver"); is unable to find any database connection which you have installed your system
@Mariel,您是否安装了 Oracle 10g 或任何版本的数据库,我认为 Class.forName("oracle.jdbc.driver.OracleDriver"); 无法找到您已安装系统的任何数据库连接
download ojdbcxxx.jar file from below drive http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
从下面的驱动器下载 ojdbcxxx.jar 文件 http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
回答by saikumarm
回答by Sachin Pardeshi
import java.sql.* ;
public class employeeMode{
public static void main(String args[]) throws SQLException{
String URL = "jdbc:oracle:thin:@localhost:1521:XEXDB";
String USER = "mariel";
String PASS = "1234";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(URL, USER, PASS);
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
}
}
回答by Lokesh Rathor
You must have to add oracle driver jar file which is available on net http://www.oracle.com/technetwork/apps-tech/jdbc-112010-090769.html. then apply it in your class path.
您必须添加可在网络http://www.oracle.com/technetwork/apps-tech/jdbc-112010-090769.html上获得的 oracle 驱动程序 jar 文件。然后将其应用到您的类路径中。
回答by Tareq Moh Qadommi
First you try to make the connection using drivermanager before load your oracle driver first you should add oracle ojdbc6-11.2.0.4.jar file to load the oracle driver
首先你尝试在加载你的oracle驱动之前使用drivermanager建立连接你应该添加oracle ojdbc6-11.2.0.4.jar文件来加载oracle驱动
you can separate your two methods one to open and the second to close your database connection as below you
您可以将两种方法分开,一种打开,第二种关闭数据库连接,如下所示
public class BDConnection
{
private static final String DB_DRIVER = "jdbc:oracle:thin:@localhost:1521:XEXDB";
private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:XEXDB";
private static final String DB_USER = "mariel";
private static final String DB_PASSWORD = "1234";
}
public static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
return DriverManager.getConnection(
DB_CONNECTION, DB_USER, DB_PASSWORD);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
public static void closeMyConnection(Connection connection) {
try {
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
And now to test your code just you shulod call getDBConnection() in your main method after adding your jar file
现在要测试您的代码,只需在添加 jar 文件后在主方法中调用 getDBConnection()
public static void main(String[] argv) {
Connection connection =getDBConnection();
closeMyConnection(connection);
}
回答by Naveen
First you have to load the driver and then create the connection as below. Also you have to put the ojdbc jar in the classpath.
首先,您必须加载驱动程序,然后创建如下连接。此外,您必须将 ojdbc jar 放在类路径中。
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(URL, USER, PASS);
回答by Lumi.seven
Isn't it
是不是
Class.forName("oracle.jdbc.OracleDriver");
? And:
? 和:
...
try {
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection(URL, USER, PASS);
}
...