JDBC:
Java Database Connectivity是一个独立于特定数据库管理系统,通用的SQL数据库存取和操作的公共接口。
- 面向应用的API,供开发人员调用。
- 面向数据库的API,供数据库开发厂商开发对应的数据库驱动
使用 JDBC 原理
1、加载数据库驱动,Java Application 和 数据库的桥梁。
2、获取 Connection,一次连接。
3、通过 Connection 对象产生 Statement,执行 SQL 语句。
4、ResultSet 保存 Statment 执行后所产生的结果。
package com.chenny.entity;
import java.sql.*;
public class Test {
public static void main(String[] args) {
//MySql连接
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
String username = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url,username,password);
//定义SQL语句
String sql = "insert into tb_user (name, age, money) VALUES ('haha',123,123124);";
//执行SQL
Statement statement = connection.createStatement();
int row = statement.executeUpdate(sql);
//修改数据
// String sql = "update user set name = '李四',age = 23,money = 1000 where id = 10";
// Statement statement = connection.createStatement();
// int row = statement.executeUpdate(sql);
// if(row == 1){
// System.out.println("修改成功!");
// }
// //删除数据
// String sql = "delete from user where id = 10";
// Statement statement = connection.createStatement();
// int row = statement.executeUpdate(sql);
// if(row == 1){
// System.out.println("删除成功!");
// }
// //查询
// String sql = "select * from user where id = 11";
// Statement statement = connection.createStatement();
// ResultSet resultSet = statement.executeQuery(sql);
// while(resultSet.next()){
// int id = resultSet.getInt(1);
// String name = resultSet.getString(2);
// int age = resultSet.getInt(3);
// int money = resultSet.getInt(4);
// System.out.println(id+"-"+name+"-"+age+"-"+money);
// id = resultSet.getInt("id");
// name = resultSet.getString("name");
// age = resultSet.getInt("age");
// money = resultSet.getInt("money");
// System.out.println(id+"-"+name+"-"+age+"-"+money);
// }
// String sql = "select * from user where id = 11";
// String sql = "insert into user(name,age,money) values('王五',25,1000)";
// String sql = "update user set name = '小明' where id = 13";
String sql = "delete from user where id = 13";
statement = connection.createStatement();
boolean flag = statement.execute(sql);
System.out.println(flag);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
} finally {
try {
connection.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//连接SQLserver数据库
// try {
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// String url = "jdbc:sqlserver://localhost:1433;database=demo";
// String user = "sa";
// String password = "123456";
// Connection connection = DriverManager.getConnection(url,user,password);
// String sql = "insert into tb_user (name,age,money) values('Danny',22,32223);";
// Statement statement = connection.createStatement();
// int row = statement.executeUpdate(sql);
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// } catch (SQLException e){
// e.printStackTrace();
// }
}
}
实际开发中一般不会使用Statment来执行SQL语句,而是选择其子类 PreparedStatment 来执行SQL。
-
PreparedStatment 提供了SQL占位符的功能,定义SQL语句时可以先使用?来标识要被替换的参数,而不需要去拼接字符串。
-
PreparedStatment 首先可以解决频繁拼接SQL字符串的问题。
-
PreparedStatment 还可以避免SQL注入的风险。
- SQL注入:利用某些系统没有对用户输入的数据进行充分检查,在用户输入的数据中注入非法的SQL语句,从而利用系统漏洞完成恶意行为的做法。
-
PreparedStatement 执行效率更高。