JDBC工具類封裝

封裝java

package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * @author sawyer 2014下午1:20:16 * */ public class JdbcUtil { private Connection conn = null; private PreparedStatement stmt = null; private ResultSet rs = null; private static String driver = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost:3306/Student"; private String user = "root"; private String password = "root"; /** * Get the driver */ static { } /** * Connect the database */ public Connection getCon() { try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn = (Connection) DriverManager .getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } return conn; } /** * @param sql * @param obj *Update */ public int update(String sql, Object... obj) { int count = 0; conn = getCon(); try { stmt = conn.prepareStatement(sql); if (obj != null) { for (int i = 0; i < obj.length; i++) { stmt.setObject(i + 1, obj[i]); } } count = stmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { close(); } return count; } /** * @param sql * @param obj * Query */ public ResultSet Query(String sql, Object... obj) { conn = getCon(); try { stmt = conn.prepareStatement(sql); if (obj != null) { for (int i = 0; i < obj.length; i++) { stmt.setObject(i + 1, obj[i]); } } rs = stmt.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } finally { // close(); } return rs; } /** * CLose the resource */ public void close() { try { if (rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (stmt != null) { stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } }

modelmysql

package model; import java.io.Serializable; public class Student implements Serializable{ private int id; private int age; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

測試sql

package model;
 
import java.sql.ResultSet; import java.sql.SQLException; import util.JdbcUtil; public class Test { public static void main(String[] args) { testInsert(); testQuery(); } public static void testInsert(){ Student s=new Student(); s.setId(12); s.setAge(23); s.setName("xxt"); JdbcUtil u=new JdbcUtil(); String sql="insert into T_Student(id,age,name) values( ?,?,?)"; int result=u.update(sql, s.getId(),s.getAge(),s.getName()); System.out.println(result); } public static void testQuery(){ String sql="select * from T_Student where age=?"; Student s=new Student(); s.setAge(23); JdbcUtil u=new JdbcUtil(); ResultSet rs=u.Query(sql, s.getAge()); try { while(rs.next()){ //s.setName(rs.getString("name")); System.out.println(rs.getString("name"));//s.getName(rs.getString("name")) } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 調用原生的方式:測試

 
Connection conn=util.getCon();
		String sql="select * from "+tableName+" where date=?";
		log.info("sql:"+sql);
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement(sql);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			stmt.setString(1, date);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

  須要注意的是,query方法若是不須要參數查所有的話,直接填nullui

ResultSet rs=util.Query(sqlStr,null);this