JDBC增加、更新、删除数据
st.executeUpdate(sql) 进行插入、更新、删除操作
返回的是受影响的记录的条数
注意:输入的sql语句中,vachar类型记住加单引号
完整代码如下:
public class JDBCTest { //建立连接 public static Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); conn=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=FALSE&serverTimezone=UTC","root","xb199795"); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } //插入数据 public static void insert() { String sql="insert into tbl_user(name,password,email)"+ "values(‘xiongda‘,‘123‘,‘[email protected]‘)"; Connection conn =getConnection(); try { Statement st=conn.createStatement(); int count =st.executeUpdate(sql); System.out.println("插入了"+count+"条记录!"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //更新数据 public static void update() { String sql="update tbl_user set email=‘[email protected]‘ where name=‘xiongda‘"; Connection conn =getConnection(); try { Statement st=conn.createStatement(); int count =st.executeUpdate(sql); System.out.println("更新了"+count+"条记录!"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //删除数据 public static void delete() { String sql="delete from tbl_user where name=‘xiongda‘"; Connection conn =getConnection(); try { Statement st=conn.createStatement(); int count =st.executeUpdate(sql); System.out.println("删除了"+count+"条记录!"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
原文地址:https://www.cnblogs.com/xtuxiongda/p/9000444.html
时间: 2024-10-11 08:43:50