赞
踩
packagecom.zjw.dao;import java.sql.*;/*** 工具类*/
public classBaseDao {static final String DB_URL = "jdbc:mysql://localhost:3306/数据库";static final String username = "用户名";static final String paw = "密码";protectedConnection conn;protectedResultSet rs;protectedPreparedStatement ps;/*** 数据库的连接
*
*@return
*/
public voidconnect() {try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(DB_URL, username, paw);
}catch(SQLException e) {
e.printStackTrace();
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}/*** 增删改*/
public intexecuteUpdate(String sql, Object... obj) {int row = 0;try{
connect();
ps=conn.prepareStatement(sql);if (obj != null) {for (int i = 0; i < obj.length; i++) {
ps.setObject(i+ 1, obj[i]);
}
row=ps.executeUpdate();
}
close(conn,null, rs);
}catch(SQLException e) {
e.printStackTrace();
}returnrow;
}/*** 查询数据
*
*@paramsql
*@paramobj
*@return
*/
public voidexecuteQuery(String sql, Object... obj) {try{
connect();
ps=conn.prepareStatement(sql);if (obj != null) {for (int i = 0; i < obj.length; i++) {
ps.setObject(i+ 1, obj[i]);
}
}
rs=ps.executeQuery();
}catch(SQLException e) {
e.printStackTrace();
}
}/*** 关闭流
*
*@paramconn
*@paramps
*@paramrs*/
public voidclose(Connection conn, PreparedStatement ps, ResultSet rs) {if (conn != null) {try{
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if (ps != null) {try{
ps.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if (rs != null) {try{
rs.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。