[JAVA100例]058、调用存储历程[Java编程]
本文“[JAVA100例]058、调用存储历程[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
import java.sql.*;
/**
* <p>Title: JDBC衔接数据库</p>
* <p>Description: 本实例演示若何利用JDBC衔接Oracle数据库,并演示增添数据和查询数据.</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: JDBCSTMConn.java</p>
* @version 1.0
*/
public class JDBCSTMConn{
private static String url="";
private static String username="";
private static String password="";
/**
*<br>办法阐明:得到数据衔接
*<br>输入参数:
*<br>返回范例:Connection 衔接对象
*/
public Connection conn(){
try {
//加载JDBC驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//成立数据库衔接
Connection con = DriverManager.getConnection(url, username, password);
return con;
}catch(ClassNotFoundException cnf){
System.out.println("driver not find:"+cnf);
return null;
}catch(SQLException sqle){
System.out.println("can´t connection db:"+sqle);
return null;
} catch (Exception e) {
System.out.println("Failed to load JDBC/ODBC driver.");
return null;
}
}
/**
*<br>办法阐明:调用存储历程,察看数据后果
*<br>输入参数:Connection con 数据库衔接
*<br>输入参数:String sql 要履行的SQL语句
*<br>返回范例:
*/
public void execute(Connection con){
CallableStatement toesUp = null;
try {
con.setAutoCommit(false);
//调用存储历程
toesUp = con.prepareCall("{call p_test(?)}");
//传送参数给存储历程
toesUp.setInt(1, 6);
//履行存储历程
toesUp.executeQuery();
Statement stmt = con.createStatement();
ResultSet rs = (ResultSet) stmt.executeQuery("SELECT * FROM TEST");
while (rs.next()) {
String ID = rs.getString(1);
String NAME = rs.getString(2);
System.out.println(ID+ " " +NAME);
}
rs.close();
} catch (SQLException e) {
System.out.println(e);
try{
toesUp.close();
con.close();
}catch(Exception es){System.out.println(es);}
}
}
/**
*<br>办法阐明:实例演示
*<br>输入参数:
*<br>返回范例:
*/
public void demo(){
try{
JDBCSTMConn oc = new JDBCSTMConn();
Connection conn = oc.conn();
oc.execute(conn);
conn.close();
}catch(SQLException se){
System.out.println(se);
}catch(Exception e){
System.out.println(e);
}
}
/**
*<br>办法阐明:主办法
*<br>输入参数:
*<br>返回范例:
*/
public static void main(String[] arg){
if(arg.length!=3){
System.out.println("use: java JDBCSTMConn url username password");
return;
}
JDBCSTMConn oc = new JDBCSTMConn();
oc.url = arg[0];
oc.username=arg[1];
oc.password=arg[2];
oc.demo();
}
}
以上是“[JAVA100例]058、调用存储历程[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |