JDBC基本概念:
Java database connectivity java 数据库连接,Java语言操作数据库
jdbc的本质:其实是官方定义的一套操作所有关系型数据库的规则,各个厂商去实现这套规则,提供数据库驱动jar包,规则即接口,我们可以使用这套接口编程,真正执行的代码是jar包中的实现类,
jdbc快速入门
步骤:
- 导入驱动jar包
- 注册驱动
- 获取数据库连接对象
- 定义SQL
- 获取执行SQL语句的对象,statement
- 执行SQL,接受返回结果
- 处理结果
- 释放资源
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Jdbcdemo01 {
public static void main(String[] args) throws Exception {
//1.导入驱动Java包,右键libs as...
//2,注册驱动
Class.forName("com.mysql.jdbc.Driver");
//3,获取数据库连接对象
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/db2","root","root");
//4,定义sql语句
String sql="update account set salary=500 where id=1";
//5获取执行SQL的对象,statement
Statement stme=conn.createStatement();
//6.执行sql
int count=stme.executeUpdate(sql);
//7,处理结果
System.out.println(count);
//8,释放资源
stme.close();
conn.close();
SQL注入问题:在拼接SQL的时候,有一些SQL的特殊关键字参与字符串的拼接,会造成安全性的问题。
例如:输入用户随便,输入密码: a’or ‘a’=’a
sql;select * from user where username=’ssssfd’ and password=’a’ or ‘a’=’a’
解决SQL注入的问题:使用preparedStatement对象来解决
预编译的SQL:参数使用?作为占位符
例如:select * from user where username=? and password =?;
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
-----------------------
jdbc工具类抽取:
JDBCUtils 目的:简化书写
分析:
- 注册驱动也抽取
- 抽取一个方法获取连接对象
需求:不想传递参数(因为比较麻烦),还要保证工具的通用性
解决:配置文件
jdbc.properties
url=
user=
password=
- 抽取一个方法释放资源
- db.properties
-
url=jdbc:mysql://localhost:3306/db2
user=root
password=root
driver=com.mysql.jdbc.Driver
- import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCutils {
private static String url;
private static String user;
private static String password;
private static String driver;
//文件的读取,只需要读取一次即可拿到这些值,使用静态代码块
static{
try {
//读取资源文件,获取值
//1.创建properties集合类,加载进内存
Properties pro=new Properties();
//获取src路径下的文件的方式--》ClassLoader
ClassLoader classLoader=JDBCutils.class.getClassLoader();
URL res=classLoader.getResource("jdbc.properties");
String path=res.getPath();
System.out.println(path);
//2.加载文件
//pro.load(new FileReader("src/jdbc.properties"));
pro.load(new FileReader(path));
//3.获取数据,赋值
url=pro.getProperty("url");
user=pro.getProperty("user");
password=pro.getProperty("password");
driver=pro.getProperty("driver");
//4,注册驱动
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//jdbc工具类
//获取连接方法
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
//释放资源方法
public static void close(Statement stmt,Connection conn){
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs, Statement stmt, Connection conn){
if(stmt!=null){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
----------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class jdbcdemo2 {
public static void main(String[] args) throws Exception {
//1.导入驱动Java包,右键libs as...
//2,注册驱动
//Class.forName("com.mysql.jdbc.Driver");
//3,获取数据库连接对象
// Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/db2","root","root");
Connection conn=JDBCutils.getConnection();
//4,定义sql语句
String sql="update account set salary=5000 where id=1";
//5获取执行SQL的对象,statement
Statement stme=conn.createStatement();
//6.执行sql
int count=stme.executeUpdate(sql);
//7,处理结果
System.out.println(count);
//8,释放资源
stme.close();
conn.close();
-------------------------------------
public class jdbcdemo4 {
public static void main(String[] args) {
List<emp> list=new jdbcdemo4().findall();
System.out.println(list);
System.out.println(list.size());
}
public List<emp> findall(){
Statement stmt = null;
Connection conn = null;
ResultSet rs = null;
List<emp> list=null;
try {
// Class.forName("com.mysql.jdbc.Driver");
// conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
conn=jdbcutils01.getConnection();
String sql = "select * from emp";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
emp emp=null;
list=new ArrayList<emp>();
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String gender = rs.getString("gender");
int salary = rs.getInt("salary");
Date date = rs.getDate("join_date");
int dapt = rs.getInt("dapt_id");
emp =new emp();
emp.setId(id);
emp.setName(name);
emp.setGender(gender);
emp.setSalary(salary);
emp.setJoin_date(date);
emp.setDapt_id(dapt);
list.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
jdbcutils01.closs(rs,stmt,conn);
}
return list;
}
}
----------------------