JDBC练习_update语句
需求:修改account表中的数据:
package CN.XueQiang.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * 修改accont表中的数据 */ public class JDBCDemo3 { public static void main(String[] args) { Statement statement = null; Connection conn = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取数据库创建连接对象 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/win", "root", "njzyb555"); //3.定义sql String sql = "update accont set balance =1500 where id=1;"; //4.获取执行sql对象 statement = conn.createStatement(); //5.执行sql int i = statement.executeUpdate(sql); System.out.println(i); if (i > 0) { System.out.println("修改成功"); } else { System.out.println("修改失败"); } } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (SQLException e) { throw new RuntimeException(e); } finally { //7.释放资源 if (statement != null) ; try { statement.close(); } catch (SQLException e) { throw new RuntimeException(e); } if (conn !=null){ try { conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } } }
修改结果
修改之后的accont
JDBC练习_update、DDL语句
需求:删除account表中的数据:
package CN.XueQiang.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** *删除accont表中的数据; */ public class JDBCDem04 { public static void main(String[] args) { Connection conn = null; Statement stat = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取数据库创建连接对象 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/win", "root", "njzyb555"); //3.定义sql String sql = "delete from accont where id = 3"; //4.获取执行sql对象 stat = conn.createStatement(); //5.执行sql int i = stat.executeUpdate(sql); System.out.println(i); if (i>0){ System.out.println("删除成功!"); }else{ System.out.println("删除失败"); } } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (SQLException e) { throw new RuntimeException(e); }finally { //7.释放资源 if (stat !=null) { try { stat.close(); } catch (SQLException e) { throw new RuntimeException(e); } if (conn !=null) { try { conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } } } }
删除前
删除后
标签:语句,JDBC,java,update,SQLException,sql,null,throw,conn From: https://www.cnblogs.com/x3449/p/16817956.html