c3p0连接池配置_Java使用c3p0连接池完成多表级联查询

引言:今天讲述实现一对一多表查询,完成两个表的关联,查询出两张表的所有字段信息。

准备工作:idea开发工具,可以在maven仓库下载c3p0连接池的jar包,对一对一,一对多有所了解,对多表查询的sql语句要有所了解,需要一定的Java基础。

1.建表

首先两张表要建立一定的关联,即一张表的主键作为另一张表的外键,本次我们有学生表和用户表,属于一对一的关系,我们就以学生表为主表,也就是他的主键当作用户表的外键


admin表,它的外键参考的是student表的主键

32b97567d1b7a3aa6aefa89b0b7a868f.png

sutent作为主表,一对一的左侧,当然两张表调换也是可以

6d0827113abe221c39bc9640789dbe7c.png

这个设置在admin表的外键序列里,我们参考的学生表的主键id,更新和删除设置restrict.具体的区别可以百度一下。

c2abe35e7efcceb1b0ba725606b865dc.png

2.配置c3p0连接池

xml version="1.0" encoding="UTF-8"?><c3p0-config><default-config>

<property name="driverClass">com.mysql.jdbc.Driver

property>

<property name="jdbcUrl">jdbc:mysql://localhost:3306/

db_student_system?useUnicode=true&

characterEncoding=utf-8property>

<property name="user">rootproperty><property name="password">123456property><property name="acquireIncrement">5property><property name="initialPoolSize">10property><property name="minPoolSize">5property><property name="maxPoolSize">20property>default-config>c3p0-config>

3.获取链接

package utils;import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3p0Utils {private static DataSource ds;static{ds=new ComboPooledDataSource();
}public static DataSource getDs(){return ds;
}

public static Connection getConnection() throws

SQLException{

return ds.getConnection();
}

public static void main(String args[])

throws SQLException {

System.out.println(new C3p0Utils()

.getConnection()+"mysql is ok");

}
}

4.建数据模型层

public class Student {private int id;private String stuno;private String sclass;

private String sex;

    private Admin admin;

public Student( int id,String stuno, String sclass,

String sex,String political, String sname, Integer age,

String level, String phone, String qq) {

this.id=id;this.stuno = stuno;this.sclass = sclass;this.sex = sex;this.political = political;this.sname = sname;this.age = age;this.level = level;this.phone = phone;this.qq = qq;
}

public Student(Integer id, String stuno,

String sclass, String sex, String account,

String pwd,String type, String political, String sname, Integer age, String level, String phone, String qq) {

super();this.id = id;this.stuno = stuno;this.sclass = sclass;this.sex = sex;this.admin=new Admin(account,pwd,type);this.political = political;this.sname = sname;this.age = age;this.level = level;this.phone = phone;this.qq = qq;

}

     private String political;private String sname;private Integer age;private String level;private String phone;private String qq;
public Student(Admin admin) {this.admin = admin;}public Admin getAdmin() {return admin;}public void setAdmin(Admin admin) {this.admin = admin;} ***********各个属性的setter和getter方法**********

}

******这个是admin表的**

package model;/*public class Admin {private Integer id;private String account;private String pwd;

private String type;

    private int s_id;
public void setAccount(String account) { this.account = account;}public Admin() {}

//构造方法,初始化对象

public Admin(String account, String pwd, String type) {this.account = account;this.pwd = pwd;this.type = type;
}public Admin(String account, String pwd, String type, int s_id) {this.account = account;this.pwd = pwd;this.type = type;this.s_id = s_id; }
 ***********各个属性的setter和getter方法**********

}

5.建立dao层

import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import model.Student;import org.apache.commons.dbutils.handlers.MapListHandler;import org.apache.commons.dbutils.handlers.ScalarHandler;import utils.C3p0Utils;public class StudentDao {//获取连接执行查询

private QueryRunner runner=new QueryRunner(C3p0Utils.getDs()

);

public List> findAll() {
List> list=null;try {

String sql = "select s.*,a.* from student s left join

admin a on s.id=a.s_id ";//左连接查询,查询的是两张表关联的字段。

//创建一个集合用来存储查询的参数,因为我们不清楚客户到底输入几个参数,所以用集合来存放list=runner.query(sql, new MapListHandler());
} catch (SQLException e) {//捕获异常throw new RuntimeException(e);//抛出运行异常}return list;

}

public static void main(String[] args){
StudentDao dao=new StudentDao();//调用对象

List> list=dao.findAll();//把多表两张表

查询的字段封装到Map里,再封装到List集合里。

Iterator> iterator=list.iterator();

//使用迭代器遍历集合。

while (iterator.hasNext()){
System.out.println(iterator.next());
}
}

}

6.运行结果截图

352ad13b8ef7d3d4b8c99a5dabf6e1e4.png

7.演示视频。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值