jdbc工具类封装
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbc工具类封装相关的知识,希望对你有一定的参考价值。
封装
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); while (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(); } } } } } }
model
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; } }
测试
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 { if(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(); } } }
以上是关于jdbc工具类封装的主要内容,如果未能解决你的问题,请参考以下文章
Java 中封装JDBC连接到JDBCUtils工具类的详解