数据驱动包:mysql-connector-java
基本使用:
//1.加载驱动
//该类中存在静态代码块用来注册Driver,加载类模板时,static代码块被执行
Class.forName("com.mysql.jdbc.Driver");
//2.用户信息和url
String url = "jdbc:mysql://localhost:3306/database_name?useUnicode = true&characterEncoding = utf8&useSSL = true";
String usename ="root";
string passname = "123456";
//3.连接成功,数据库对象Connection代表数据库
Connection conn = DriverManager.getConnection(url,username,password);
//4.执行SQL语句对象Statement,用于向数据库发送sql语句
Statement statement = connection.createStatement();
String sql = "sql语句";
//5.执行sql语句,可能存在返回结果
/*executeQuery()用于查询数据,返回结果集,executeUpdate()用于增删改数据,返回被改变的总行数(int类型)*/
ResultSet resultSet =statement.executeQuery(sql);
while(resultSet.next)
{
System.out.println(reultSet.getObject("列名");
}
//6.关闭连接
resultSet.close();
statement.close();
conn.close();
提取工具类
/** jdbc工具类*/
public class JdbcUtils{
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
static{
try{
//将用户信息及url放入properties配置文件中,从配置文件db.properties中获取配置数据
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties prop = new Properties;
prop.load(in);
driver = prop.getProperty("driver");
url = prop.getProperty("url");
username = prop.getProperty("username");
password = prop.getProperty("password");
Class.forName(driver);
}catch(Exception e){
e.printStackTrace();
}
}
//获取数据库连接对象
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,username,password);
}
//关闭连接
public static void release(Connetion conn,Statement st,ResultSet rs){
if(rs!=null){
try{
re.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(st!=null){
try{
st.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
使用工具类
SQL注入:
web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。
//userName和password从前端传递进来,当传入的userNamehe password是"'or '1=1'"时,SQL注入
String sql = "select * from users where `NAME`='" +userName+ "'and `password`='" +password+ "'";
/*sql注入:select * from users where `NAME`=''or '1=1' and `password`='' or '1=1';
前端传入恶意数据导致where条件变成了永真,会输出表中所有数据,导致信息泄露
PreparedStatement对象
PreparedStatement可以防止SQL注入,并且效率更高。
Statement 和 PreparedStatement之间的关系和区别.
关系:PreparedStatement继承自Statement,都是接口
区别:PreparedStatement可以使用占位符?,是预编译的,批处理比Statement效率高 SQL 语句被预编译并存储在 PreparedStatement 对象中。然后可以使用此对象多次高效地执行该语句。
Connection conn =JdbcUtils.getConnection();
String sql="insert into lover values(?,?,?)";
ps=conn.prepareStatement(sql);
//代表设置给第一个?号位置的值为Int类型的21
ps.setInt(1,21);
//代表设置给第二个?号位置的值为String类型的suwu150
ps.setString(2,"suwu150");
//进行类型转换,由util类型的date转化为sql类型的
java.util.Date utilDate=new java.util.Date();
ps.setDate(3, new java.sql.Date(utilDate.getTime()));
//执行sql语句
ps.executeUpdate();
//解决SQL注入问题,preparedStatement会把传入的参数当做字符,类似单引号'字符会被直接转义
String sql = "select * from usrs where `NAME`=?and`password`=?";
ps=conn.prepareStatement(sql);
//userName和password参数由前端输入,
ps.setString(1,userName);
ps.setString(2,password);
JDBC操作事务
try{
conn=JdbcUtils.getConnection();
//关闭数据库的自动提交,自动开启事务
conn.setAutoConnection(false);//开启事务
/**
*操作SQL语句
*/
conn.commit();//提交事务
}catch(SQLException e){
//失败默认回滚,无须显式写明
// try{
// conn.rollback();//事务执行失败则回滚
// }catch(SQLException e1){
// e1.printStackTrace();
// }
e.printStackTrace();
}finally{
JdbcUtils.release();//释放连接
}
数据库连接池
编写连接池需实现java.sql.DataSource接口。
设置:最小连接数;最大连接数;等待超时
开源数据源实现:DBCP,C3P0,Druid(Alibaba)
连接池参考:https://www.cnblogs.com/xdp-gacl/p/4002804.html