1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.sympa.lesson01;
import javax.swing.plaf.nimbus.State; import java.sql.*;
public class JdbcFirstDemo { public static void main(String[] args) throws ClassNotFoundException, SQLException { String url = "jdbc:mysql://127.0.0.1:3306/jdbcstudy?serverTimezone=UTC&useSSL=false&useServerPrepStmts=true"; String username = "root"; String password = ""; Connection connection = DriverManager.getConnection(url, username, password);
String sql = "select * from users"; String sql1 = "update users set password = ? where id = ?"; PreparedStatement pstmt = connection.prepareStatement(sql); PreparedStatement pstmt1 = connection.prepareStatement(sql1); pstmt1.setInt(1, 23333); pstmt1.setInt(2, 1);
ResultSet resultSet = null; try { connection.setAutoCommit(false); int resultSet1 = pstmt1.executeUpdate(); resultSet = pstmt.executeQuery(); while(resultSet.next()){ System.out.println("id=" + resultSet.getObject("id")); System.out.println("name=" + resultSet.getObject("name")); System.out.println("password=" + resultSet.getObject("password")); System.out.println("email=" + resultSet.getObject("email")); System.out.println("birthday=" + resultSet.getObject("birthday")); } connection.commit(); } catch (Exception e) { connection.rollback(); e.printStackTrace(); } resultSet.close(); pstmt.close(); pstmt1.close(); connection.close(); } }
|