我用的是class12.jar,给你一个例子:
//File Demo1.java
import java.sql.*;
import java.io.*;
import java.util.*;
public class Demo1
{
public static void main(String[] args)
{
try
{
String url = "jdbc:oracle:thin:@192.168.16.68:1521:ORA816";
//注册驱动器
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
DriverManager.setLoginTimeout(1);
//建立数据库连接
Connection con = DriverManager.getConnection(url, "dwl", "jingyi");
//创建statement
Statement stmt = con.createStatement();
//1、删除数据(不带参数)
String del_row = "delete from emp";
int ret = stmt.executeUpdate(del_row);
System.out.println("删除记录条数:" + ret);
//2、简单插入数据
int i;
for(i=0;i<9;i++){
String ins_row = "insert into emp values ('" + "dwl_" + i + "'," + i + ")";
stmt.executeUpdate(ins_row);
}
//3、删除数据(带参数)
int eno = 2;
String del2_row = "delete from emp where eno=" + eno;
ret = stmt.executeUpdate(del2_row);
System.out.println("删除记录条数:" + ret);
//4、查询数据,返回结果集
String sel_row = "select * from emp order by eno";
ResultSet result = stmt.executeQuery(sel_row);
while(result.next()){
// System.out.println(result.getInt("eno") + " " + result.getString("ename"));
System.out.println(result.getInt(2) + " " + result.getString(1));
}
stmt.close();
//5、调用存储过程(无参数)
String call_p = "call my_procedure_1()";
CallableStatement call_stmt = con.prepareCall(call_p);
call_stmt.executeUpdate();
call_stmt.close();
//6、调用有参数的存储过程
String call_p2= "call my_procedure_4(?, ?, ?)";
CallableStatement call_stmt2 = con.prepareCall(call_p2);
call_stmt2.setString(1, "dwl");
call_stmt2.setInt(2, 1);
call_stmt2.registerOutParameter(3, Types.NUMERIC);
call_stmt2.executeUpdate();
eno = 23;
eno = call_stmt2.getInt(3);
System.out.println(eno);
call_stmt2.close();
con.commit();
con.close();
}
catch(SQLException ex)
{
System.out.println(ex);
}
}
};
评论0
最新资源