news 2026/6/10 13:15:37

SDUT Java---jdbc

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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(); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 10:30:31

LXMusic V4版:智能音乐发现与个性化下载体验完全指南

你是否曾经为了找到一首心仪的冷门歌曲而翻遍各大音乐平台?或者因为下载音乐过程繁琐而放弃收藏?今天,让我们一起来探索LXMusic音乐下载器V4版如何彻底改变你的音乐获取方式。 【免费下载链接】LXMusic音源 lxmusic(洛雪音乐&…

作者头像 李华
网站建设 2026/6/10 11:49:01

ros2常用命令

1、ros2安装 wget http://fishros.com/install -O fishros && . fishros2、小海龟:测试一下 ros2 run turtlesim turtlesim_noderos2 run turtlesim turtle_teleop_key3、colcon安装 sudo apt install python3-colcon-common-extensionscolcon --helppip3 s…

作者头像 李华
网站建设 2026/6/10 11:52:43

TranslucentTB安装失败终极解决方案:7步彻底修复透明任务栏问题

TranslucentTB安装失败终极解决方案:7步彻底修复透明任务栏问题 【免费下载链接】TranslucentTB 项目地址: https://gitcode.com/gh_mirrors/tra/TranslucentTB 你是否曾经对Windows单调的任务栏感到厌倦?想要体验透明、模糊的现代化效果&#x…

作者头像 李华
网站建设 2026/6/10 11:54:49

昇腾AI全栈技术深度解析:从异构计算到应用开发实战

历经13年异构计算研发,我深刻体会到:“真正的技术深度不在于知道多少API,而在于能否从晶体管的行为推演出系统级性能瓶颈”。本文将带你穿透华为昇腾AI全栈技术的层层抽象,直抵达芬奇架构的物理本质,掌握从芯片指令到A…

作者头像 李华
网站建设 2026/6/10 2:59:57

TaskFlow:Java开发者必备的DAG任务编排终极指南

TaskFlow:Java开发者必备的DAG任务编排终极指南 【免费下载链接】taskflow taskflow是一款轻量、简单易用、可灵活扩展的通用任务编排框架,基于有向无环图(DAG)的方式实现,框架提供了组件复用、同步/异步编排、条件判断、分支选择等能力&…

作者头像 李华
网站建设 2026/6/10 11:04:38

decimal.js终极指南:彻底解决JavaScript精度问题的5个关键步骤

JavaScript开发者在处理数值计算时,经常会遇到一个令人头疼的问题:精度丢失。当你尝试计算0.1 0.2时,得到的结果是0.30000000000000004,而不是预期的0.3。这种精度问题在财务计算、科学计算等场景中可能导致严重错误。 【免费下载…

作者头像 李华