package com.mine.Test;
import com.alibaba.druid.pool.DruidAbstractDataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.mine.dao.DruidTools;
import com.mine.dao.UserDao;
import com.mine.dao.UserDaoImpl;
import com.mine.pojo.User;
import jdk.internal.util.xml.impl.Input;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
public class Demo {
UserDao userDao=new UserDaoImpl();
public static void main(String[] args) throws Exception{
}
@Test
public void fun1() throws Exception{
User user=new User();
user.setId(2);
user.setUsername("superman");
user.setPassword("888");
user.setAddress("cq");
user.setState(1);
//将时间转换为字符串
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s=sdf.format(date);
user.setTime(s);
UserDao userDao=new UserDaoImpl();
int i = userDao.addUser(user);
System.out.println(i);
}
@Test
public void fun2() throws Exception{
List<User> userList=userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
}
@Test
public void fun3() throws Exception{
User user=new User();
user.setId(3);
user.setUsername("qwer");
user.setPassword("qewr");
user.setAddress("TY");
user.setState(1);
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(date);
user.setTime(s);
int i = userDao.updateUser(user);
if (i>0){
System.out.println("ok!!");
}else {
System.out.println("fuck!");
}
}
@Test
public void fun4() throws Exception{
//创建连接池对象
DruidDataSource dds=new DruidDataSource();
//设置连接数据库的账号和密码
dds.setDriverClassName("com.mysql.jdbc.Driver");
dds.setUrl("jdbc:mysql://localhost:3306/mydb1");
dds.setUsername("root");
dds.setPassword("123");
//最多可以创建10个对象
dds.setMaxActive(10);
System.out.println(dds);
//获取连接对象,并输出
System.out.println(dds.getConnection());
}
/**
* 通过连接池链接数据库
*
* @throws Exception
*/
@Test
public void fun5() throws Exception {
//获取类加载器用于加载classpath下面的配置文件
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
//读取db.properties配置对象
InputStream inputStream = classLoader.getResourceAsStream("db.properties");
//创建Properties对象,并读取配置文件对应的输入流
Properties properties = new Properties();
properties.load(inputStream);
//创建连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
//获取连接对象
System.out.println(dataSource.getConnection());
}
/**
* 使用DButils工具进行查询整体语句
*
* @throws Exception
*/
@Test
public void fun6() throws Exception{
//Dbtils使用测试
QueryRunner qr=new QueryRunner(DruidTools.getDateSoure());
String sql="select * from tb_mine";
//利用反射,返回User所有的属性,将查询后的的结果保存到集合里面
List<User> list= qr.query(sql, new BeanListHandler<User>(User.class));
for (User user : list) {
System.out.println(user);
}
}
/**
* 使用DButils工具进行查询条件语句
*
* @throws Exception
*/
@Test
public void fun7() throws Exception{
QueryRunner qr=new QueryRunner(DruidTools.getDateSoure());
String sql="select * from tb_mine where id=?";
//Object ... params ... (三个点)语法表示可变参数,该参数个人不确定,范围是0~n个
User user = qr.query(sql, new BeanHandler<User>(User.class), 1);
System.out.println(user);
}
/**
* 使用DButils工具进行修改语句
*
* @throws Exception
*/
@Test
public void fun8() throws Exception {
QueryRunner qr=new QueryRunner(DruidTools.getDateSoure());
String sql="insert into tb_mine(id,username,password) values(?,?,?)";
Object[] o={7,"tt","123"};
int i = qr.update(sql,o);
System.out.println(i);
}
}
package com.mine.dao;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class DruidTools {
private static Properties properties=new Properties();
private static DataSource dataSource;
static {
try {
//获取类加载器
ClassLoader classLoader=Thread.currentThread().getContextClassLoader();
//使用类加载器获取 项目类路径下的文件
InputStream inputStream=classLoader.getResourceAsStream("db.properties");
//使用Properti假爱配置文件对挺的输入流
properties.load(inputStream);
//创建连接池对象
dataSource= DruidDataSourceFactory.createDataSource(properties);
}catch (Exception e){
e.printStackTrace();
}
}
public static DataSource getDateSoure(){
return dataSource;
}
public static Connection getConnection() throws Exception {
return dataSource.getConnection();
}
}
学习过程13小补充(DButils的加入和小测试,作为11的小补充,仅为DButils使用小测试)
最新推荐文章于 2022-10-11 12:40:45 发布