当前位置:网站首页>Add, delete, modify and query the database
Add, delete, modify and query the database
2022-07-29 23:51:00 【like the rising sun】
After we have connected to the database earlier,It is necessary to perform further operations on the database,The simplest is to add, delete, modify and check operations
1.查
public void QueeyAll() throws Exception{
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
String sql = "select * from product";
result = stmt.executeQuery(sql);
while(result.next()){
int proid = result.getInt("proid");
String proname = result.getString("proname");
String proflag = result.getString("proflag");
double procost = result.getDouble("procost");
System.out.print(proid + "\t");
System.out.print(proname + "\t");
System.out.print(proflag + "\t");
System.out.print(procost + "\n");
}
result.close();
stmt.close();
conn.close();
}
We return data from the database by defining a result set.
2.增
public int InsertPro(int proid,String proname,double procost,String proflag) throws Exception{
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
String sql = "insert into product (proid,proname,procost,proflag) values(null,?,?,?)";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1,proname);
preparedStatement.setDouble(2,procost);
preparedStatement.setString(3,proflag);
int i = preparedStatement.executeUpdate();
System.out.println(i);
return i;
}
Determine whether the insertion is successful by defining the number of affected rows
3.删
public int DeleteByID(int proID) throws Exception{
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
String sql = "delete from product where proid = ?";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1,proID);
//Output the number of affected rows to determine whether the deletion is successful
int i = preparedStatement.executeUpdate();
System.out.println(i);
preparedStatement.close();
conn.close();
return i;
}
Deleting is the same as adding,It also determines whether the deletion is successful by defining the number of affected rows
4.改
public int UpdatePro(int proid,String proname,double procost,String proflag) throws Exception{
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
String sql = "update product set proname = ? , procost = ? ,proflag = ? where proid = ?";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, proname);
preparedStatement.setDouble(2, procost);
preparedStatement.setString(3, proflag);
preparedStatement.setInt(4, proid);
int i = preparedStatement.executeUpdate();
System.out.println(i);
return i;
5.小结
It should be noted that the order of parameter indices should correspond one-to-one
边栏推荐
猜你喜欢
437. The total path III low low
HRNet-Facial-Landmark-Detection 训练自己数据集
MySQL【基本select语句】
JetsonNano learning (5) JetsonNano installs PyTorch and Torchvision
新标杆!美创科技助力广西桂林某三甲医院实现勒索病毒主动防御
树莓派上安装 wiringPi 2.6 解决 gpio readall 命令的错误
Design for failure常见的12种设计思想
Windows 安装 MySQL 5.7详细步骤
容器化 | 在 Rancher 中部署 MySQL 集群
Gao Shu Xia|Triple Integral Exercises|Uncle Gao Shu|Handwritten Notes
随机推荐
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史
【openlayers】Map【1】
【openlayers】地图【一】
全网最强 JVM 来袭!(至尊典藏版)
彻底搞懂kubernetes调度框架与插件
y81. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Monitoring Extension (12)
r‘w‘r‘w‘r‘w‘r
一文解答web性能优化
单片机开发之拓展并行I/O口
C语言初阶-初识C语言
高数下|三重积分的计算3|高数叔|手写笔记
线上无序的
Codeforces Round #245 (Div. 1) A (dfs)
仿牛客论坛项目部署总结
USACO2008通信线路
什么是色选机(color sorter)?
rk-boot框架实战(1)
环形链表(LeetCode 141、142)
树莓派上安装 wiringPi 2.6 解决 gpio readall 命令的错误
[2023 School Recruitment Questions] Summary of Common Interview Questions (7. Common Bus Protocols) (Continuously updated with subsequent interviews....)