Java工具类如何实现数据库查询连接?
- 内容介绍
- 文章标签
- 相关推荐
本文共计399个文字,预计阅读时间需要2分钟。
通过主键ID获取对象:
javapublic static T getObject(Class c, int id) { T t=null; ResultSet rs=null; try { // 获取所有列名 List columns=new ArrayList(); // 根据类生成列名 addColumnByClass(c, columns); // 生成SQL语句 String sql=SELECT + String.join(, , columns) + FROM + c.getSimpleName() + WHERE id=?; // 执行查询 rs=executeQuery(sql, id); if (rs.next()) { t=c.getDeclaredConstructor().newInstance(); for (String column : columns) { // 设置对象属性 setProperty(t, column, rs.getObject(column)); } } } catch (Exception e) { e.printStackTrace(); } finally { close(rs); } return t;}
gistfile1.txt/**通过主键ID获取对象*/
public static
本文共计399个文字,预计阅读时间需要2分钟。
通过主键ID获取对象:
javapublic static T getObject(Class c, int id) { T t=null; ResultSet rs=null; try { // 获取所有列名 List columns=new ArrayList(); // 根据类生成列名 addColumnByClass(c, columns); // 生成SQL语句 String sql=SELECT + String.join(, , columns) + FROM + c.getSimpleName() + WHERE id=?; // 执行查询 rs=executeQuery(sql, id); if (rs.next()) { t=c.getDeclaredConstructor().newInstance(); for (String column : columns) { // 设置对象属性 setProperty(t, column, rs.getObject(column)); } } } catch (Exception e) { e.printStackTrace(); } finally { close(rs); } return t;}
gistfile1.txt/**通过主键ID获取对象*/
public static

