JDBC使用步骤
第0步: 导包
第1步:注册驱动 (仅仅做一次)
CLass.forName("com.mysql.jdbc.Driver");
第2步:建立连接(Connection)DriverManager 关键字
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/day10_20?useSSL=false 时区,取消SSL安全认证", "root", "root");
//第一个参数是url
jdbc:mysql://IP:端口/数据库
第二个参数是数据库用户名
第三个参数是数据库密码
第3步:创建运行SQL的语句对象(Statement)
//创建Sql 语句
String sql = "SELECT *FROM student ";
statement = connection.createStatement();
第4步:运行语句
//运行sql语句
resultSet = statement.executeQuery(sql);
// 增删改用 preparedStatement.executeUpdate
// 查询使用 preparedStatement.executeQuery
第5步:处理运行结果(ResultSet)
//处理运行结果
while (resultSet.next()) {
System.out.println(resultSet.getString("sname"));
}
第6步:释放资源
resultSet.close();
statement.close();
connection.close();
完整的jdbc
public static void main(String[] args) throws Exception {
Connection connection=null; // 因为try 不能关闭在finally 中获取关闭资源的关键字.所以提作用域
Statement statement=null;
ResultSet resultSet=null;
try {
// 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//创建 连接(connection)
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/day10_20?useSSL=false", "root", "root");
//创建Sql 语句
String sql = "SELECT *FROM student ";
statement = connection.createStatement();
//运行sql语句
resultSet = statement.executeQuery(sql);
//处理运行结果
while (resultSet.next()) {
System.out.println(resultSet.getString("sname"));
}
}catch (SQLException e) {
e.printStackTrace();
}
finally {
//关闭资源
if (resultSet != null) {
resultSet.close();
}
if (statement != null){
statement.close();
}
if (connection != null){
connection.close();
}
}
}
优化jdbc
public class jdbc_02 {
public static void main(String[] args)throws Exception{
m1("14"); //调用方法来优化
}
public static void m1(String id ) throws Exception {
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try {
// 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//创建 连接(connection)
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/day10_20?useSSL=false", "root", "root");
//创建Sql 语句
String sql = "SELECT *FROM student where sid=1";
statement = connection.createStatement();
//运行sql语句
resultSet = statement.executeQuery(sql);
//处理运行结果
while (resultSet.next()) {
System.out.println(resultSet.getString("sname"));
}
}catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null){
statement.close();
}
if (connection != null){
connection.close();
}
}catch (Exception e) {
e.getMessage();
}
}
PreparedStatement (主要作用就是为了防止sql 注入)
Statement 和 PreparedStatement 的区别
Statement用于执行静态SQL语句,在执行的时候,必须指定一个事先准备好的SQL语句,并且相对不安全,会有SQL注入的风险
PreparedStatement是预编译的SQL语句对象,sql语句被预编译并保存在对象中, 被封装的sql语句中可以使用动态包含的参数 ? ,
在执行的时候,可以为?传递参数
public static void m1(String id ) {
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
// 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//创建 连接(connection)
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/day10_20?useSSL=false", "root", "root");
//创建Sql 语句
String sql = "delete FROM student where sid=?";
preparedStatement = connection.prepareStatement(sql);
//运行sql语句
preparedStatement.setString(1,id);
int i = preparedStatement.executeUpdate();
System.out.println("影响"+i);
//处理运行结果
// while (resultSet.next()) {
//
//
// }
}catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
finally {
try {
if (resultSet != null) {
resultSet.close();
}
if ( preparedStatement != null){
preparedStatement.close();
}
if (connection != null){
connection.close();
}
}catch (Exception e) {
e.getMessage();
}
//关闭资源
将 jdbc 分步封装成方法
// 将 url 和账号密码穿进去
public static Connection getUrl(String url,String username,String password) {
// 注册驱动和创建连接封装的方法
try {
//创建 连接(connection)
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//关闭流的方法
public static void close(AutoCloseable a){ // 将流的父类传入
try {
if (a != null) {
a.close();
}
}catch (Exception e) {
e.getMessage();
}
}
掉用方法
public class jdbc_05 {
public static void main(String[] args) throws Exception {
Properties properties=new Properties(); 创建properties对象
//使用 properties .load 方法(随便的一个类调用Class方法,然后调用类加载器 ,将功能表转换成流) properties.load(jdbc_05.class.getClassLoader().getResourceAsStream("url.properties"));
String url1 = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
//调用 注册和创建连接的方法
Connection url = jdbc_04.getUrl(url1,username,password );
//写sql语句
String sql="SELECT *FROM student";
PreparedStatement preparedStatement = url.prepareStatement(sql);
// 运行sql 语句
ResultSet sid = preparedStatement.executeQuery();
//处理sql 语句
while (sid.next()) {
System.out.println(sid.getString("sname"));
}
//调用方法关闭流
jdbc_04.close(sid);
jdbc_04.close(preparedStatement );
jdbc_04.close(url);
}
}
事务
原子性
一致性
隔离性
持久性