JDBC mysql 相关内容笔记
程序员文章站
2022-04-28 12:42:27
解决乱码: url字符串加上?useUnicode=true&characterEncoding=utf-8; mysql数据库无法插入中文数据问题:将mysql数据库的编码改为utf-8; ResultSet中get(列名)方法不一定获取的是数据库字段名,也有可能是自定义的别名,例如:select ......
解决乱码: url字符串加上?useunicode=true&characterencoding=utf-8;
mysql数据库无法插入中文数据问题:将mysql数据库的编码改为utf-8;
resultset中get(列名)方法不一定获取的是数据库字段名,也有可能是自定义的别名,例如:select id,name,password pwd from user 此时resultset.get("password")会抛出异常!
反射封装jdbc:
1 package com.jdbc; 2 3 import java.lang.reflect.field; 4 import java.sql.connection; 5 import java.sql.drivermanager; 6 import java.sql.preparedstatement; 7 import java.sql.resultset; 8 import java.sql.resultsetmetadata; 9 import java.sql.sqlexception; 10 import java.util.arraylist; 11 import java.util.list; 12 13 public class testreflectionjdbc<t> { 14 15 private static connection connection = null; 16 17 private static resultset resultset = null; 18 19 private static preparedstatement preparedstatement = null; 20 21 public static void main(string[] args) { 22 23 } 24 25 public connection getconnection() throws exception { 26 class.forname("com.mysql.jdbc.driver"); 27 if (connection != null) { 28 connection = drivermanager.getconnection( 29 "jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8", "root", "123456"); 30 } 31 return connection; 32 } 33 34 public list<t> query(string sql, class<t> clzz, object... obj) throws exception { 35 list<t> list = new arraylist<t>(); 36 preparedstatement = getconnection().preparestatement(sql); 37 for (int i = 0; i < obj.length; i++) { 38 preparedstatement.setobject(i + 1, obj[i]); 39 } 40 resultset = preparedstatement.executequery(); 41 if (resultset != null) { 42 resultsetmetadata resultsetmetadata = null; 43 while (resultset.next()) { 44 if (resultsetmetadata == null) { 45 resultsetmetadata = resultset.getmetadata(); 46 int count = resultsetmetadata.getcolumncount(); 47 t t = clzz.newinstance(); 48 for (int i = 0; i < count; i++) { 49 string name = resultsetmetadata.getcolumnname(i + 1); 50 field field = clzz.getdeclaredfield(name); 51 field.setaccessible(true); 52 object value = resultset.getobject(name); 53 field.set(t, value); 54 } 55 list.add(t); 56 } 57 } 58 } 59 return list; 60 } 61 62 public int update(string sql, object... obj) { 63 int result = 0; 64 try { 65 preparedstatement = getconnection().preparestatement(sql); 66 for (int i = 0; i < obj.length; i++) { 67 preparedstatement.setobject(i + 1, obj[i]); 68 } 69 result = preparedstatement.executeupdate(); 70 } catch (sqlexception e) { 71 e.printstacktrace(); 72 } catch (exception e) { 73 e.printstacktrace(); 74 } finally { 75 if (preparedstatement != null) { 76 try { 77 preparedstatement.close(); 78 } catch (sqlexception e) { 79 e.printstacktrace(); 80 } 81 } 82 if (resultset != null) { 83 try { 84 resultset.close(); 85 } catch (sqlexception e) { 86 e.printstacktrace(); 87 } 88 } 89 if (connection != null) { 90 try { 91 connection.close(); 92 } catch (sqlexception e) { 93 e.printstacktrace(); 94 } 95 } 96 } 97 return result; 98 } 99 100 }
下一篇: 性能优化:一次性返回大量数据渲染DOM