连接到远程的oracle数据库
我正在尝试运行以下连接到远程数据库并检索记录的代码:
import java.sql.*;
class Employee
{
public static void main (String args [])
throws SQLException, ClassNotFoundException {
// Load the Oracle JDBC driver
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
// Connect to the database
// You must put a database name after the @ sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as <host>:<port>:<sid>. The example uses the short cut syntax.
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@ourcompany.com:1521:course", "username", "password");
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select * from test");
// Iterate through the result and print the employee names
/*
while (rset.next ())
System.out.println (rset.getString ("name"));
System.out.println (rset.getString ("id"));
*/
rset.next();
System.out.println(rset.getString("name"));
} 从 Netbeans 运行此代码后,
我收到错误:
线程“main”中出现异常java.sql.SQLException:找不到jdbc的合适驱动程序:oracle:thin:@ourcompany.com:1521:course 在 java.sql.DriverManager.getConnection(DriverManager.java:604) 在 java.sql.DriverManager.getConnection(DriverManager.java:221) 在 Employee.main(Empyoee.java:23) Java 结果:1 构建成功(总时间:2秒)
我已经下载了ojdbc14.jar并保存在C:\Program Files\Java\jdk1.7.0\jre\lib路径中。 我不知道我哪里出了问题?...请帮助我。
I am trying to run following code which is connecting to remote database and retrieving records:
import java.sql.*;
class Employee
{
public static void main (String args [])
throws SQLException, ClassNotFoundException {
// Load the Oracle JDBC driver
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
// Connect to the database
// You must put a database name after the @ sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as <host>:<port>:<sid>. The example uses the short cut syntax.
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@ourcompany.com:1521:course", "username", "password");
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select * from test");
// Iterate through the result and print the employee names
/*
while (rset.next ())
System.out.println (rset.getString ("name"));
System.out.println (rset.getString ("id"));
*/
rset.next();
System.out.println(rset.getString("name"));
}
}
after running this code from Netbeans i am getting an error:
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@ourcompany.com:1521:course
at java.sql.DriverManager.getConnection(DriverManager.java:604)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at Employee.main(Emplyoee.java:23)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
I have downloaded ojdbc14.jar and kept in C:\Program Files\Java\jdk1.7.0\jre\lib path.
I do not know where i am going wrong?...plz help me here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用非常旧版本的 Oracle JDBC 驱动程序。您应该使用 ojdbc6.jar。
You are using awfully old version of Oracle JDBC driver. You should be using ojdbc6.jar.
尝试使用此驱动程序:
检查 Netbeans 中的类路径:
如何在 NetBeans 中设置类路径:
在 NetBeans 项目属性窗口中,单击左侧面板中的“库”,右侧面板中有 4 类可以配置的类路径:
传播到类路径的其他类别,因此您不需要
在所有 4 个类别中重复同一组 jar 文件。
已编译的类(例如,构建/类)。
类路径、编译类(例如,构建/类)和 JUnit。
编译的测试。
Try with this driver:
Check your classpath in Netbeans:
How to set classpath in NetBeans:
In NetBeans Project Properties Window, you click Libraries in the left panel, and in the right panel are 4 categories of classpath you can configure:
propagated to other categories of classpath, so you don't need to
repeat the same set of jar files in all 4 categories.
compiled classes (e.g., build/classes).
classpath, compiled classes (e.g., build/classes), and JUnit.
compiled tests.