您的位置:首页 > 数据库 > Oracle

JAVA 调用Oracle 及存储过程

2013-03-22 10:44 330 查看
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@192.168.1.48:1521:orcl";
String user="scott";
String password="tiger";
Connection ct= DriverManager.getConnection(url,user,password);

Statement s = ct.createStatement();
ResultSet r = s.executeQuery("SELECT empno,ename from emp");
while(r.next()) {
System.out.println(r.getString("empno") + ", " + r.getString("ename"));
}

r.close();
s.close();
ct.close();
}catch(Exception e){
e.printStackTrace();
}
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@192.168.1.48:1521:orcl";
String user="scott";
String password="tiger";
Connection ct= DriverManager.getConnection(url,user,password);

CallableStatement proc =null;
proc = ct.prepareCall("{ call sp_t2('yang',25)}");
proc.execute();
ct.close();
}catch(Exception e){
e.printStackTrace();
}
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@192.168.1.48:1521:orcl";
String user="scott";
String password="tiger";
Connection ct= DriverManager.getConnection(url,user,password);

CallableStatement proc =null;
proc = ct.prepareCall("{ call sp_t2(?,?)}");
proc.setString(1,"gq");
proc.setInt(2,24);
proc.execute();
ct.close();
}catch(Exception e){
e.printStackTrace();
}
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@192.168.1.48:1521:orcl";
String user="scott";
String password="tiger";
Connection ct= DriverManager.getConnection(url,user,password);

CallableStatement proc =null;
proc = ct.prepareCall("{ call sp_emp(?,?)}");
proc.setInt(1,7844);
proc.registerOutParameter(2,Types.VARCHAR);
proc.execute();

String testP = proc.getString(2);
System.out.println(testP);

ct.close();
}catch(Exception e){
e.printStackTrace();
}
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@192.168.1.48:1521:orcl";
String user="scott";
String password="tiger";
Connection ct= DriverManager.getConnection(url,user,password);

CallableStatement proc =null;
ResultSet rs = null;
proc = ct.prepareCall("{ call sp_emp2(?)}");
proc.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);
proc.execute();

rs = (ResultSet)proc.getObject(1);
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2));
}

ct.close();
}catch(Exception e){
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: