public class MyTest { public static void main(String[] args) { Connection connection = null; Statement statement = null; try { //1.加载驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //2.获得连接 String username = "root"; String Pwd = "123456"; String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC"; connection = DriverManager.getConnection(url,username,Pwd); //3 定义sql 创建状态通道(进行sql语句的发送) statement = connection.createStatement(); //4 返回受影响的行数 int result = statement.executeUpdate("insert into student(stuId,stuname,sex,age) values (213,'李梅','女',23)"); if (result > 0 ){ System.out.println("执行成功!"); }else{ System.out.println("执行失败!"); } } catch (SQLException | ClassNotFoundException throwables) { throwables.printStackTrace(); }finally { try { if (statement != null){ statement.close(); } if(connection != null){ connection.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
JAVA-JDBC操作数据库之增删改
最新推荐文章于 2024-07-13 13:15:21 发布