请教大家,希望得以解决,谢谢
import java.sql.*;public class JDBCDemo {
public static void main (String args[]) {
try {
Statement stmt;
PreparedStatement pstmt;
ResultSet rs;
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("加载驱动");
String url = "jdbc:oracle:thin://localhost:1521:oraDB";
// 得到与数据库的连接
Connection con = DriverManager.getConnection(url,"scott","tiger");
// 显示url和连接信息
System.out.println("URL"+ url);
System.out.println("Connection"+ con); // 得到一个Statement对象
stmt = con.createStatement();
// 如果Demo表存在,则删除掉,否则抛出异常
System.out.println("DROP TABLE Demo, if it exists.");
try
{
stmt.executeUpdate("DROP TABLE Demo");
}
catch (Exception ex)
{
System.out.println(ex);
System.out.println("No exitsting table to delete!"); } //在数据库中创建一个表Demo
stmt.executeUpdate("CREATE TABLE Demo ("+"test_id int,test_val char(15) not null)");
System.out.println("表已创建");
//在表中插入一些值
stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(1,'One')");
stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(2,'Two')");
stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(3,'Three')");
stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(3,'Foue')");
stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(3,'Five')");
System.out.println("rows1: " + stmt.getMaxRows());
//得到另外一个Statement对象
stmt = con.createStatement();
//查询表Demo,并将结果存储在ResultSet对象rs中
rs = stmt.executeQuery("SELECT * FROM Demo ORDER BY test_id");
//显示表的所有记录
System.out.println("Display all results:");
System.out.println("rows: " + stmt.getMaxRows());
while (rs.next())
{
int testId = rs.getInt("test_id");
String testVal = rs.getString("test_val");
System.out.println("test_id"+testId + "test_val" + testVal);
}
//更新表中test_val的值
pstmt = con.prepareStatement("UPDATE Demo SET test_val = ? WHERE test_id = ?");
//更新表中的第二条记录的test_val字段的值
//填充UPDATE语句中的"?"
pstmt.setString(1,"hello");
pstmt.setInt(2,2);
pstmt.executeUpdate();
System.out.println("UPDATE row number 2 OK");
//显示表中更新后的第二条记录
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM Demo ORDER BY test_id");
System.out.println("Display row 2:");
while (rs.next())
{
int testId = rs.getInt("test_id");
String testVal = rs.getString("test_val");
System.out.println("test_id" + testId + "test_val" + testVal);
}
stmt.close();
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}以下是我运行时抛出的异常:java.sql.SQLException: IO 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=150999279)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:333)
at oracle.jdbc.driver.OracleConnection.(init)(OracleConnection.java:404) at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:468)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at JDBCDemo.main(JDBCDemo.java:27)
Press any key to contine...
import java.sql.*;public class JDBCDemo {
public static void main (String args[]) {
try {
Statement stmt;
PreparedStatement pstmt;
ResultSet rs;
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("加载驱动");
String url = "jdbc:oracle:thin://localhost:1521:oraDB";
// 得到与数据库的连接
Connection con = DriverManager.getConnection(url,"scott","tiger");
// 显示url和连接信息
System.out.println("URL"+ url);
System.out.println("Connection"+ con); // 得到一个Statement对象
stmt = con.createStatement();
// 如果Demo表存在,则删除掉,否则抛出异常
System.out.println("DROP TABLE Demo, if it exists.");
try
{
stmt.executeUpdate("DROP TABLE Demo");
}
catch (Exception ex)
{
System.out.println(ex);
System.out.println("No exitsting table to delete!"); } //在数据库中创建一个表Demo
stmt.executeUpdate("CREATE TABLE Demo ("+"test_id int,test_val char(15) not null)");
System.out.println("表已创建");
//在表中插入一些值
stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(1,'One')");
stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(2,'Two')");
stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(3,'Three')");
stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(3,'Foue')");
stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(3,'Five')");
System.out.println("rows1: " + stmt.getMaxRows());
//得到另外一个Statement对象
stmt = con.createStatement();
//查询表Demo,并将结果存储在ResultSet对象rs中
rs = stmt.executeQuery("SELECT * FROM Demo ORDER BY test_id");
//显示表的所有记录
System.out.println("Display all results:");
System.out.println("rows: " + stmt.getMaxRows());
while (rs.next())
{
int testId = rs.getInt("test_id");
String testVal = rs.getString("test_val");
System.out.println("test_id"+testId + "test_val" + testVal);
}
//更新表中test_val的值
pstmt = con.prepareStatement("UPDATE Demo SET test_val = ? WHERE test_id = ?");
//更新表中的第二条记录的test_val字段的值
//填充UPDATE语句中的"?"
pstmt.setString(1,"hello");
pstmt.setInt(2,2);
pstmt.executeUpdate();
System.out.println("UPDATE row number 2 OK");
//显示表中更新后的第二条记录
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM Demo ORDER BY test_id");
System.out.println("Display row 2:");
while (rs.next())
{
int testId = rs.getInt("test_id");
String testVal = rs.getString("test_val");
System.out.println("test_id" + testId + "test_val" + testVal);
}
stmt.close();
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}以下是我运行时抛出的异常:java.sql.SQLException: IO 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=150999279)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:333)
at oracle.jdbc.driver.OracleConnection.(init)(OracleConnection.java:404) at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:468)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at JDBCDemo.main(JDBCDemo.java:27)
Press any key to contine...
解决方案 »
- java多线程同步问题
- 转义字符"\n"的输出问题
- 问一个关于static的小白问题
- java .exe 文件运行时的警告 (警告: Duplicate name in Manifest: Sealed)
- 进来看下啊 大虾啊!!!跪到了~~关于回文的判断啊~
- 没有jdk无法验证,不知道(i+'a')==(i+97)对不对?
- 请教各位大哥如何用线程在一个Frame上实现时钟的功能???
- 显示进度信息,在线等
- 条件判断的一个问题
- 这段代码有什么问题?
- 如何把"yyyy-MM-dd"格式的时间转换成毫秒值?
- 我载了Borland JBuilder2005企业版CD1.bin 和Borland JBuilder2005企业版CD2.bin,怎么安装?
谢谢你的热心,一样的url在教室里我能连上的,但不知道为什么在家就连不上
里的localhost改成具体的ip地址
另 家里保证能连到这个数据库服务器
String url = "jdbc:oracle:thin://127.0.0.1:1521:oraDB";
我改成这样,结果是一样的错误.
其次确定oracle是否打开。
最后就是端口1521是否打开。