先前一直都是用的直接用加载驱动 然后创建连接进行操作数据 如果我的数据库换了 那么要修改的地方也比较多 不利于维护 所以就想到了将所有配置连接信息都用xml封装起来 以至于我每次都只要修改一下我的xml配置文件 不需要修改我的代码 这也就有了下面的操作 将驱动 url 用户名和密码都写到xml文件里面
1 <?xml version="1.0" encoding="UTF-8"?>2 <database>3 <driver>com.mysql.jdbc.Driver</driver>4 <url>jdbc:mysql://localhost:3306/employee</url>5 <user>root</user>6 <password>root</password>7 </database>
有了xml 文件之后就需要来进行解析
1 // 使用dom4j解析xml 2 private static void parseXmlInfo() { 3 // 创建saxreader对象 4 SAXReader saxReader = new SAXReader(); 5 try { 6 // 加载xml文件 7 Document doc = saxReader.read(DBUtil.class.getResourceAsStream("/jdbc.xml")); 8 // 获得根元素 9 Element root = doc.getRootElement(); 10 // 获得对应的元素的文本值11 driver = root.elementText("driver"); 12 url = root.elementText("url"); 13 user = root.elementText("user"); 14 password = root.elementText("password"); 15 16 } catch (DocumentException e) { 17 // TODO Auto-generated catch block18 e.printStackTrace(); 19 } 20 }
解析完了之后就可以获取连接操作数据库
将增删改封装到一个方法里面 以后每次只需要写sql语句就可以
1 /** 2 * 修改数据的方法 3 * 4 * @param sql 5 * @param values sql中所有?的值 6 * 7 * @return 0表示修改失败,其他表示修改成功 8 */ 9 public static int update(String sql, Object[] values) { 10 PreparedStatement ps = null; 11 ResultSet rs = null; 12 // 获取连接13 getConnection(); 14 try { 15 // 创建prepareStatement16 ps = conn.prepareStatement(sql); 17 System.out.println(ps); 18 for (int i = 0; i < values.length; i++) { 19 ps.setObject(i + 1, values[i]); 20 } 21 System.out.println(ps); 22 // 执行修改语句返回受影响的行数23 int num = ps.executeUpdate(); 24 System.out.println(num); 25 } catch (SQLException e) { 26 e.printStackTrace(); 27 } finally { 28 // 关闭资源29 if (ps != null) { 30 try { 31 ps.close(); 32 } catch (SQLException e) { 33 // TODO Auto-generated catch block34 e.printStackTrace(); 35 } 36 } 37 } 38 return 0; 39 }
将查询封装在一个方法里面
1 /** 2 * 查询的方法 3 * 4 * @param sql 5 * @param values sql中?的值 6 * @return 查询到的数据 7 */ 8 public static List<Map<String, String>> query(String sql, Object[] values) { 9 PreparedStatement ps = null; 10 ResultSet res = null; 11 List<Map<String, String>> list = new ArrayList<>(); 12 getConnection(); 13 try { 14 //创建语句对象15 ps = conn.prepareStatement(sql); 16 //为ps中的?设置值17 if (values != null && values.length > 0) { 18 for (int i = 0; i < values.length; i++) { 19 ps.setObject(i + 1, values[i]); 20 } 21 } 22 // 执行查询操作23 res = ps.executeQuery(); 24 //获得结果集中所有的列的信息25 ResultSetMetaData metaData = res.getMetaData(); 26 // 获取到列的总数27 int columnCount = metaData.getColumnCount(); 28 while (res.next()) { 29 // 创建Map集合对象,用于存储一行数据30 Map<String, String> map = new HashMap<>(); 31 for (int i = 0; i < columnCount; i++) { 32 // 获得列名33 String columnNames = metaData.getColumnName(i + 1); 34 // 获得列名指定的数据35 String columnValues = res.getString(columnNames); 36 // 把数据放到map集合中37 map.put(columnNames, columnValues); 38 } 39 list.add(map); 40 } 41 } catch (SQLException e) { 42 // TODO Auto-generated catch block43 e.printStackTrace(); 44 } finally { 45 if (res != null) { 46 try { 47 res.close(); 48 } catch (SQLException e) { 49 // TODO Auto-generated catch block50 e.printStackTrace(); 51 } 52 } 53 if (ps != null) { 54 try { 55 ps.close(); 56 } catch (SQLException e) { 57 // TODO Auto-generated catch block58 e.printStackTrace(); 59 } 60 } 61 } 62 return list; 63 64 }
完整代码
1 1 package com.newroad.xmlparsedbuitl; 2 2 import java.sql.Connection; 3 3 import java.sql.DriverManager; 4 4 import java.sql.PreparedStatement; 5 5 import java.sql.ResultSet; 6 6 import java.sql.ResultSetMetaData; 7 7 import java.sql.SQLException; 8 8 import java.util.ArrayList; 9 9 import java.util.HashMap; 10 10 import java.util.List; 11 11 import java.util.Map; 12 12 import org.dom4j.Document; 13 13 import org.dom4j.DocumentException; 14 14 import org.dom4j.Element; 15 15 import org.dom4j.io.SAXReader; 16 16 17 17 public class DBUtil { 18 18 private static String driver; 19 19 private static String url; 20 20 private static String user; 21 21 private static String password; 22 22 private static Connection conn = null; 23 23 24 24 // 解析xml文件 获取驱动 用户名 密码 由于不需要每次加载可以写在静态方法中 25 25 static { 26 26 parseXmlInfo(); 27 27 } 28 28 29 29 // 创建连接 30 30 public static void getConnection() { 31 31 // 判断一下如果conn为空或者被关闭就开连接 节省资源 32 32 try { 33 33 if (conn == null || conn.isClosed()) { 34 34 // 加载驱动获取连接 35 35 Class.forName(driver); 36 36 conn = DriverManager.getConnection(url + "?characterEncoding=utf-8", user, password); 37 37 } 38 38 } catch (SQLException e) { 39 39 e.printStackTrace(); 40 40 } catch (ClassNotFoundException e) { 41 41 // TODO Auto-generated catch block 42 42 e.printStackTrace(); 43 43 } 44 44 } 45 45 /** 46 46 * 修改数据的方法 47 47 * 48 48 * @param sql 49 49 * @param values 50 sql语句中所有?的值 51 51 * @return 0表示修改失败,其他表示修改成功 52 52 */ 53 53 public static int update(String sql, Object[] values) { 54 54 PreparedStatement ps = null; 55 55 ResultSet rs = null; 56 56 // 获取连接 57 57 getConnection(); 58 58 try { 59 59 // 创建prepareStatement 60 60 ps = conn.prepareStatement(sql); 61 61 System.out.println(ps); 62 62 for (int i = 0; i < values.length; i++) { 63 63 ps.setObject(i + 1, values[i]); 64 64 } 65 65 System.out.println(ps); 66 66 // 执行修改语句返回受影响的行数 67 67 int num = ps.executeUpdate(); 68 68 System.out.println(num); 69 69 } catch (SQLException e) { 70 70 e.printStackTrace(); 71 71 } finally { 72 72 // 关闭资源 73 73 if (ps != null) { 74 74 try { 75 75 ps.close(); 76 76 } catch (SQLException e) { 77 77 // TODO Auto-generated catch block 78 78 e.printStackTrace(); 79 79 } 80 80 } 81 81 } 82 82 return 0; 83 83 } 84 84 85 85 /** 86 86 * 查询的方法 87 87 * 88 88 * @param sql 89 89 * @param values 90 90 * @return 查询到的数据 91 91 */ 92 92 public static List<Map<String, String>> query(String sql, Object[] values) { 93 93 PreparedStatement ps = null; 94 94 ResultSet res = null; 95 95 List<Map<String, String>> list = new ArrayList<>(); 96 96 getConnection(); 97 97 try { 98 98 //创建语句对象 99 99 ps = conn.prepareStatement(sql);100 100 //为ps中的?设置值101 101 if (values != null && values.length > 0) {102 102 for (int i = 0; i < values.length; i++) {103 103 ps.setObject(i + 1, values[i]);104 104 }105 105 }106 106 // 执行查询操作107 107 res = ps.executeQuery();108 108 //获得结果集中所有的列的信息109 109 ResultSetMetaData metaData = res.getMetaData();110 110 // 获取到列的总数111 111 int columnCount = metaData.getColumnCount();112 112 while (res.next()) {113 113 // 创建Map集合对象,用于存储一行数据114 114 Map<String, String> map = new HashMap<>();115 115 for (int i = 0; i < columnCount; i++) {116 116 // 获得列名117 117 String columnNames = metaData.getColumnName(i + 1);118 118 // 获得列名指定的数据119 119 String columnValues = res.getString(columnNames);120 120 // 把数据放到map集合中121 121 map.put(columnNames, columnValues);122 122 }123 123 list.add(map);124 124 }125 125 } catch (SQLException e) {126 126 // TODO Auto-generated catch block127 127 e.printStackTrace();128 128 } finally {129 129 if (res != null) {130 130 try {131 131 res.close();132 132 } catch (SQLException e) {133 133 // TODO Auto-generated catch block134 134 e.printStackTrace();135 135 }136 136 }137 137 if (ps != null) {138 138 try {139 139 ps.close();140 140 } catch (SQLException e) {141 141 // TODO Auto-generated catch block142 142 e.printStackTrace();143 143 }144 144 }145 145 }146 146 return list;147 147 148 148 }149 149 150 150 // 使用dom4j解析xml151 151 private static void parseXmlInfo() {152 152 // 创建saxreader对象153 153 SAXReader saxReader = new SAXReader();154 154 try {155 155 // 加载xml文件156 156 Document doc = saxReader.read(DBUtil.class.getResourceAsStream("/jdbc.xml"));157 157 // 获得根元素158 158 Element root = doc.getRootElement();159 159 // 获得对应的元素的文本值160 160 driver = root.elementText("driver");161 161 url = root.elementText("url");162 162 user = root.elementText("user");163 163 password = root.elementText("password");164 164 165 165 } catch (DocumentException e) {166 166 // TODO Auto-generated catch block167 167 e.printStackTrace();168 168 }169 169 }170 170 /**171 171 * 关闭资源的方法172 172 */173 173 public static void closeConnection() {174 174 try {175 175 if(conn != null && !conn.isClosed()) {176 176 conn.close();177 177 }178 178 } catch (SQLException e) {179 179 // TODO Auto-generated catch block180 180 e.printStackTrace();181 181 }182 182 }183 183 184 184 }
DBUtil