JDBC练习_update、DDL语句和JDBC各个类详解_ResultSet_基本使用
JDBC练习_delete、DDL语句
/* account表 删除一条记录 */ private static void delete() { Connection conn = null; Statement stat = null; try { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取Connection对象 conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root"); //定义sql String sql = "delete from account where id = 3"; //获取执行sql的对象 Statement stat = conn.createStatement(); //执行sql int i = stat.executeUpdate(sql); //处理结果 System.out.println(i); if (i>0){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { if (stat!=null){ try { stat.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
/* 创建一张student表 */ private static void DDL() { Connection conn = null; Statement stat = null; try { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取Connection对象 conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root"); //定义sql String sql = "create table student (id int, name varchar(20))"; //获取执行sql的对象 Statement stat = conn.createStatement(); //执行sql int i = stat.executeUpdate(sql); //处理结果 System.out.println(i); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { if (stat!=null){ try { stat.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
JDBC各个类详解_ResultSet_基本使用
ResultSet:结果集对象,封装查询结果
next() :游标向下移动一行
getXxx(参数):获取数据
Xxx:代表数据类型 如:int getInt(),String getString()
参数:
1.int:代表列的编号,从1开始 如:getString(1)
2.String : 代表列名称。如:getDouble("balance")
private static void ResultSet() { Connection conn = null; Statement stat = null; ResultSet rs = null; try { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取Connection对象 conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root"); //定义sql String sql = "select * from account"; //获取执行sql的对象 Statement stat = conn.createStatement(); //执行sql rs = stat.executeQuery(sql); //处理结果 //让游标向上移动一行 rs.next(); //获取数据 int id = rs.getInt(1); String name = rs.getString("name"); double balance = rs.getDouble(3); System.out.println(id+"-"+name+"-"+balance); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { if (rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (stat!=null){ try { stat.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix