JDBC操作学习-5
JDBC的封装
- 问题:在数据库操作方法中关于驱动加载和数据库连接对象的代码是重复的.会造成修改数据源特别麻烦. - 解决1:将jdbc参数在功能类中提取为全局变量
 
- 问题2:如果使用解决方式1解决后,修改了数据源则必须重启程序.而开发过程中尽量要求,在不重启程序的情况下完成对代码的参数的修改. - 解决2:将jdbc参数存储到properties属性配置文件中,封装工具类进行获取.
 
- 知识点: - properties文件是专门用来存储属性配置的文件,格式要求必须是键值对,以=号隔开.一行一组键值对,并且不能使用分号结尾.可以使用Properties对象来进行读取该文件的内容.
 
- 使用: - 创建java工具类(Util-JdbcUtil)
- 创建jdbc参数静态变量
- 创建静态代码块
- 创建properties对象(db.properties)
- 获取properties文件流对象
- 加载属性配置文件
- 获取jdbc参数并赋值给静态变量
- 加载驱动
- 创建获取Connection对象的静态方法
- 创建获取PreparedStatement对象的静态方法
- 创建获取Statement对象的静态方法
- 创建关闭资源静态方法
- 创建增删改的封装方法–executeDML
 
- 注意:需要在src下声明存储了jdbc参数的properties文件(db.properties): - 1 
 2
 3
 4- driver= oracle.jdbc.driver.OracleDriver 
 url=jdbc:oracle:thin:@localhost:1521:XE
 username=scott
 password=tiger- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119- JDBCUtil封装源码: 
 public class JdbcUtil {
 private static String driver;
 private static String url;
 private static String username;
 private static String password;
 static{
 //创建properties对象获取属性文件的内容
 Properties p=new Properties();
 //获取属性文件的读取流对象
 InputStream is=JdbcUtil.class.getResourceAsStream("/db.properties");
 try {
 //加载属性配置文件
 p.load(is);
 //获取jdbc参数
 driver=p.getProperty("driver");
 url=p.getProperty("url");
 username=p.getProperty("username");
 password=p.getProperty("password");
 //加载驱动
 Class.forName(driver);
 } catch (IOException e) {
 e.printStackTrace();
 } catch (ClassNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
 //获取Connection对象
 public static Connection getConnection(){
 Connection conn=null;
 try {
 conn=DriverManager.getConnection(url, username, password);
 } catch (SQLException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 return conn;
 
 }
 //封装获取PreparedStatement对象
 public static PreparedStatement getPreparedStatement(String sql,Connection conn){
 
 PreparedStatement ps=null;
 try {
 ps =conn.prepareStatement(sql);
 } catch (SQLException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 return ps;
 
 }
 //封装获取Statement对象
 public static Statement getStatement(Connection conn){
 Statement stmt=null;
 try {
 stmt = conn.createStatement();
 } catch (SQLException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 return stmt;
 
 }
 //关闭资源
 public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
 try {
 rs.close();
 } catch (Exception e) {
 
 }
 try {
 stmt.close();
 } catch (SQLException e) {
 
 }
 try {
 conn.close();
 } catch (SQLException e) {
 
 }
 }
 //封装DML
 public static int executeDML(String sql,Object...objs){
 //创建连接对象
 Connection conn=getConnection();
 //创建sql命令对象
 PreparedStatement ps=JdbcUtil.getPreparedStatement(sql, conn);
 //给占位符赋值
 try {
 conn.setAutoCommit(false);
 for(int i=0;i<objs.length;i++){
 ps.setObject(i+1, objs[i]);
 }
 int i=ps.executeUpdate();
 conn.commit();
 return i;
 } catch (Exception e) {
 try {
 conn.rollback();
 } catch (SQLException e1) {
 // TODO Auto-generated catch block
 e1.printStackTrace();
 }
 }finally{
 //关闭资源
 JdbcUtil.closeAll(null, ps, conn);
 }
 //返回结果
 return -1;
 }
 }
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Kylin!
 评论












