博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JDBC_02 实现对数据表的增删改操作
阅读量:3967 次
发布时间:2019-05-24

本文共 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/

你可能感兴趣的文章
常见网络安全设备默认口令
查看>>
VirtualBox虚拟机网络配置
查看>>
oracle vm virtualbox虚拟机下,CentOS7系统网络配置
查看>>
解决Linux CentOS中cp -f 复制强制覆盖的命令无效的方法
查看>>
wdcpv3升级到v3.2后,多PHP版本共存的安装方法
查看>>
PHP统计当前网站的访问人数,访问信息,被多少次访问。
查看>>
Windows10远程报错CredSSP加密oracle修正
查看>>
Windows server 2016 设置多用户登陆
查看>>
偶然发现的面包屑
查看>>
CentOS 7 下挂载NTFS文件系统磁盘并设置开机自动挂载
查看>>
非插件实现Typecho语法高亮
查看>>
windows 下 netsh 实现 端口映射(端口转发)
查看>>
两个好用的命令行工具 watch 和 rsync
查看>>
信安入门神级书单
查看>>
【IPFS指南】IPFS的竞争对手们(一)
查看>>
docker更换国内镜像
查看>>
CentOS 下 tree命令用法详解
查看>>
docker上传镜像至Registry时https报错解决方法
查看>>
docker下删除none的images
查看>>
Linux提权获取敏感信息方法
查看>>