一.JDBC连接实例(尽量把程序写完善)
1.读驱动
Class.forName()|Class.forName().newInstance()|new DriverName()
实例化时自动向DriverManager注册,不需显式调用DriverManager.registerDriver方法
2.连接数据库
DriverManager.getConnection()
例:
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:FOX";
String user = "scott";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, user, password);
3.执行SQL语句
Connection.CreateStatement()
Statement.executeQuery()
Statement.executeQuery()
例: Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from emp");
4.循环返回结果集
while(rs.next()){
}
5.显示数据库数据
把数据库各种类型转换为JAVA的类型用get***()方法
例:
while(rs.next()) {
System.out.print(rs.getInt("EMPNO")+ " ");
System.out.print(rs.getInt(8)+ " ");
}
6.关闭资源
调用ResultSet|Statement|Connection 的close()方法
例:
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
二.JDBC进阶
1.prepareStatement接口--是Statement的子接口
可以再方便的书写SQL语句,用问号替换字段变量。
String sql = "insert into dept values (?,?,?)";
prepareStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,deptno);
pstmt.setString(2,dname);
pstmt.setString(3, loc);
pstmt.executeUpdate();
2.JDBC调用存储过程--用CallableStatement接口
继承来自Statement和prepareStatement接口
String sql = "{call p(?, ?, ?, ?)}";
CallableStatement cstmt = conn.prepareCall(sql);
cstmt.registerOutParameter(3, Types.INTEGER);
cstmt.setInt(1, 3);
cstmt.setInt(2, 4);
cstmt.execute();
System.out.println(cstmt.getInt(3));
3.JDBC批处理--用prepareStatement接口的addBatch();
String sql = "insert into dept values (?,?,?)";
prepareStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,87);
pstmt.setString(2,'haha');
pstmt.setString(3,'bj');
pstmt.addBatch();
pstmt.executeBatch();
pstmt.close();
4.JDBC处理Transaction
保证同时有效性。
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.addBatch("insert into dept values (51, 'GAME', 'FZ'));
...
stmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
}catch(SQLException e) {
e.printStackTrace();
try{
if(conn != null)
{
conn.rollback();
conn.setAutoCommit(true);
}
}catch(){}
}
5.JDBC处理可以滚动的结果集
设置对滚动敏感不、只读
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("select * from emp order by sal");
rs.next();
System.out.println(rs.getInt(1)); //打印当前字段
rs.last(); //最后一条
System.out.println(rs.getString(1));
System.out.println(rs.isLast()); //是否为最后一条
System.out.println(rs.isAfterLast()); //是否为最后一条的下一条
System.out.println(rs.getRow()); //打印当前是第几条
rs.previous(); //滚动到前一条
rs.absolute(6); //滚动到第六条
System.out.println(rs.getString(1));
rs.close();
6.JDBC处理可更新的结果集 (不过有些数据库不支持像Oracle)
设置对滚动敏感不、可更新
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("select * from emp2");
rs.next();
//更新一行
rs.updateString("ename","AAAA");
rs.updateRow();
//插入新行
rs.moveToInsertRow();
rs.updateString("ename","AAA");
rs.updateInt("mgr",7893);
rs.insertRow();
//光标移到新建的行
rs.moveToCurrentRow();
//删除行
rs.absolute(5);
rs.deleteRow();
//取消更新
rs.cancelRowUpdate();
7.JDBC新增加接口
1).DataSource
DriverManager的替代,可以方便实现连接池和分布式(属性可以动态改变)。
2).RowSet
新的ResultSet
从ResultSet继承
支持断开的结果集
支持JavaBean标准,可以保持连接