数据库驱动
程序会通过数据库驱动,与数据库交互
JDBC
为了简化开发人员的操作(对数据库的统一操作),提供了一个(java操作数据库)规范 jdbc
这些规范的实现 由具体的厂商实现
对于开发人员,只需要掌握JDBC接口的操作
java.sql
javax.sql
还需要解压一个数据库驱动包
jdbc实例
public class JdbcDemo {
public static void main(String[] args) throws SQLException {
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.创建连接
//useUnicode=true&characterEncoding=utf8 设置编码格式
String url="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8";
String user="root";
String password="123321";
Connection connection = (Connection) DriverManager.getConnection(url, user, password);
//3,创建Statement对象 用于执行sql语句
Statement statement = (Statement) connection.createStatement();
//4.执行sql语句 接收返回值
//executeUpdate 增删改都属于update方法 返回值为数据库中被影响数据的行数
//executeQuery查询 返回值是数据库中的数据集 保存在ResultSet对象中
int count = statement.executeUpdate("insert into `student`(`StudentNo`,`LoginPwd`,`StudentName`,`Sex`,`GradeID`,`Phone`,`Address`,`BornDate`,`Emile`,`IdentityCard`)\r\n" +
"values('1705100217','111','测试名',0,1,'111','彭山','2020-8-3','@qq.com','111222333')");
ResultSet resultSet = statement.executeQuery("select * from student");
//
// if(count>0) {
// System.out.println("修改成功");
// }else {
// System.out.println("修改失败");
// }
// while(resultSet.next()) {
// System.out.println(resultSet.getObject("StudentNo"));
// System.out.println(resultSet.getObject("StudentName"));
// System.out.println(resultSet.getObject("BornDate"));
// System.out.println(resultSet.getObject("IdentityCard"));
// }
//5释放连接
resultSet.close();
statement.close();
connection.close();
}
}
Statement对象
jdbc中的statement对象用于向数据库发送sql语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送对应的SQL语句即可
statemrnt对象主要的两个方法
- executeUpdate(String sql)
- 用于向数据库中发送增删改语句
- 返回类型 int 数据库中被影响的行数
- executeQuery(String sql)
SQL注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息
public class JdbcDemo {
public static void main(String[] args) throws SQLException {
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.创建连接
//useUnicode=true&characterEncoding=utf8 设置编码格式
String url="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8";
String user="root";
String password="123321";
Connection connection = (Connection) DriverManager.getConnection(url, user, password);
//3,创建Statement对象 用于执行sql语句
Statement statement = (Statement) connection.createStatement();
//4.执行sql语句 接收返回值
//select * from student where name='' or '1=1' and password='' or '1=1'
ResultSet resultSet = statement.executeQuery("select * from student where name='' or '1=1' and password='' or '1=1'");
//
// if(count>0) {
// System.out.println("修改成功");
// }else {
// System.out.println("修改失败");
// }
// while(resultSet.next()) {
// System.out.println(resultSet.getObject("StudentNo"));
// System.out.println(resultSet.getObject("StudentName"));
// System.out.println(resultSet.getObject("BornDate"));
// System.out.println(resultSet.getObject("IdentityCard"));
// }
//5释放连接
resultSet.close();
statement.close();
connection.close();
}
}
select * from student where name=’’ or ‘1=1’ and password=’’ or ‘1=1’
name=’’ or ‘1=1’ 返回true
password=’’ or ‘1=1’ 返回true
name 和password总是返回true 可以查询表中的所以信息
PrepareStatement(防止SQL注入)
防止SQL 注入的本质:将参入的参数当做字符,如果存在转义字符,会被直接转义;
**
public class JdbcDemo {
public static void main(String[] args) throws SQLException {
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.创建连接
//useUnicode=true&characterEncoding=utf8 设置编码格式
//useSSL=true"
String url="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8";
String user="root";
String password="123321";
Connection connection = (Connection) DriverManager.getConnection(url, user, password);
//4.执行sql语句 接收返回值
// ? 占位符
//prepareStatement(sql) 预编译sql 不执行
PreparedStatement statement =connection.prepareStatement("INSERT INTO `student`(`StudentNo`,`LoginPwd`,`StudentName`,`Sex`,`GradeID`,`Phone`,`Address`,`BornDate`,`Emile`,`IdentityCard`) \r\n" +
"VALUES(?,?,?,?,?,?,?,?,?,?)");
//对相应位置的占位符赋值 根据对应位置数据类型 使用setString() setint() setObject() 等方法
statement.setObject(1,"1705100217");
statement.setObject(2,"111");
statement.setObject(3,"测试");
statement.setObject(4,0);
statement.setObject(5,1);
statement.setObject(6,"111");
statement.setObject(7,"彭山");
statement.setObject(8,"2020-8-3");
statement.setObject(9,"@qq.com");
statement.setObject(10,"111222333");
int count = statement.executeUpdate();
System.out.println(count);
//5释放连接
statement.close();
connection.close();
}
}