import java.sql.*; public class Test { static final String driver="com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8"; static final String USER = "root"; static final String PASS = "root"; static final String QUERY = "UPDATE sc SET sc.`name` = \"Tom\" WHERE sc.id=2"; public static void main(String[] args) { Connection conn = null; try { Class.forName(driver); conn= DriverManager.getConnection(DB_URL, USER, PASS); conn.setAutoCommit(false); Statement stmt = conn.createStatement(); int i = stmt.executeUpdate(QUERY); conn.commit(); if (i>0){ System.out.println("success"); conn.rollback(); } } catch (SQLException e) { try { conn.rollback(); } catch (SQLException ex) { ex.printStackTrace(); } e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
复习JDBC连接数据库
最新推荐文章于 2023-01-11 13:44:37 发布