当前位置: 首页 > news >正文

SDUT Java---jdbc

8-1 sdut-JDBC-1 实现数据库表的CRUD操作

import java.sql.*; public class Main { public static void main(String[] args) throws ClassNotFoundException, SQLException { Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/school?useunicode=true&characterEncoding=utf-8", "root", "123456"); //数据库服务器名称(地址)、端口号、数据库名称、用户名、密码须根据实际情况改变 Statement st = con.createStatement(); //向表中增加记录并显示所有记录(数据自己指定); String insertSQL1 = "insert into student values(null,'嘿嘿',86);"; String insertSQL2 = "insert into student values(null,'哈哈',99);"; String insertSQL3 = "insert into student values(null,'呼呼',88);"; st.executeUpdate(insertSQL1); st.executeUpdate(insertSQL2); st.executeUpdate(insertSQL3); System.out.println("--1.增加-------"); String selectall = "Select * from student"; ResultSet rs = st.executeQuery(selectall); while(rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); double score = rs.getDouble(3); System.out.println(id + " " + name + " " + score); } //从表中删除id=1的记录,并显示所有记录; String deletSQL = "delete from student where id=1"; st.executeUpdate(deletSQL); System.out.println("--2.删除---"); rs = st.executeQuery(selectall); while(rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); double score = rs.getDouble(3); System.out.println(id + " " + name + " " + score); } //修改表中记录:查询条件id=2,将name修改为:山东理工,修改完毕显示所有记录; System.out.println("\n--3.修改---"); String updateSQL = "update student set name='山东理工' where id=2"; st.executeUpdate(updateSQL); rs = st.executeQuery(selectall); while(rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); double score = rs.getDouble(3); System.out.println(id + " " + name + " " + score); } //查询表中id=3的记录并显示。 System.out.println("\n-4.条件查询-----"); String selectSQL = "select * from student where id=3"; rs = st.executeQuery(selectSQL); if(rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); double score = rs.getDouble("score"); System.out.println(id + " " + name + " " + score); } } }

8-2 sdut-JDBC-2 实现数据库表的CRUD操作_中级(PreparedStatement)

import java.sql.*; public class Main { public static void main(String[] args) throws SQLException { Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/school?useunicode=true&characterEncoding=utf-8", "root", "123456"); //数据库服务器名称(地址)、端口号、数据库名称、用户名、密码须根据实际情况改变 PreparedStatement pst = null; //向表中增加记录(id列自增,可只考虑姓名和成绩),并显示所有记录; String insertSQL = "insert into student values(null, ?, ?)"; pst = con.prepareStatement(insertSQL); pst.setString(1, "ANNa"); pst.setDouble(2, 86.5); pst.executeUpdate(); System.out.println("--1.插入------"); String selectall = "Select * from student"; ResultSet rs = pst.executeQuery(selectall); while(rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); double score = rs.getDouble(3); System.out.println(id + " " + name + " " + score); } //从表中删除id=? 的记录,并显示所有记录; String deleteSQL = "delete from student where id=?"; pst = con.prepareStatement(deleteSQL); pst.setInt(1, 1); pst.executeUpdate(); System.out.println("\n--2.删除-----"); rs = pst.executeQuery(selectall); while(rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); double score = rs.getDouble(3); System.out.println(id + " " + name + " " + score); } //修改表中记录:查询条件id=?,将name修改为:?,修改完毕显示所有记录; String updateSQL = "update student set name=? where id=?"; pst = con.prepareStatement(updateSQL); pst.setString(1, "山东理工"); pst.setInt(2, 2); pst.executeUpdate(); System.out.println("\n--3.修改-----"); rs = pst.executeQuery(selectall); while(rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); double score = rs.getDouble(3); System.out.println(id + " " + name + " " + score); } //查询表中id=? 的记录并显示。 String selectSQL = "select * from student where id=?"; pst = con.prepareStatement(selectSQL); pst.setInt(1, 3); rs = pst.executeQuery(); System.out.println("\n--4.查询-----"); if(rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); double score = rs.getDouble("score"); System.out.println(id + " " + name + " " + score); } rs.close(); pst.close(); con.close(); } }

8-3 sdut-JDBC-3 实现数据库表的CRUD操作_中级(事务)

import java.sql.*; public class Main { public static void main(String[] args) throws SQLException { Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/school?useunicode=true&characterEncoding=utf-8", "root", "123456"); //数据库服务器名称(地址)、端口号、数据库名称、用户名、密码须根据实际情况改变 con.setAutoCommit(false); PreparedStatement pst = null; //向表中增加1条记录,id列自增,可只考虑姓名和成绩列的数据, String insertSQL = "insert into student values(null, ?, ?)"; pst = con.prepareStatement(insertSQL); pst.setString(1, "CHRISE"); pst.setDouble(2, 86.5); int result1 = pst.executeUpdate(); //从表中删除id=? 的记录; String deleteSQL = "delete from student where id=?"; pst = con.prepareStatement(deleteSQL); pst.setInt(1, 2); int result2 = pst.executeUpdate(); int result = result1*result2; System.out.println(result == 1 ? "增加记录和删除记录成功" : "增加记录和删除记录失败"); if(result == 1) { con.commit(); } else { con.rollback(); } // 显示所有 System.out.println("\n---表中记录为--"); String selectall = "select * from student"; ResultSet rs = pst.executeQuery(selectall); while(rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); double score = rs.getDouble(3); System.out.println(id + " " + name + " " + score); } rs.close(); pst.close(); con.close(); } }
http://www.jsqmd.com/news/89760/

相关文章:

  • openMES开源制造执行系统:从零部署到生产智能化的完整指南
  • 第55天(简单题中等题 数据结构)
  • C#开发者必知的100个黑科技(前50)!从主构造函数到源生成器全面掌握
  • Unity反向遮罩终极指南:打造惊艳UI特效的5个秘诀
  • 飞书文档批量导出工具完整使用指南
  • nchu_两次电路模拟大作业及课堂测验总结
  • 行业聚焦:2025年四通球阀制造厂家权威排名TOP10,市场上四通球阀公司推荐排行优选实力品牌 - 品牌推荐师
  • Source Han Serif TTF:开源中文字体的完美解决方案
  • KeymouseGo深度解析:解放双手的智能自动化实践手册
  • ros2常用命令
  • AMD处理器性能调优进阶实战:深度掌握Ryzen SDT调试工具
  • 固定球阀优质供应商排行,采购必看指南,固定球阀排行双达阀门发展迅速,实力雄厚 - 品牌推荐师
  • qy_蓝桥杯编程系列_编程22 不停的上课
  • GPU显存健康诊断:memtest_vulkan全面评测与实战指南
  • GPU显存健康诊断:memtest_vulkan全面评测与实战指南
  • 如何用OneMore插件实现终极笔记管理:开源免费的效率神器
  • 无锡地铁广告投放费用排行,高口碑供应商盘点,地铁站广告/电梯框架广告/应援广告/社区广告/电梯广告/电梯电子屏广告地铁广告采购有哪些 - 品牌推荐师
  • 题目集4~5以及课堂测验的总结Blog
  • ArkLights明日方舟终极自动化助手:一站式解放双手的完整解决方案
  • 5步掌握Python多尺度地理加权回归实战:从数据准备到结果解读
  • 联想拯救者工具箱:硬件优化与性能控制的终极解决方案
  • OpenWrt路由解锁网易云音乐全攻略:从零部署到高阶配置
  • 2025最新车牌识别/道闸/门禁/通道闸/停车场/人脸门禁公司首选骏通智能——智慧出入管理解决方案优选供应商 - 全局中转站
  • Easy-Scraper:零代码网页数据采集终极解决方案
  • iOS调试兼容性终极解决方案:全版本DeviceSupport文件使用指南
  • PiKVM硬件选型指南:从入门到专业部署的完整方案
  • 从 PRM 到 G-E:推荐重排架构的范式升级与工业实践
  • 终极指南:5步快速搭建纯净Galgame社区TouchGAL
  • 从 PRM 到 G-E:推荐重排架构的范式升级与工业实践
  • PKHeX自动化修改插件终极指南:5步打造完美合法宝可梦