package com.xuankai.jdbc;
import java.sql.*;
public class OracleTest2
{
public static void main(String[] args)
{
Connection ct=null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.获得连接
ct = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:orcl","system","Oracle456");
//3.创建statement实例(Statement,PreparedStatement,CallableStatement)
ps = ct.prepareStatement("select * from scott.emp where empno = ? and deptno = ?");
ps.setInt(1,7936);
ps.setInt(2,20);
//4.通过statement向数据库发出sql指令
rs = ps.executeQuery();
// System.out.println(n);
while(rs.next())
{
System.out.println("name: "+rs.getString(2));
}
System.out.println("-----------------------------");
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
//关闭资源(先开后关)
if(rs!=null)
{
try
{
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
rs=null;
}
if(ps!=null)
{
try
{
ps.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
ps=null;
}
if(null!=ct)
{
try
{
ct.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
ct=null;
}
}
}
}
import java.sql.*;
public class OracleTest2
{
public static void main(String[] args)
{
Connection ct=null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.获得连接
ct = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:orcl","system","Oracle456");
//3.创建statement实例(Statement,PreparedStatement,CallableStatement)
ps = ct.prepareStatement("select * from scott.emp where empno = ? and deptno = ?");
ps.setInt(1,7936);
ps.setInt(2,20);
//4.通过statement向数据库发出sql指令
rs = ps.executeQuery();
// System.out.println(n);
while(rs.next())
{
System.out.println("name: "+rs.getString(2));
}
System.out.println("-----------------------------");
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
//关闭资源(先开后关)
if(rs!=null)
{
try
{
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
rs=null;
}
if(ps!=null)
{
try
{
ps.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
ps=null;
}
if(null!=ct)
{
try
{
ct.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
ct=null;
}
}
}
}