package cn.bruce.MySql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class Prepared { public static void main(String[] args) { String name, password, url, sql, jdbc; Connection con = null; name = "root"; password = "jinqi1986"; url = "jdbc:mysql://localhost:3306/mytrain?useSSL=false&serverTimezone=CTT"; jdbc = "com.mysql.cj.jdbc.Driver"; try { Class.forName(jdbc); System.out.println("驱动加载成功"); } catch (Exception e) { e.printStackTrace(); } try { con = DriverManager.getConnection(url, name, password); System.out.println("连接成功"); } catch (Exception e) { e.printStackTrace(); } try { String n = "小金11"; String p = "12345622"; sql = "update users set zusename = ?, zpassword = ? where id = ?;"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, n); ps.setString(2, p); ps.setInt(3, 2); System.out.println(ps.executeUpdate() + "行进行修改"); } catch (Exception e) { e.printStackTrace(); } try { String n = "小金22"; String p = "334455"; String n1 = "小金33"; String p1 = "22334455"; sql = "insert into users (zusename,zpassword) values (?,?),(?,?);"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, n); ps.setString(2, p); ps.setString(3, n1); ps.setString(4, p1); System.out.println(ps.executeUpdate() + "行进行修改"); } catch (Exception e) { e.printStackTrace(); } try { sql = "select * from users;"; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println( rs.getString("id") + " " + rs.getString("zusename") + " " + rs.getString("zpassword")); } ps.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch (Exception e2) { e2.printStackTrace(); } } } }