com.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();
}
}
}
}
mysql ----BaseDao工具类
标签:connect void rest stat ret nal cal catch tco
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:https://www.cnblogs.com/gun-a/p/10535342.html