学习
视频地址
JDBC
面向应用的API + 面向数据库的API
1. 获取数据库连接
需要四个参数:url user password 驱动
将连接数据库的基本信息通过配置文件加载得:
@Test
public void test4() throws Exception {
//1.读取配置文件
Properties p = new Properties();
InputStream is = contest.class.getClassLoader().getResourceAsStream("jdbctest.properties");
p.load(is);
String user = p.getProperty("user");
String password = p.getProperty("password");
String url = p.getProperty("url");
//反射获取运行时类 ---- 获取驱动
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
//获取连接
Connection con = DriverManager.getConnection(url, user, password);
System.out.println(con);
}
2. statement与preparestatement
statement:sql注入问题不用
prepareStatment:
@Test
public void test5() throws Exception {
//增删改
//获取配置文件 得到url 用户名 密码
Properties p = new Properties();
InputStream is = contest.class.getClassLoader().getResourceAsStream("jdbctest.properties");
p.load(is);
String user = p.getProperty("user");
String url = p.getProperty("url");
String password = p.getProperty("password");
//加载驱动
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
//预编译sql语句,返回preparestatement实例
String sql = "insert into customers(name,email,birth) values(?,?,?);";
PreparedStatement ps = connection.prepareStatement(sql);
//获取数据
ps.setString(1,"张三");
ps.setString(2,"1162916986@163.com");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date parse = sdf.parse("1999-03-23");
ps.setDate(3, new java.sql.Date(parse.getTime()));
//执行
ps.execute();
//关闭连接
ps.close();
connection.close();
}
增删改
public static void update(String sql,Object args[] ) {
//连接数据库
Connection connection = null;
PreparedStatement ps = null;
try {
connection = JDBCUtil.getConnection();
//预编译sql
ps = connection.prepareStatement(sql);
} catch (Exception e) {
e.printStackTrace();
}
//填充占位符
for(int i = 0 ; i < args.length;i++){
try {
ps.setObject(i+1,args[i]);
} catch (SQLException e) {
e.printStackTrace();
}
}
//执行
try {
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
//关闭
JDBCUtil.closeResource(connection,ps);
System.out.println("修改成功!");
}
查
public static <T> ArrayList<T> select(Class<T> cls, String sql, Object[] args) throws Exception {
//获取链接
Connection con = JDBCUtil.getConnection();
//预编译sql
PreparedStatement ps = con.prepareStatement(sql);
//填充占位符
for(int i=0 ; i<args.length;i++){
ps.setObject(i+1,args[i]);
}
//执行sql并接收返回值
ResultSet rs = ps.executeQuery();
//获取结果集的元数据
ResultSetMetaData rsmd = rs.getMetaData();
获取元数据的列数
int columnCount = rsmd.getColumnCount();
int num=0;
ArrayList<T> ts = new ArrayList<T>();
while (rs.next()){
//创建对象 ,泛型
T t = cls.getDeclaredConstructor().newInstance();
//获取元数据中的值
for(int j=0 ; j <columnCount;j++){
//获取元数据中的值
Object object = rs.getObject(j + 1);
//获取元数据中字段名/别名 --- 可以在sql中起别名
String columnLabel = rsmd.getColumnLabel(j + 1);
//通过字段名,反射写入泛型对象中
Field df = order.class.getDeclaredField(columnLabel);
df.setAccessible(true);
df.set(t,object);
}
//将泛型对象存储入集合中
ts.add(num,t);
}
//返回集合
return ts;
}
blob数据
tinyblob:255
blob:65K
mediumblob:16M
longblob:4G
存入blob文件:
FileInputStream fis = new FileInputStream(new File("1.jpg"));
ps.setBlob(4,fis);
查询blob文件:
Blob photo = rs.getBlob("photo");
// 返回一个用于从 BLOB 中读取数据的输入流。
InputStream is = photo.getBinaryStream();
//写入流
FileOutputStream fos = new FileOutputStream("2.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer))!= -1){
fos.write(buffer,0,len);
}
批量写入
- 设置不自动提交: connection.setAutoCommit(false);
- 攒sql: ps.addBatch();
//连接数据库
Connection connection = JDBCUtil.getConnection();
connection.setAutoCommit(false);
//预编译sql
String sql = "insert into ss(name) values(?);";
PreparedStatement ps = connection.prepareStatement(sql);
//填充占位符
for (int i = 0; i <= 60000; i++) {
ps.setString(1, "陈可辛"+i+1);
ps.addBatch();
if(i %6000==0){
ps.executeBatch();
ps.clearBatch();
}
}
connection.commit();