package erchou;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Demo {
public static void main(String[] args) {
Connection connection = null;
ResultSet result = null;
PreparedStatement statement = null;
try{
connection = JdbcUtil.getConnection();
String sql = "INSERT userr(userName, password)VALUES(?,?);";
statement = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
statement.setString(1,"lucy");
statement.setString(2, "666");
int count = statement.executeUpdate();
System.out.println("一共影响了"+ count+"行");
result = statement.getGeneratedKeys();
if(result.next()){
System.out.println(result.getInt(1));
}
}catch(Exception e){
throw new RuntimeException();
}finally{
JdbcUtil.close(connection, statement, result);
}
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Demo {
public static void main(String[] args) {
Connection connection = null;
ResultSet result = null;
PreparedStatement statement = null;
try{
connection = JdbcUtil.getConnection();
String sql = "INSERT userr(userName, password)VALUES(?,?);";
statement = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
statement.setString(1,"lucy");
statement.setString(2, "666");
int count = statement.executeUpdate();
System.out.println("一共影响了"+ count+"行");
result = statement.getGeneratedKeys();
if(result.next()){
System.out.println(result.getInt(1));
}
}catch(Exception e){
throw new RuntimeException();
}finally{
JdbcUtil.close(connection, statement, result);
}
}
}