增删改都可以通过一个方法来完成,查需要一个方法,因为查需要返回一个结果集,而其他的操作并不需要。
封装的工具类:
/**
*
*/
package com.sweet.tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author Administrator
*
*/
public class DBUtil {
// 驱动
private final static String DRIVER_CLASS = "com.mysql.jdbc.Driver";
// 账号密码
private final static String URL = "jdbc:mysql://127.0.0.1:3306/mytest?characterEncoding=utf8";
private final static String USERNAME = "root";
private final static String PASSWORD = "root";
// 属性对象
private static Connection connection;
private static PreparedStatement preparedStatement;
private static ResultSet set;
static {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 获得连接
public static Connection getConnection() throws SQLException {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
return connection;
}
// 释放
public static void close() throws SQLException {
if (set != null) {
set.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
}
// CRUD
// 更改emp表中年龄为100员工的姓名为 李四
// update(sql,Object[] objects);
// 增加删除修改可以用一个
public static int updateAndInsertAndDelete(String sql,Object[] objects) throws SQLException {
//1 获得连接
connection = getConnection();
// 2 准备sql
preparedStatement = connection.prepareStatement(sql);
// 3 填充sql中的?参数
for (int i = 0; i < objects.length; i++) {
preparedStatement.setObject(i+1, objects[i]);
}
// 4 执行sql 被返回记录数
return preparedStatement.executeUpdate();
}
public static ResultSet query(String sql,Object[] objects) throws SQLException {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
preparedStatement.setObject(i + 1, objects[i]);
}
set = preparedStatement.executeQuery();
return set;
}
}
测试类:
/**
*
*/
package com.sweet.jdbcDemo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.zhiyou.dao.IEmpDao;
import com.zhiyou.dao.impl.EmpDaoImpl;
import com.zhiyou.model.EmpObj;
import com.zhiyou.tools.DBUtil;
/**
* @author Administrator
*
*/
public class Test {
/**
* @param args
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
DBUtil.getConnection();
String sql1 = "select * from emp where e_age=?";
Object[] objects1 = {20};
ResultSet set = DBUtil.query(sql1, objects1);
while (set.next()) {
System.out.println(set.getString("e_name"));
}
//更改
String sql = "update emp set e_name=? where e_age=?";
Object[] objects = {"laosun12",20};
int increateValue = DBUtil.updateAndInsertAndDelete(sql, objects);
//插入
String sql2 = "insert into emp(e_name,e_age) values(?,?)";
Object[] objects2 = {"sweet",20};
int increateValue = DBUtil.updateAndInsertAndDelete(sql2, objects2);
//删除
String sql3 = "delete from emp where e_age=?";
Object[] objects3 = {20};
int increateValue = DBUtil.updateAndInsertAndDelete(sql3, objects3);
System.out.println(increateValue);
DBUtil.close();
}
}