二、JDBC
1.JDBC概述
全称:Java database connectivity java连接数据库
连接数据库的驱动,想使用就要加载jar包
静态SQL语句:insert into work (name, age, info) values(“狗蛋”, 12, “asjj”); Statement
参数化的SQL语句: insert into work (name, age, info) values(?, ?, ?); PreparedStament
2.Statement增删改
public class Demo1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.准备连接数据库的参数
String url="jdbc:mysql://localhost:3306/数据库名?useSSL=false";
String user = "root";
String password = "123456";
//3.获取连接数据库的对象
Connection connection = DriverManager.getConnection(url,user,password);
//4.创建Statement搬运工对象 通过此对象将SQL语句发送到数据库执行。
Statement statement = connection.createStatement();
//5.写SQL语句
String sql = "增删改的sql语句";
//6.使用statement对象去执行sql语句
int i = statement.executeUpdate(sql);
//7.关闭资源
statement.close();
connection.close();
}
}
3.Statement查询数据库全数据
resultset对象保持一个光标指向其当前的数据行。
光标最初位于第一行数据之前,next()方法将光标移动到下一行。
查询出来的数据需要赋值给一个类对象,然后存到集合中
public class Demo1 {
public static void main(String[] args)throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url =
"jdbc:mysql://localhost:3306/数据库名?useSSL=false";
String user = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url,user,password);
Statement statement = connection.createStatement();
String sql = "select * from 表名";
//执行给定sql语句,返回单个ResultSet对象结果集。
//ResultSet对象中包含了给定查询产生的数据
//因为ResultSet对象中没有更多行时会返回false
//所以可以通过while循环中使用循环来遍历结果集
ResultSet resultset = statement.executeQuery(sql);
List<Work> list = new ArrayList<>();//数组<work>接收数据
while(resultset.next()){//行数
int id=resultset.getInt(1);//参数1是字段的顺序,从1开始
String name = resultSet.getString("name");//参数是字段名
int age = resultSet.getInt("age");//获取该字段下的数据值
String info = resultSet.getString("info");
Work work = new Work(id,name,age,info);
list.add(work);
}
resultSet.close();
statement.close();
connection.close();
}
}
4.预处理搬运工增删改
使用预处理的搬运工对象可以防止SQL注入 sql语句条件 1=1
public class Demo1 {
public static void main(String[] args)throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/java2304?useSSL=false";
String user = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url, user, password);
//1.创建预处理的搬运工对象
String sql="insert into work (name, age, info) values(?, ?, ?)";
//2.SQL语句已经预编译并存储预搬工对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//3.使用预处理对象给指定参数赋值
prepareStatement.setObject(1,"狗蛋")//对第一个?赋值
preparedStatement.setObject(2, 12);//对第二个?赋值
preparedStatement.setObject(3, "喜欢案");//对第三个?赋值
//4.执行sql语句
int i= preparedStatement.executeUpdate();
//5.关闭资源
preparedStatement.close();
connection.close();
}
}
三、封装JdbcUtil工具类
封装的内容是什么?
1.获取connection对象
2.关闭资源
去将连接库的一些信息参数 放到配置文件中,然后让Java去读取配置文件。改的时候只需要改配置文件
1.在src文件夹下面新建一个叫 db.properties
2.在 db.properties 去写连接数据库的参数 以键值对的数据
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/java2304?useSSL=false
user=root
password=123456
public class JdbcUtil {
private static String url = null;
private static String user = null;
private static String password = null;
static{//只要JdbcUtil类加载了,就会执行静态代码块
try{
//Properties类表示一组持久的属性。Properties可以保存到流中或者从流中加载。
//属性列表中的每个键及其对应的值都是一个字符串
Properties properties = new Properties();
properties.load(new FileInputStream("src/db.properties"));
String diver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
Class.forName(diver);
} catch (Exception e) {
e.printStackTrace();
}
}
//连接数据库
public static Connection getConnection(){
Connection connection = null;
try{
connection = DriverManager.getConnection(url,user,pass)
}catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
//关闭资源
//增删改 需要关闭两个 查 关闭三个
public static void close (Statement statement, Connection connection) {
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close (ResultSet resultSet, Statement statement, Connection connection) {
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
四、JavaBean规范
JavaBean代表实体类, Work类就是实体类。实体类规范有四条
1.私有化成员变量。
2.一定要提供一个无参构造方法。
3.一定要提供set和get方法。
4.每个类都要单独成为一个文件。
五、BeanUtils类
第三方提供的一个类,需要导入jar包 beanutils和logging
之前取值和赋值是使用的set和get方法,BeanUtils是另一种方式。
BeanUtils类是动态的对属性进行赋值和取值。
public class Demo1 {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
//setProperty 设置属性
//第一个参数: Object bean 属性所在的类对象
//第二个参数: String name 属性的名字
//第三个参数: Object value 属性的值
//static void setProperty(Object bean, String name, Object value)
Work work = new Work();
BeanUtils.setProperty(work, "name", "大黄");
//getProperty 获取属性值
//第一个参数: Object bean 属性所在的类对象
//第二个参数: String name 属性的名字
//static String getProperty(Object bean, String name)
String name = BeanUtils.getProperty(work, "name");
System.out.println(name);
}
}
六、元数据【重点】
metadata 元数据
1.关于SQL参数的元数据
public class Demo1 {
public static void main(String[] args) throws
SQLException {
Connection connection = JdbcUtil.getConnection(); String sql = "insert into work (name, age, info) values(?,?,?)";
PreparedStatement preparedstatement =
connection.getPrepareStatement(sql);
//借助于预处理搬运工对象获取 参数元数据对象
ParameterMetaData parameterMetaData =
preparedstatement.getParameterMetaData();
//通过参数元数据对象 获取参数的个数
int parameterCount =
parametermetadata.getParameterCount();
//使用Object[]数组对?进行赋值
Object[] objs = {"狗蛋", 12, "嘻嘻按时间计算"};
for (int i = 1; i <= parameterCount; i++) {
preparedStatement.setObject(i, objs[i - 1]);
}
prepareStatement.execuUpdate();
}
}
2.关于结果集的元数据
public class Demo3 {
public static void main(String[] args)throws Exception{
Connection connection = JdbcUtil.getConnection();
String sql = "select * from work";
PreparedStatement preparedstatement =
connection.getPrepareStatement(sql);
ResultSet resultset = preparedstatement.executeQuery();
//通过结果集对象获取结果集元数据
ResultSetMetaData metaData = resultset.getMetaData();
//ResultSetMetaData 结果集元数据中存有字段个数和字段名字
//通过结果集对象获取字段个数
int columnCount = metaData.getColunmCount();
while(resultset.next()){//数据库表的行数
for (int i = 1; i <= columnCount ; i++) {//列数
//通过结果集对象获取字段名
String columnName = metaData.getColumnName(i);
//通过结果集对象得到该字段名下的数据
Object object = resultset.getObject(columnName);
System.out.println(object);
}
}
}
}