JDBC--API--Statement

发布时间 2023-04-21 17:30:40作者: 为TT
  1. import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class jdbcdemo_Statement {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://127.0.0.1:3306/homework?useSSL=false";
    String user="root";
    String password="1234";
    Connection conn = DriverManager.getConnection(url, user, password);
    String sql="update lyj set credit=888 where id=3";
    Statement statement = conn.createStatement();
    conn.setAutoCommit(false);
    try {

    int i = statement.executeUpdate(sql);
    if (i>0){
    System.out.println("执行成功");
    }else {
    System.out.println("执行失败");

    } } catch (SQLException throwables) {
    conn.rollback();
    throwables.printStackTrace();
    }
    conn.commit();

    conn.close();
    statement.close();


    }
    }