完成MySQL数据库的删除,增加和修改,确实不会,求问111111111
6条回答 默认 最新
- yy64ll826 2023-04-14 10:56关注
可以借鉴下
package com.example.helloworld.Utils; import java.sql.*; public class JDBCUtils { public static Connection getConn(){ try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } Connection conn = null; //mysql用户名 String userName = "root"; //mysql密码 String password = "password"; //数据库URL String url = "jdbc:mysql://localhost:3306/<数据库名>?useSSL=false"; try { conn = DriverManager.getConnection(url, userName, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } //查询 public static String GetData(String table,String column,String name,String col){ Connection conn = getConn(); String a = null; Statement stmt; try { stmt = conn.createStatement(); String sql; sql = "SELECT * FROM "+table + " WHERE " + column + "='" + name+"'"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { a=rs.getString(col); } rs.close(); stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return a; } public static String pGetData(String table, String column, String name, String col) { Connection conn = getConn(); String a = null; PreparedStatement ps = null; try { //创建prepareStatement对象 String sql = "SELECT * FROM "+table+" WHERE "+column+" = ?"; ps = conn.prepareStatement(sql); ps.setString(1, name); ps.execute();//执行 System.out.println(ps.execute());//执行表输出返回的结果,结果为false,因为没有返回的结果集 //处理结果集 ResultSet rs=ps.executeQuery(); while(rs.next()){ a=rs.getString(col); } } catch (Exception e) { e.printStackTrace(); } finally { //关闭资源 try { if (ps != null) ps.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return a; } //更新 public static void UpdateData(String table, String column, String name, String col , String newdata) { Connection conn = getConn(); Statement stmt; try { stmt = conn.createStatement(); String sql; sql = "UPDATE "+ table + " SET " + col + " = '"+newdata + "' WHERE " + column + " = '"+name+"'"; stmt.executeUpdate(sql); stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void pUpdateData(String table, String column, String name, String col , String newdata) { Connection conn = getConn(); PreparedStatement ps = null; try { //创建prepareStatement对象 String sql = "UPDATE "+table+" SET "+col+" = ? WHERE "+column+" = ?"; ps = conn.prepareStatement(sql); //执行sql语句 //ps.setString(1, table); //ps.setString(2, col); ps.setString(1, newdata); //ps.setString(4, column); ps.setString(2, name); //java.util.Date utilDate = new java.util.Date();//进行类型转换,由util类型的date转化为sql类型的 ps.execute();//执行 System.out.println(ps.execute());//执行表输出返回的结果,结果为false,因为没有返回的结果集 //处理结果集 } catch (Exception e) { e.printStackTrace(); } finally { //关闭资源 try { if (ps != null) ps.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } //删除 public static void DeleteData(String table,String col,String name){ Connection conn = getConn(); Statement stmt; try { stmt = conn.createStatement(); String sql; sql = "DELETE FROM "+table + " WHERE " + col + "='" + name+"'"; stmt.executeUpdate(sql); stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void pDeleteData(String table,String col,String name){ Connection conn = getConn(); PreparedStatement ps = null; try { //创建prepareStatement对象 String sql = "DELETE FROM "+table + " WHERE " + col + "= ?"; ps = conn.prepareStatement(sql); //执行sql语句 ps.setString(1, name); //java.util.Date utilDate = new java.util.Date();//进行类型转换,由util类型的date转化为sql类型的 ps.execute();//执行 System.out.println(ps.execute());//执行表输出返回的结果,结果为false,因为没有返回的结果集 //处理结果集 } catch (Exception e) { e.printStackTrace(); } finally { //关闭资源 try { if (ps != null) ps.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } //添加 public static void AddDatastu(String name1,String password1,String qqaccount1){ Connection conn = getConn(); Statement stmt; try { stmt = conn.createStatement(); String sql; sql = "insert into stu(stuname,password,qq) values ('"+name1+"','"+password1+"','"+qqaccount1+"');"; stmt.executeUpdate(sql); stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void pAddDatastu(String name1,String password1,String qqaccount1){ Connection conn = getConn(); PreparedStatement ps = null; try { //创建prepareStatement对象 String sql = "insert into stu(stuname,password,qq) values (?,?,?)"; ps = conn.prepareStatement(sql); //执行sql语句 ps.setString(1, name1); ps.setString(2,password1); ps.setString(3,qqaccount1); //java.util.Date utilDate = new java.util.Date();//进行类型转换,由util类型的date转化为sql类型的 //ps.execute(); // 执行 System.out.println(ps.execute());//执行表输出返回的结果,结果为false,因为没有返回的结果集 //处理结果集 } catch (Exception e) { e.printStackTrace(); } finally { //关闭资源 try { if (ps != null) ps.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1
悬赏问题
- ¥25 LT码在高斯信道下的误码率仿真
- ¥45 渲染完成之后将物体的材质贴图改变,自动化进行这个操作
- ¥15 yolov5目标检测并显示目标出现的时间或视频帧
- ¥15 电视版的优酷可以设置电影连续播放吗?
- ¥50 复现论文;matlab代码编写
- ¥30 echarts 3d地图怎么实现一进来页面散点数据和卡片一起轮播
- ¥15 数字图像的降噪滤波增强
- ¥15 心碎了,为啥我的神经网络训练的时候第二个批次反向传播会报错呀,第一个批次都没有问题
- ¥15 MSR2680-XS路由器频繁卡顿问题
- ¥15 VB6可以成功读取的文件,用C#读不了