两种方式:
thin是一种瘦客户端的连接方式,即采用这种连接方式不需要安装oracle客户端,只要求classpath中包含jdbc驱动的jar包就行。thin就是纯粹用Java写的ORACLE数据库访问接口。
oci 是一种胖客户端的连接方式,即采用这种连接方式需要安装oracle客户端。oci是Oracle Call Interface的首字母缩写,是ORACLE公司提供了访问接口,就是使用Java来调用本机的Oracle客户端,然后再访问数据库,优点是速度 快,但是需要安装和配置数据库。
从使用thin驱动切换到oci驱动在配置来说很简单,只需把连接字符串java:oracle:thin:@hostip:port:实例名换为java:oracle:oci@本地服务名即可。
如:从 jdbc:oracle:thin:@10.1.1.2:1521:xe
改成
jdbc:oracle:oci8:@xe
调用storedprocedure使用方式:
CallableStatement cstmt = con.prepareCall("CALL spname(? , ?)") ;
获取数据库的元数据信息:
DatabaseMetaData dbmd = con.getMetaData();
获取结果集的元数据信息:
ResultSetMetaData rsmd = rs.getMetaData();
statement构造函数可以包含3个参数:
-
resultSetType
,它的取值包括:ResultSet.TYPE_FORWARD_ONLY
、ResultSet.TYPE_SCROLL_INSENSITIVE
或ResultSet.TYPE_SCROLL_SENSITIVE,默认情况下,该参数的值是
ResultSet.TYPE_FORWARD_ONLY
。 -
resultSetConcurrency
,它的取值包括:ResultSet.CONCUR_READ_ONLY
或ResultSet.CONCUR_UPDATABLE
,默认情况下,该参数的值是ResultSet.CONCUR_READ_ONLY
。 -
resultSetHoldability
,它的取值包括:ResultSet.HOLD_CURSORS_OVER_COMMIT
或ResultSet.CLOSE_CURSORS_AT_COMMIT
。
获取数据集的时候,可以设定resultSet.SetFetchSize来避免outofmemory错误的发生。
关于jdbc数据库连接,同一个数据库连接,不能同时做多件事情,必须串行做?还是这是由数据库的隔离级别决定的?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException; public class jdbcdemo { //create table mytest(key varchar2(2000),value varchar2(2000)); public static final String ORACLE_DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver"; public static void main(String[] args)
{
System.out.println("hello jdbc"); final Connection conn = getConn(false);
//selectDemo("select * from mytest");
//insertDemo(conn,10000,1000);
//selectDemo("select * from mytest");
/*
new Thread(){public void run()
{
try {
insertDemo(conn,100,10);
} catch (SQLException e) {
e.printStackTrace();
}
}}.start(); new Thread(){public void run()
{
try {
insertDemo(conn,100,10);
} catch (SQLException e) {
e.printStackTrace();
}
}}.start();
*/
new Thread(){public void run()
{
try {
selectDemo(conn,"select * from mytest");
} catch (SQLException e) {
e.printStackTrace();
}
}}.start();
try {
Thread.sleep(1000);
conn.commit();
} catch (Exception e) {
e.printStackTrace();
} } static Connection getConn(boolean autoCommit)
{
Connection conn = null;
try {
Class.forName(ORACLE_DRIVER_CLASS);
//two method , use thin(pure java not need oracle client installed)
//or oci(need oracle client installed) conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.99.105:1521:xe","system","student");
conn.setAutoCommit(autoCommit);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
return conn;
} static void selectDemo(Connection conn,String sql) throws SQLException
{
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
rs.setFetchSize(1);//it is useful to set this value when the resultset is very large
ResultSetMetaData rsmd = rs.getMetaData();
for(int index=1;index<=rsmd.getColumnCount();index++)
System.out.print(rsmd.getColumnName(index)+"\t");
System.out.println();
while(rs.next())
{
for(int index=1;index<=rsmd.getColumnCount();index++)
System.out.print(rs.getString(index)+"\t");
System.out.println();
}
} static void insertDemo(Connection conn,int rowcount,int batchcount) throws SQLException
{
String sql="insert into mytest(key,value) values(?,?)";
PreparedStatement stmt = conn.prepareStatement(sql); for(int i=1;i<=rowcount;i++)
{
stmt.setString(1,"Key"+i);
stmt.setString(2,"Value"+i+" "+Thread.currentThread().getName());
stmt.addBatch();
if(i % batchcount == 0)
stmt.executeBatch();
}
stmt.executeBatch();
} }