链接代码:publicclassTestOracleCrud{//演示对Oracle的Crud操作publicstaticvoidmain(String[]args){Connectionct=null;//与特定数据库的连接(会话)。PreparedStatementps=null;//...
链接代码:
public class TestOracleCrud {
//演示对Oracle 的Crud操作
public static void main(String[] args) {
Connection ct=null;//与特定数据库的连接(会话)。
PreparedStatement ps=null;//表示预编译的 SQL 语句的对象
ResultSet rs=null;//表示数据库结果集的数据表,通常通过执行查询数据库的语句生成
try {
//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.得到连接
ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","yonghu");
//3.创建PreparedStatement 接口引用对象
ps=ct.prepareStatement("select * from emp");
System.out.println(ps);
//如果是添加
//ps=ct.prepareStatement("insert into emp('ename','sal') values('yqm','5000')");
//System.out.println(ps);
//4.完成查询
rs=ps.executeQuery();
//ps.executeUpdate();添加
//5.循环输入结果
while (rs.next()) {
System.out.println(rs.getString("ename"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
//先打开的后关闭,后打开的先关闭;
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs=null;
}
if (ps!=null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ps=null;
}
if(ct!=null)
{
try {
ct.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ct=null;
}
}
}
}
写完后 运行和他怎么老出现
java.sql.SQLException: Io 异常: The Network Adapter could not establish the connection
这个错误 怎么回事? 我查了半天不知道哪里出错!!
展开