JDBC——增删改查及开启事务操作
作者:互联网
1.在JDBC中进行对mysql的添加数据
public void test() throws SQLException {
//获取对象
Connection connection = JDBCutil.getConnection();
//写sql
String sql="insert into studnet values(?,?)";
//预编译
PreparedStatement ps = connection.prepareStatement(sql);
//写入数据
ps.setInt(1,03);
ps.setString(2,"张三");
int i = ps.executeUpdate();//运行sql并返回有几行数据收到影响
System.out.println(i);
//关闭资源
JDBCutil.close(ps,connection);
}
2.在JDBC中进行对mysql的修改/更新数据
@Test
//修改操作
public void test02() throws SQLException {
//获取对线
Connection connection = JDBCutil.getConnection();
//写sql语句
String s="update studnet set name =? where id=?";
//写占位符的信息
PreparedStatement ps= connection.prepareStatement(s);
ps.setString(1,"ca");
ps.setInt(2,1);
//执行语句
int i = ps.executeUpdate();
System.out.println(i+"条语句受到影响");
//关闭资源
JDBCutil.close(ps,connection);
}
3.在JDBC中进行对mysql的删除数据
@Test
//删除操作
public void test03() throws SQLException {
//获取对象
Connection connection = JDBCutil.getConnection();
//写sql
String s="delete from studnet ";
//占位符
PreparedStatement ps= connection.prepareStatement(s);
// ps.setInt(1,2);
int i = ps.executeUpdate();
System.out.println(i+"条语句受到影响");
//关闭资源
JDBCutil.close(ps,connection);
}
注意: JDBC中查询语句的方法与之前三者不同,需要调用executeQuery方法
4.在JDBC中查询一条数据
@Test
//查询操作,查询一条数据
public void test() throws SQLException {
//获取对象
Connection connection = JDBCutil.getConnection();
//写sql
String s="select * from studnet where id =?";
//占位符
PreparedStatement ps= connection.prepareStatement(s);
ps.setInt(1,1);
//执行sql语句,executeQuery方法
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()){
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
System.out.println(id+" "+name);
}
//关闭资源
JDBCutil.close(ps,connection,resultSet);
}
5.在JDBC中查询多条数据,此时我们需要自定义一个javaBean类,来进行存储查询语句所返回的值,并使用ArrayList集合接收
创建student Bean类
package net.cyan.cy01;
public class Student {
private int id;
private String name;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return id+" "+name;
}
}
之后进行查询
@Test
//查询多条数据需要创建一个bean类,来适用对象存储查询到的结果
//先创建student类
public void test04(){
//获取对象
Connection connection = JDBCutil.getConnection();
//写sql
String s="select * from studnet";
//预编译
PreparedStatement ps=null;
//创建集合进行接受
List<Student> list=new ArrayList<>();
ResultSet rs=null;
try {
ps = connection.prepareStatement(s);
rs = ps.executeQuery();
while (rs.next()){
int id=rs.getInt(1);
String name=rs.getString(2);
list.add(new Student(id,name));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//关闭资源
JDBCutil.close(ps,connection,rs);
}
//遍历
for (Student s1 : list) {
System.out.println(s1);
}
}
6.JDBC中开启事务的操作
场景:比如银行转账,从A账户扣去1000元打到B账户,此时如果当中出现了异常很有可能导致从A账户扣除了钱但没有到B账户此类的问题,所以为了解决这类问题我们使用事务将两步操作合并为一条,任何一条执行错误就回滚数据。
package net.cyan.cy02;
import net.cyan.cy01.JDBCutil;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBCDemoSw {
@Test
public void test(){
Connection connection = JDBCutil.getConnection();
//写sql语句
String s="update money set money=? where name=?";
PreparedStatement ps=null;
//写入信息
try {
//添加事务开始
connection.setAutoCommit(false);
ps= connection.prepareStatement(s);
//从zs账户减除余额
ps.setInt(1,0);
ps.setString(2,"zs");
ps.executeUpdate();
// System.out.println(1/0);
//ls账户加入钱
ps.setInt(1,2000);
ps.setString(2,"ls");
ps.executeUpdate();
//流程没有异常则提交事务
connection.commit();
} catch (Exception e) {
e.printStackTrace();//打印异常信息
//当事务出现了异常,则回滚数据
try {
connection.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}finally {
//关闭事务,并修改自动提交选项
try {
connection.setAutoCommit(true);
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
//关闭资源
JDBCutil.close(ps,connection);
}
}
}
标签:ps,JDBC,name,改查,id,connection,sql,增删,public 来源: https://www.cnblogs.com/CYan521/p/16469807.html