本文共 4862 字,大约阅读时间需要 16 分钟。
本人是个新手,写下博客用于自我复习、自我总结。 如有错误之处,请各位大佬指出。 学习资料来源于:尚硅谷
每个人的数据库的名字以及表可能不相同,可以对以下中出现的数据进行相应的修改
package com.guigu.preparedstatement.crud;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.Date;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Statement;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Properties;import org.junit.Test;import com.guigu.connection.ConnectionTest;import com.guigu.util.JDBCUtils;/** * 使用PreparedStatement来替换Statement,实现对数据表的增删改查操作。 * * insert/delete from/update/select * */public class PreparedStatementUpdateTest { // 向student表中添加一条记录(表名因人而异) @Test public void testInsert() { Connection conn = null; PreparedStatement ps = null; try { // 1、读取配置文件中的基本信息 InputStream is = ConnectionTest.class.getClassLoader() .getResourceAsStream("jdbc.properties"); Properties pros = new Properties(); pros.load(is); String user = pros.getProperty("user"); String url = pros.getProperty("url"); String password = pros.getProperty("password"); // 2、加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 3、获取链接 conn = DriverManager.getConnection(url, user, password); // 4、预编译sql语句,返回PreparedStatement的实例 // ?:占位符 String sql = "insert into student(stuname,stuscore,stunum,stubirth)values(?,?,?,?)"; ps = conn.prepareStatement(sql); // 5、填充占位符 ps.setString(1, "蔡徐坤"); ps.setDouble(2, 80); ps.setInt(3, 1007); // 设置日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date date = sdf.parse("2000-11-11"); ps.setDate(4, new Date(date.getTime())); // 6、执行操作 ps.execute(); } catch (Exception e) { e.printStackTrace(); } finally { // 7、资源关闭 try { if (ps != null) ps.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } // 修改student表中的一条记录 @Test public void testUpdate() { Connection conn = null; PreparedStatement ps = null; try { // 1、获取数据库的连接 conn = JDBCUtils.getConnection(); // 2、预编译sql语句,返回PreparedStatement的实例 String sql = "update student set stuname = ? where stuid = ?"; ps = conn.prepareStatement(sql); // 3、填充占位符 ps.setObject(1, "蔡徐坤坤"); ps.setObject(2, 7); // 4、执行 ps.execute(); } catch (Exception e) { e.printStackTrace(); } finally { // 5、资源关闭 JDBCUtils.closeResource(conn, ps); } } }
testInsert()这种实现方法太过繁琐,可以把获取数据库连接的操作放在util包中,统一管理:
(类似testUpdate()这种实现方法)package com.guigu.util;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;/** * 操作数据库的工具类 * */public class JDBCUtils { /** * 获取数据库的连接 * */ public static Connection getConnection() throws Exception{ // 1、读取配置文件中的基本信息 InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties"); Properties pros = new Properties(); pros.load(is); String user = pros.getProperty("user"); String url = pros.getProperty("url"); String password = pros.getProperty("password"); // 2、加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 3、获取链接 Connection conn = DriverManager.getConnection(url, user, password); return conn; } //关闭资源的操作 public static void closeResource(Connection conn,Statement ps){ try { if(ps !=null) ps.close(); } catch (SQLException e) { e.printStackTrace(); } try { if(conn !=null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } }}
除此以外还可以试着来写一个通用的增删改操作:
package com.guigu.preparedstatement.crud;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.Date;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Statement;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Properties;import org.junit.Test;import com.guigu.connection.ConnectionTest;import com.guigu.util.JDBCUtils;public class PreparedStatementUpdateTest { // 通用的增删改操作 // sql中占位符的个数与可变形参的个数相同 public void update(String sql, Object... args) { Connection conn = null; PreparedStatement ps = null; try { // 1、获取数据库连接 conn = JDBCUtils.getConnection(); // 2、预编译sql语句,返回PreparedStatement实例 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); } } @Test public void testCommonUpdate(){ String sql = "delete from student where stuid = ?"; update(sql,7); }}
转载地址:http://kayki.baihongyu.com/