1. jdbc : java数据库连接技术
2.主要用到的类及接口
Class | Driver | ManagerDriver | Connection | Statement | PreparedStatement | CallableStatement | ResultSet | SQLException |
3.JDBC批量处理
4.JDBC数据流
blob、clob
5.对象
statement:boolean execute (String SQL)
: 如果可以检索到ResultSet
对象,则返回一个布尔值true
; 否则返回false
preparedstatement: 可以使用输入参数的SQL语句, setXXX()
方法将值绑定到参数
CallableStatement:registerOutParameter(3, java.sql.Types.INTEGER);
6.ResultSet
prepareStatement(String SQL, int RSType, int RSConcurrency);
类型:ResultSet.TYPE_SCROLL_SENSITIVE
并发:ResultSet.CONCUR_UPDATABLE
- updateString(int columnIndex, String s) ;updateRow()
7.简单示例
1 package com.rong.web; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class Test1 { 10 11 /** 12 * @author 容杰龙 13 * 14 */ 15 public static void main(String[] args) { 16 Connection conn = null; 17 Statement cs = null; 18 ResultSet rs = null; 19 try { 20 // 加载MySQL数据库驱动 21 Class.forName("com.mysql.jdbc.Driver"); 22 String url = "jdbc:mysql://localhost:3306/rjl"; 23 String user = "root"; 24 String password = "123123"; 25 // 获取连接对象的三种方式 26 conn = DriverManager.getConnection(url, user, password); 27 28 // url="jdbc:mysql://localhost:3306/rjl?user=root&password=123123"; 29 // conn =DriverManager.getConnection(url); 30 31 // Properties properties=new Properties(); 32 // properties.setProperty("user", "root"); 33 // properties.setProperty("password", "123123"); 34 // conn =DriverManager.getConnection(url, properties); 35 36 // 创建语句对象 37 cs = conn.createStatement(); 38 String sql = "select * from emp;"; 39 // 执行语句 40 rs = cs.executeQuery(sql); 41 while (rs.next()) { 42 // 获取结果集中的数据 43 System.out.println(rs.getString("ename")); 44 } 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } finally { 48 // 关闭资源 49 try { 50 if (rs != null) { 51 rs.close(); 52 } 53 } catch (SQLException e) { 54 e.printStackTrace(); 55 } 56 try { 57 if (cs != null) { 58 cs.close(); 59 } 60 } catch (SQLException e) { 61 e.printStackTrace(); 62 } 63 try { 64 if (conn != null) { 65 conn.close(); 66 } 67 } catch (SQLException e) { 68 e.printStackTrace(); 69 } 70 } 71 72 } 73 74 }