章节目录
向数据库中插入数据
@Test
public void testInsert(){
Connection conn = null;
PreparedStatement ps = null;
try {
//1. 加载配置文件
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties info = new Properties();
info.load(is);
//2. 获取配置信息
String url = info.getProperty("url");
String user = info.getProperty("user");
String password = info.getProperty("password");
String className = info.getProperty("className");
//3. 加载驱动
Class.forName(className);
//4. 获取连接
conn = DriverManager.getConnection(url, user, password);
//5. 预编译sql语句,返回PreparedStatement实例
String sql = "insert into customers(name,email,birth) values(?,?,?)";
ps = conn.prepareStatement(sql);
//6. 填充占位符
ps.setString(1, "碎月有婷");
ps.setString(2, "ting@qq.com");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = sdf.parse("2000-12-15");
ps.setDate(3, new Date(date.getTime()));
//7. 执行
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
//8. 资源关闭
try {
if(ps != null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
以上在获取连接时采用的是数据与代码相分离的方式,因此需要在当前工程的src下创建
jdbc.properties
文件
修改数据库中某一条数据
从上述代码可以看出,所有的增删改都少不了获取连接和资源关闭,因此我们新建一个util包,将该代码写在util包中的JDBCUtils类中,如下:
public class JDBCUtils {
//用于获取连接,即一个Connection实例的方法
public static Connection getConnection() throws Exception{
//1. 加载配置文件
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties info = new Properties();
info.load(is);
//2. 获取配置信息
String url = info.getProperty("url");
String user = info.getProperty("user");
String password = info.getProperty("password");
String className = info.getProperty("className");
//3. 加载驱动
Class.forName(className);
//4. 获取连接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
//用于资源关闭的方法
public static void closeResource(Statement ps, Connection conn){
try {
if(ps != null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
此时修改数据就会简化很多,下面便是真正的修改操作需要的代码体现
@Test
public void testUpdate(){
Connection conn = null;
PreparedStatement ps = null;
try {
//1. 获取连接
conn = JDBCUtils.getConnection();
//2. 预编译sql语句
String sql = "update customers set name = ? where id = ?";
ps = conn.prepareStatement(sql);
//3. 填充占位符, 把 id=2 位置的 name 改为“碎月有婷”
ps.setString(1, "碎月有婷");
ps.setInt(2, 2);//前面的2表示占位符的索引为2,后面的是id=2
//4. 执行
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
//5. 资源关闭
JDBCUtils1.closeResource(ps, conn);
}
删除数据库中某一条记录
与修改数据类似,我们依然采用调用方法进行删除数据,如下:
@Test
public void testDelete(){
//1. 获取连接
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
//2. 预编译sql语句,获取PreparedStatement实例
String sql = "delete from customers where name = ?";
ps = conn.prepareStatement(sql);
//3. 填充占位符
ps.setString(1, "碎月有婷");
//4. 执行
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
//5. 资源关闭
JDBCUtils.closeResource(conn, ps);
}
}
通用的增删改模板
从上面三个例子可以看出,增删改操作基本类似,只有sql语句有差别,因此,我们可以封装一个方法,可以实现三者的功能
public void update(String sql, Object ...args){
//1. 获取连接
Connection conn = null;
//2. 预编译
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
//3. 填充占位符
for(int i = 0; i < args.length; i++){
ps.setObject(i + 1, args[i]);
}
//4. 执行
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
//5. 资源关闭
JDBCUtils.closeResource(conn, ps);
}
}
此时实现增删改就格外简单了,如下:
1. 插入数据:
@Test
public void test1(){
String sql = "insert into customers(name,email,birth) values(?,?,?)";
update(sql,"碎月有婷","ting@qq.com",new Date(1999-1900,12-1,15+1));
}
2. 修改数据
@Test
public void test2(){
String sql = "update customers set name = ? where id = ?";
update(sql,"张三","5");
}
3. 删除数据
@Test
public void test3(){
String sql = "delete from customers where id = ?";
update(sql,"6");
}