Eclipse 中连接Oracle怎么连接?java 老是有错误
packagecom.sun.jdbc;importjava.sql.Connection;importjava.sql.DriverManager;importjava...
package com.sun.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
public void test()
{
Connection con;
Statement st;
ResultSet rs;
try {
Class.forName("Oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
String user ="scott";
String password = "tiger";
con = DriverManager.getConnection(url,user,password);
st = con.createStatement();
String sql = "select * from emp";
rs = st.executeQuery(sql);
while(rs.next()){
rs.getInt(1);
rs.getString("ename");
}
rs.close();
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestJDBC tj = new TestJDBC();
tj.test();
}
}
错误提示:java.sql.SQLException: Io 异常: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:274)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:319)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:344)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:148)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:545)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.sun.jdbc.TestE.test1(TestE.java:20)
at com.sun.jdbc.TestE.main(TestE.java:153)
我的ORACLE服务打开了,其他的工作都好了比如加入ojdbc14.jar
开启了,我但是VISTA操作系统,装的11g,引包jodbc14.jar 展开
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
public void test()
{
Connection con;
Statement st;
ResultSet rs;
try {
Class.forName("Oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
String user ="scott";
String password = "tiger";
con = DriverManager.getConnection(url,user,password);
st = con.createStatement();
String sql = "select * from emp";
rs = st.executeQuery(sql);
while(rs.next()){
rs.getInt(1);
rs.getString("ename");
}
rs.close();
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestJDBC tj = new TestJDBC();
tj.test();
}
}
错误提示:java.sql.SQLException: Io 异常: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:274)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:319)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:344)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:148)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:545)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.sun.jdbc.TestE.test1(TestE.java:20)
at com.sun.jdbc.TestE.main(TestE.java:153)
我的ORACLE服务打开了,其他的工作都好了比如加入ojdbc14.jar
开启了,我但是VISTA操作系统,装的11g,引包jodbc14.jar 展开
4个回答
展开全部
//除了连接方式有区别,Oracle数据库的使用和SQL Server是一样的
public class DBOperator {
public static Connection getConnection()
{
Connection conn=null;
try
{
//在项目中增加一个包含Oracle驱动的jar文件,C:\oracle\ora90\jdbc\lib\classes12.jar
//右键单击项目,选择弹出菜单最后一项Properties,在目录结构中选择Java Builder Path,选择Libraries选项卡,单击Add External JARs...,增加上面的jar文件就可以把Oracle的驱动程序加到项目中。
Class.forName("oracle.jdbc.driver.OracleDriver");
//连接字符串:sunny位置写oracle的连接标识符(全局数据库名),后面接用户名和密码
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:sunny","system","manager");
return conn;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
public class DBOperator {
public static Connection getConnection()
{
Connection conn=null;
try
{
//在项目中增加一个包含Oracle驱动的jar文件,C:\oracle\ora90\jdbc\lib\classes12.jar
//右键单击项目,选择弹出菜单最后一项Properties,在目录结构中选择Java Builder Path,选择Libraries选项卡,单击Add External JARs...,增加上面的jar文件就可以把Oracle的驱动程序加到项目中。
Class.forName("oracle.jdbc.driver.OracleDriver");
//连接字符串:sunny位置写oracle的连接标识符(全局数据库名),后面接用户名和密码
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:sunny","system","manager");
return conn;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
参考资料: 中南大学软件学院
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
The Network Adapter could not establish the connection
应该是你的Oracle监听没有开启!!!!
你的plsql能不能连上Oracle???????
应该是你的Oracle监听没有开启!!!!
你的plsql能不能连上Oracle???????
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我开刷刷.你们接着聊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询