1. 注册驱动
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
2. 建立连接
//DriverManager.getConnection("jdbc:mysql://localhost/数据库名?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC", "登录名", "密码");
3. 创建statement
//创建statement,跟数据库打交道,一定需要这个对象
statement = connection.createStatement();
4. 执行sql,得到resultSet
//执行查询,得到结果集
String sql = "select * from t_stu";
resultSet = statement.executeQuery(sql);
5. 遍历结果集
//遍历查询每一条记录
if(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("String");
String password = resultSet.getString("password");
System.out.println("id = "+id+"\n"+"username = "+username+"\n"+"password = "+password+"\n");
}
数据库的CRUD sql
*insert
insert into t_stu (name, age) values (‘wangpangzi’, 24)
insert into t_stu values(null, ‘wangpang2’, 25)
//获取数据库对象
connection = JDBCUtil.getConn();
//根据连接对象,得到statement
statement = connection.createStatement();
//执行添加的sql语句
String sql = "insert into t_stu values(null, 'aobama', 60)";
//executeUpdate影响的行数,如果大于0表明操作成功。否则失败
int result = statement.executeUpdate(sql);
if(result > 0 ){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
*delete
delete from t_stu where id = 7
//获取数据库对象
connection = JDBCUtil.getConn();
//根据连接对象,得到statement
statement = connection.createStatement();
//执行sql语句
String sql = "delete from t_stu where id = 11";
//executeUpdate影响的行数,如果大于0表明操作成功。否则失败
int result = statement.executeUpdate(sql);
if(result > 0 ){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.release(statement, connection);
}
*query
select * from t_stu
//注册驱动
connection = JDBCUtil.getConn();
//创建statement,跟数据库打交道,一定需要这个对象
statement = connection.createStatement();
//执行查询,得到结果集
String sql = "select * from t_stu";
resultSet = statement.executeQuery(sql);
//遍历每一条记录
while(resultSet.next()){
int id = resultSet.getByte("id");
String name = resultSet.getString("name");
int age = resultSet.getByte("age");
System.out.println("id = "+id+" name = "+name+" age = "+age);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtil.release(resultSet, statement, connection);
}
*update
update t_stu set age = 38 where id = 8
try {
//获取数据库对象
connection = JDBCUtil.getConn();
//根据连接对象,得到statement
statement = connection.createStatement();
//执行sql语句
String sql = "update t_stu set age = 23 where name = 'wangwu'";
//executeUpdate影响的行数,如果大于0表明操作成功。否则失败
int result = statement.executeUpdate(sql);
if(result > 0 ){
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.release(statement, connection);
}
使用单元测试,测试代码
- 定义一个类, TestXXX,里面定义方法 testXXX
- 添加junit的支持
添加 junit-4.12.jar和 harmcrest-core-1.3.jar - 在方法的上面添加@test注解,其实就是一个标记
@Test
public void testQuery(){
…
} - 光标选中方法名字,然后右键执行单元测试。 或者Alt + 7打开Structure视图,选中方法右键执行
DAO模式
Data Access Object 数据库访问对象
-
新建一个dao的接口,里面声明数据库访问规则
-
新建一个dao的实现类,具体实现早前定义的规则
public class UserDaoImpl implements UserDao { @Override public void findAll(){ Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { //获取连接对象 connection = JDBCUtil.getConn(); //创建statement对象 statement = connection.createStatement(); String sql = "select * from t_user "; resultSet = statement.executeQuery(sql); while (resultSet.next()){ String username = resultSet.getString("username"); String password = resultSet.getString("password"); System.out.println("username: "+username+"\n"+"password:"+password); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.release(resultSet, statement, connection); } } }
-
直接使用实现
@Test public void testFindAll(){ //面向接口编程,父类的引用,(父类引用指向子类对象)接口类的引用指向实现类的对象 UserDao dao = new UserDaoImpl(); dao.findAll(); }
Statement安全问题
-
Statement执行,其实是拼接sql语句。顺序:先拼接sql语句,然后再一起执行。
String sql = "select * from t_user where username = '"+ username +"' and password = '"+ password +"'"; UserDao dao = new UserDaoImpl(); dao.login("admin", "10080gnbxf' or '1=1"); select * from t_user where username = 'admin' and password = '10080gnbxf‘ or '1=1' 前面先拼接Sql语句,如果变量里面带有了 数据库的关键字,那么一并认为是关键字。不认为是普通的字符串。 resultSet = statement.executeQuery(sql);
PrepareStatement
该对象就是替换前面的statement对象
-
相比较之前的statement,预先处理给定的sql语句,对其进行语法检查。在sql语句里面使用“?”占位符来替代后续要传递进来的变量。后面进来的变量值,将会被看成是字符串,不会产生任何的关键字。
String sql = "insert into t_user values (null, ?, ?)"; preparedStatement = connection.prepareStatement(sql); //给占位符“?”赋值 preparedStatement.setString(1, username); preparedStatement.setString(2, password);
总结
-
JDBC入门
-
抽取工具类 ###
-
Statenment CRUD ###
演练crud
-
Dao模式 ###
声明与实现分开
-
PrepareStatement CRUD ###
预处理sql语句,解决上面statement出现的问题