public static void main(String[] args) { Connection connection = null; Statement statement =null; try { Class.forName("com.mysql.cj.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?userSSL=false&serverTimezone=Asia/Shanghai","root","root"); statement = connection.createStatement(); String sql = "insert into student(name,age) values ('李四',123145)"; //executeUpdate用于添加,修改,删除语句 int rs = statement.executeUpdate(sql); if (rs>0) System.out.println("添加成功"); else System.out.println("添加失败"); } catch (Exception e){ e.printStackTrace(); } finally { //6.关闭连接 try { if (statement!=null){ statement.close(); } if (connection!=null){ connection.close(); } }catch (Exception e){ e.printStackTrace(); } } }
JDBC的添加
最新推荐文章于 2024-10-08 13:19:19 发布