前言
上次写了idea连接jdbc,并执行了查询方法,这次实现增删改方法
说点什么才能让你们点这个链接转到上一个文章
一、添加实现代码
上次是直接写在main函数中的,这次改变一下,直接上代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* 类名:Test1
* 读书破万卷,下笔如有神
* 代码反行之,算法记于心
* 作者:劫恋李
* 日期:2021/6/14 12:40
* 版本:V1.0
*/
public class Test1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Test1 test1 = new Test1();
test1.add();
}
public void add() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("1.完成注册");
//建立连接,
String url="jdbc:mysql://localhost:3306/ljl";
//主要协议:次要协议://主机名:端口号/数据库名
Connection connection = DriverManager.getConnection(url, "root", "root");
System.out.println("2.完成连接");
String sql="insert into users values(null,'乙方','123',20,'男')";
PreparedStatement preparedStatement=connection.prepareStatement(sql);
System.out.println("3.创建成功");
//注意和查询用的不同!
preparedStatement.executeUpdate();
System.out.println("4.执行sql语句成功");
preparedStatement.close();
connection.close();
System.out.println("5.关闭连接");
}
}
这里有一点注意:因为涉及到对数据库的修改,所以要用executeUpdate();函数
这点和查询不同!
二、 添加代码优化
不知道你们发现这样写有问题吗,反正添加进去了,我不想添加不变的数据,我想自己指定数据(那你想填啥写啥不就行了)。。添加方法我是用来调用的,我不能每次都上方法里面修改数据!太麻烦,且不能一次添加多条数据!我们只需要修改一个地方就行了!
1.添加优化部分
// String sql="insert into users values(null,'乙方','123',20,'男')";
String sql="insert into users values(null,?,?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,uname);
preparedStatement.setString(2,upass);
preparedStatement.setInt(3,uage);
preparedStatement.setString(4,usex);
一个 ? 代表一个 位置,表中有几个数据就有几个问号,我用户id是自增的,所以设置了空
2.完整优化代码
代码如下(示例):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* 类名:Test1
* 读书破万卷,下笔如有神
* 代码反行之,算法记于心
* 作者:劫恋李
* 日期:2021/6/14 12:40
* 版本:V1.0
*/
public class Test1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Test1 test1 = new Test1();
test1.add("丙方","1234","女",34);
test1.add("乙方","123","男",23);
}
public void add(String uname,String upass,String usex,int uage) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("1.完成注册");
//建立连接,
String url="jdbc:mysql://localhost:3306/ljl";
//主要协议:次要协议://主机名:端口号/数据库名
Connection connection = DriverManager.getConnection(url, "root", "root");
System.out.println("2.完成连接");
// String sql="insert into users values(null,'乙方','123',20,'男')";
String sql="insert into users values(null,?,?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,uname);
preparedStatement.setString(2,upass);
preparedStatement.setInt(3,uage);
preparedStatement.setString(4,usex);
System.out.println("3.创建成功");
preparedStatement.executeUpdate();
System.out.println("4.执行sql语句成功");
preparedStatement.close();
connection.close();
System.out.println("5.关闭连接");
}
//修改删除代码跟在这个后面,都在Test1 里面,别把代码复制别的地方去。
}
运行结果
二、 修改删除
public void update(String uname,String upass,String usex,int uage,int uid) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("1.用户注册成功");
//通过驱动管理器获取数据库链接对象
// 2.建立连接
String url="jdbc:mysql://localhost:3306/ljl";
Connection connection = DriverManager.getConnection(url, "root", "root");
System.out.println("2.数据库链接成功");
//3.通过连接对象创建预编译陈述对象
String sql="update users set uname=?,upassword=?,usex=?,uage=? where uid=?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,uname);
preparedStatement.setString(2,upass);
preparedStatement.setInt(3,uage);
preparedStatement.setString(4, usex);
preparedStatement.setInt(5,uid);
System.out.println("3.创建预编译陈述对象成功");
//4通过陈述对象执行sql语句
preparedStatement.executeUpdate();
System.out.println("4.执行sql语句成功");
//关闭数据库资源
preparedStatement.close();
connection.close();
System.out.println("5.关闭数据库资源成功!");
}
//删除
public void delete(int uid) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("1.用户注册成功");
//通过驱动管理器获取数据库链接对象
// 2.建立连接
String url="jdbc:mysql://localhost:3306/ljl";
Connection connection = DriverManager.getConnection(url, "root", "root");
System.out.println("2.数据库链接成功");
//3.通过连接对象创建预编译陈述对象
String sql="delete from users where uid=?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,uid);
System.out.println("3.创建预编译陈述对象成功");
//4通过陈述对象执行sql语句
preparedStatement.executeUpdate();
System.out.println("4.执行sql语句成功");
//关闭数据库资源
preparedStatement.close();
connection.close();
System.out.println("5.关闭数据库资源成功!");
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Test1 test1 = new Test1();
// test1.add("丙方","1234","女",34);
// test1.add("乙方","123","男",23);
// 调用方法
test1.update("123","123","女",21,5);
test1.delete(1);
}
不运行了,就sql语句不一样,没啥大问题,相信你们能学会!。
你们可以尝试将查询语句写成方法形式,在main函数中调用!链接在上方!
总结
本次主要对数据库进行修改,注意的是调用的是:preparedStatement中的executeUpdate方法,就这里和查询调用的不同
其他的就是基本的sql语句了。