JDBC: (java database Connection) java数据库连接。
java面向对象的语言
sql结构化查询语言
这两种语言不能直接沟通 出来一个翻译Mysql: 每一个数据库公司提供的方法名可能不一致。导致了java必须记住每一个数据库的方法名。
java语言出来规范,让这些数据库公司实现这个规范。
JDBC链接的步骤:
1. 把jar包放入到项目lib下并add Libaray
2. 加载驱动 Class.forName("com.mysql.cj.jdbc.Driver");
3. 获取链接对象 Connection connection=DriverManager.getConnection(url,user,password);
4. 获取执行sql语句的对象: Statement statement=connection.createStatement();
5. 执行sql语句 int row=statement.executeUpdate(sql); 或 ResultSet resultSet=statement.executeQuery(sql); 遍历结果集
6. 关闭资源(后用先关 先用后关)
package com.hpj.jdbc1; import java.sql.*; public class Test7 { public static void main(String[] args){ Connection connection=null; Statement statement=null; ResultSet resultSet=null; try { Class.forName("com.mysql.cj.jdbc.Driver"); //加载驱动
//获取java与数据库之间的一个通道 //url : 数据库的请求路径 jdbc:mysql://ip:port/数据库名?serverTimezone=时区 //user : 数据库账号 //password : 数据库密码 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/10_16?serverTimezone=Asia/Shanghai", "root", "123@qwe"); //获取链接对象 statement = connection.createStatement(); //获取执行sql语句的对象//执行sql语句executeUpdate 执行增删改的sql //executeQuery 执行查讯的sql String sql = "select * from score"; resultSet = statement.executeQuery(sql); while (resultSet.next()){ System.out.println(resultSet.getString("stuNo")); } } catch (Exception e){ e.printStackTrace(); }finally { try { if (resultSet != null) { resultSet.close(); } if(statement!=null){ statement.close(); } if(connection!=null){ connection.close(); } }catch (Exception e){ e.printStackTrace(); } } } }
1. 如何防止sql注入。
什么是sql注入。----->sql拼接安全问题。
Statement存在sql注入的问题:因为他的sql字符串拼接。
解决方案使用PrepareStatement来进行sql得预编译。
sql可以使用占位符。
2. 正式开发时得模式。
Dao模式。---java的一个实体类对应数据库的一张表。 实体类中的属性对应数据库中字段 实体类对象对应数据库的记录。
DAO类对数据库表进行相应的CRUD(增删改查)
3. 抽取到BaseDao中
抽取一个工具类。
(1)在src根目录下创建一个db.properties
driverName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/10_20?serverTimezone=Asia/Shanghai username=root password=123@qwe
(2) 读取属性文件中的内容
//数据库连接信息
public static String driverName; //驱动名称
public static String url;//数据库路径
public static String username;
public static String password;
//读取数据库连接信息和加载驱动
static{
InputStream resourceAsStream =BaseDao.class.getResourceAsStream("/属性文件的路径");
Properties properties=new Properties();
properties.load(resourceAsStream);
driverName=properties.get("driverName"); //driverName要和属性文件的key对应
url=properties.get("url"); //url要和属性文件的key对应
username=properties.get("username"); //username要和属性文件的key对应
password=properties.get("password"); //password要和属性文件的key对应
Class.forName(driverName);
}
(3)BaseDao 增删改抽取
//增删改的公共方法
public int update(String sql,Object... params){
try {
getConnection();
ps = connection.prepareStatement(sql);
//为占位符赋值
for (int index=0;index<params.length;index++){
ps.setObject(index+1,params[index]);
}
int i = ps.executeUpdate();
return i;
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return 0;
}
(4) BaseDao全部代码块
//抽取BaseDao父类 dao继承BaseDao
public class BaseDao{
//数据库的对象
public Connection connection;
public PrepareStatement ps;
public ResultSet resultSet;
//数据库连接信息
public static String driverName; //驱动名称
public static String url;//数据库路径
public static String username;
public static String password;
//读取数据库连接信息和加载驱动
static{
InputStream resourceAsStream =BaseDao.class.getResourceAsStream("/属性文件的路径");
Properties properties=new Properties();
properties.load(resourceAsStream);
driverName=properties.get("driverName"); //driverName要和属性文件的key对应
url=properties.get("url"); //url要和属性文件的key对应
username=properties.get("username"); //username要和属性文件的key对应
password=properties.get("password"); //password要和属性文件的key对应
Class.forName(driverName);
}
//获取连接对象
public void getConn(){try{
connection=DriverManager.getConnection(url,username,password);}catch (Exception e){
e.printStackTrace();
}//增删改得通用方法
public int update(String sql,Object... params){ //params:占位符参数的值
try{
getConn();
ps=connection.prepareStatement(sql);
//为占位符赋值
for(int i=0;i<params.length;i++){
ps.setObject(i+1,params[i]);
}
//执行sql
int row=ps.executeUpdate();
return row;
}catch(Execption e){
}finally{
closeAll();
}
return 0;
}
//关闭资源
public void closeAll(){
try {
if (resultSet != null) {
resultSet.close();
}
if (ps != null) {
ps.close();
}
if (connection != null) {
connection.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}