2018-07-31
MySQL
1 package oop_emp.com.neusoft.dao; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 /** 10 * 数据库操作基础类 11 * 12 * @author xxf 13 * 14 */ 15 public class BaseDao { 16 // 创建4个常量(数据库连接地址,数据库驱动类地址,数据库登录权限名,登录密码) 17 private static final String DRIVER="com.mysql.jdbc.Driver"; 18 private static final String URL="jdbc:mysql://localhost:3306/emp?characterEncoding=utf-8"; 19 private static final String UNAME="root"; 20 private static final String UPWD="123456"; 21 22 // 使用静态块加载数据库的驱动 23 static { 24 try { 25 Class.forName(DRIVER); 26 } catch (ClassNotFoundException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 } 31 // 声明三个核心接口对象(Connection,PreparedStatment,ResultSet) 32 private Connection conn = null;// 数据库连接的对象 33 private PreparedStatement pstmt = null;// SQL命令预处理并执行操作的对象 34 protected ResultSet res = null;// 查询后返回的结果集对象 35 // 编写创建数据库连接对象的方法(DriverManager) 36 private void getConn() { 37 try { 38 conn = DriverManager.getConnection(URL, UNAME, UPWD); 39 } catch (SQLException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 45 // 编写关闭数据库释放资源的方法 46 protected void colseAll() { 47 if (null != res) { 48 try { 49 res.close(); 50 } catch (SQLException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 54 } 55 if (null != pstmt) { 56 try { 57 pstmt.close(); 58 } catch (SQLException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } 62 } 63 if (null != conn) { 64 try { 65 conn.close(); 66 } catch (SQLException e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } 70 } 71 } 72 73 // 编写数据库的查询方法 74 protected ResultSet excuteSelect(String sql, Object[] params) { 75 // 调用数据库连接对象的方法 76 this.getConn(); 77 // 创建预处理对象 78 try { 79 pstmt = conn.prepareStatement(sql); 80 // 通过for循环对参数进行预处理 81 if (null != params) { 82 for (int i = 0; i < params.length; i++) { 83 pstmt.setObject(i+1, params[i]); 84 } 85 } 86 // 操作查询并返回结果集 87 res = pstmt.executeQuery(); 88 } catch (SQLException e) { 89 // TODO Auto-generated catch block 90 e.printStackTrace(); 91 } 92 return res; 93 } 94 // 编写数据库的增删改的方法 95 protected int excuteEdit(String sql,Object[] params){ 96 int count = 0; 97 //调用数据库连接对象的方法 98 this.getConn(); 99 try { 100 //创建SQL命令预处理执行操作的对象 101 pstmt = conn.prepareStatement(sql); 102 //对参数进行预处理 103 for (int i = 0; i < params.length; i++) { 104 pstmt.setObject(i+1, params[i]); 105 } 106 //接收操作执行返回的行数 107 count = pstmt.executeUpdate(); 108 } catch (SQLException e) { 109 // TODO Auto-generated catch block 110 e.printStackTrace(); 111 }finally { 112 this.colseAll(); 113 } 114 return count; 115 } 116 117 }
1 public class TestJDBC3 2 { 3 public static void main(String[] args) 4 { 5 String driver = "com.mysql.jdbc.Driver"; 6 String url = "jdbc:mysql://127.0.0.1:3306/otherww? useUnicode=true&characterEncoding=utf8"; 7 String user = "root"; 8 String password = "123456"; 9 Connection conn = null; 10 PreparedStatement pstmt = null; 11 ResultSet rs = null; 12 13 String sql = "select name,money,id,age from student"; 14 15 try 16 { 17 // 1,加载驱动 18 Class.forName(driver); 19 // 2,获得连接 20 conn = DriverManager.getConnection(url, user, password); 21 // 3,获得状态集 22 pstmt = conn.prepareStatement(sql); 23 // 4,获得结果集 24 rs = pstmt.executeQuery(); 25 // 5,处理结果集 26 while (rs.next()) 27 { 28 int id = rs.getInt("id"); 29 String name = rs.getString("name"); 30 int age = rs.getInt("age"); 31 double money = rs.getDouble("money"); 32 Student s = new Student(); 33 s.setId(id); 34 s.setStuName(name); 35 s.setAge(age); 36 s.setMoney(money); 37 } 38 } 39 catch (Exception e) 40 { 41 e.printStackTrace(); 42 } 43 finally 44 { 45 //6,释放资源 46 try 47 { 48 if(null != rs) 49 { 50 rs.close(); 51 } 52 if(null != pstmt) 53 { 54 pstmt.close(); 55 } 56 if(null != conn) 57 { 58 conn.close(); 59 } 60 } 61 catch (SQLException e) 62 { 63 e.printStackTrace(); 64 } 65 } 66 } 67 }
Oracle
1 package com.neusoft.dao; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 /** 10 * 数据库操作基础类 11 * 12 * @author xxf 13 * 14 */ 15 public class BaseDao { 16 // 创建4个常量(数据库连接地址,数据库驱动类地址,数据库登录权限名,登录密码) 17 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; 18 private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; 19 private static final String UNAME = "scott"; 20 private static final String UPWD = "123"; 21 22 // 使用静态块加载数据库的驱动 23 static { 24 try { 25 Class.forName(DRIVER); 26 } catch (ClassNotFoundException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 } 31 // 声明三个核心接口对象(Connection,PreparedStatment,ResultSet) 32 private Connection conn = null;// 数据库连接的对象 33 private PreparedStatement pstmt = null;// SQL命令预处理并执行操作的对象 34 protected ResultSet res = null;// 查询后返回的结果集对象 35 // 编写创建数据库连接对象的方法(DriverManager) 36 private void getConn() { 37 try { 38 conn = DriverManager.getConnection(URL, UNAME, UPWD); 39 } catch (SQLException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 45 // 编写关闭数据库释放资源的方法 46 protected void colseAll() { 47 if (null != res) { 48 try { 49 res.close(); 50 } catch (SQLException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 54 } 55 if (null != pstmt) { 56 try { 57 pstmt.close(); 58 } catch (SQLException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } 62 } 63 if (null != conn) { 64 try { 65 conn.close(); 66 } catch (SQLException e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } 70 } 71 } 72 73 // 编写数据库的查询方法 74 protected ResultSet excuteSelect(String sql, Object[] params) { 75 // 调用数据库连接对象的方法 76 this.getConn(); 77 // 创建预处理对象 78 try { 79 pstmt = conn.prepareStatement(sql); 80 // 通过for循环对参数进行预处理 81 if (null != params) { 82 for (int i = 0; i < params.length; i++) { 83 pstmt.setObject(i+1, params[i]); 84 } 85 } 86 // 操作查询并返回结果集 87 res = pstmt.executeQuery(); 88 } catch (SQLException e) { 89 // TODO Auto-generated catch block 90 e.printStackTrace(); 91 } 92 return res; 93 } 94 // 编写数据库的增删改的方法 95 protected int excuteEdit(String sql,Object[] params){ 96 int count = 0; 97 //调用数据库连接对象的方法 98 this.getConn(); 99 try { 100 //创建SQL命令预处理执行操作的对象 101 pstmt = conn.prepareStatement(sql); 102 //对参数进行预处理 103 for (int i = 0; i < params.length; i++) { 104 pstmt.setObject(i+1, params[i]); 105 } 106 //接收操作执行返回的行数 107 count = pstmt.executeUpdate(); 108 } catch (SQLException e) { 109 // TODO Auto-generated catch block 110 e.printStackTrace(); 111 }finally { 112 this.colseAll(); 113 } 114 return count; 115 } 116 117 }