当前位置:网站首页>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
边栏推荐
- devops学习(四) Jenkins CI 持续集成
- 深度学习的随机种子
- Override and customize dependent native Bean methods
- Raspberry pie wiringPi 2.6 installed on solving gpio readall command mistakes
- USACO2008通信线路
- HRNet-Facial-Landmark-Detection 训练自己数据集
- C陷阱与缺陷 第4章 链接 4.1 什么是链接器
- 关于MySQL索引的一些个人理解(部分参考MySQL45讲)
- 决策树原理及代码实现
- devops学习(八) 搭建镜像仓库---jenkins推送镜像
猜你喜欢

Design for failure常见的12种设计思想

很遗憾,没有一篇文章能讲清楚分布式事务

The latest Gansu construction welder (construction special operation) simulation question bank and answer analysis in 2022

437. The total path III low low

DNA修饰碱基5-甲基胞嘧啶和8-羟基鸟嘌呤|DNA修饰量子点|规格信息

环形链表(LeetCode 141、142)

Huawei 14 Days - (3) Kernel Development

Apache Doris 1.1 特性揭秘:Flink 实时写入如何兼顾高吞吐和低延时

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
随机推荐
全国双非院校考研信息汇总整理 Part.8
[leetcode] 82. Delete duplicate elements in sorted linked list II (medium)
JVM初探- 内存分配、GC原理与垃圾收集器
devops学习(九) Helm工具--持续部署
Redis系列:高可用之Sentinel(哨兵模式)
JSON.parseObject 带泛型告警
暴力递归到动态规划 03 (背包问题)
[2023 School Recruitment Questions] Summary of knowledge points and hand-tear code in the written test and interview
Codeforces Round #805 (Div. 3)总结
C陷阱与缺陷 第4章 链接 4.2 声明与定义
2022/7/29 考试总结
jenkins use and maintenance
全国双非院校考研信息汇总整理 Part.7
C陷阱与缺陷 第4章 链接 4.3 命名冲突与static修饰符
标签分发协议(LDP)
Access Modbus TCP and Modbus RTU protocol devices using Neuron
Guidelines for the Release of New WeChat Mini Programs
[leetcode] 80. Delete duplicates in sorted array II (medium) (double pointer, in-place modification)
Gao Shu Xia|Triple Integral Exercises|Uncle Gao Shu|Handwritten Notes
y81.第四章 Prometheus大厂监控体系及实战 -- 监控扩展(十二)