JDBCUtil
1 package com.dingheng.util; 2 3 import java.sql.*; 4 5 /** 6 * TODO 7 * 8 * @ClassName: JDBCUtil 9 * @author: DingH 10 * @since: 2019/5/28 16:39 11 */ 12 public class JDBCUtil { 13 private Connection conn = null; 14 private PreparedStatement stmt = null; 15 private ResultSet rs = null; 16 private static String driver = "com.mysql.jdbc.Driver"; 17 private String url = "jdbc:mysql://localhost:3306/Student"; 18 private String user = "root"; 19 private String password = "root"; 20 21 /** 22 * Get the driver 23 */ 24 static { 25 26 } 27 /** 28 * Connect the database 29 */ 30 public Connection getCon() { 31 try { 32 Class.forName(driver); 33 } catch (ClassNotFoundException e) { 34 e.printStackTrace(); 35 } 36 try { 37 conn = (Connection) DriverManager 38 .getConnection(url, user, password); 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 return conn; 43 } 44 45 /** 46 * @param sql 47 * @param obj 48 *Update 49 */ 50 public int update(String sql, Object... obj) { 51 int count = 0; 52 conn = getCon(); 53 try { 54 stmt = conn.prepareStatement(sql); 55 if (obj != null) { 56 for (int i = 0; i < obj.length; i++) { 57 stmt.setObject(i + 1, obj[i]); 58 } 59 } 60 count = stmt.executeUpdate(); 61 } catch (SQLException e) { 62 e.printStackTrace(); 63 } finally { 64 close(); 65 } 66 return count; 67 } 68 69 /** 70 * @param sql 71 * @param obj 72 * Query 73 */ 74 public ResultSet Query(String sql, Object... obj) { 75 conn = getCon(); 76 try { 77 stmt = conn.prepareStatement(sql); 78 if (obj != null) { 79 for (int i = 0; i < obj.length; i++) { 80 stmt.setObject(i + 1, obj[i]); 81 } 82 } 83 rs = stmt.executeQuery(); 84 } catch (SQLException e) { 85 e.printStackTrace(); 86 } finally { 87 // close(); 88 } 89 return rs; 90 } 91 92 /** 93 * CLose the resource 94 */ 95 public void close() { 96 try { 97 if (rs != null) { 98 rs.close(); 99 } 100 } catch (SQLException e) { 101 e.printStackTrace(); 102 } finally { 103 try { 104 if (stmt != null) { 105 stmt.close(); 106 } 107 } catch (SQLException e) { 108 e.printStackTrace(); 109 } finally { 110 if (conn != null) { 111 try { 112 conn.close(); 113 } catch (SQLException e) { 114 e.printStackTrace(); 115 } 116 } 117 } 118 } 119 } 120 121 122 /** 123 * @Title: test 124 * @param 125 * @return 126 */ 127 public static void main(String[] args) { 128 //insert 129 Student s=new Student(); 130 s.setId(12); 131 s.setAge(23); 132 s.setName("xxt"); 133 JDBCUtil u=new JDBCUtil(); 134 String sql="insert into T_Student(id,age,name) values(?,?,?)"; 135 int result=u.update(sql, s.getId(),s.getAge(),s.getName()); 136 System.out.println(result); 137 138 //query 139 String sql1="select * from T_Student where age=?"; 140 Student s=new Student(); 141 s.setAge(23); 142 JDBCUtil u1=new JDBCUtil(); 143 ResultSet rs=u1.Query(sql1, s.getAge()); 144 try { 145 while(rs.next()){ 146 //s.setName(rs.getString("name")); 147 System.out.println(rs.getString("name"));//s.getName(rs.getString("name")) 148 } 149 } catch (SQLException e) { 150 // TODO Auto-generated catch block 151 e.printStackTrace(); 152 } 153 } 154 }