Realize the interaction between database and java code.

Joint application of data pool C3P0, database mysql and DbUtils framework

Ready to work

You need to prepare the c3p0 data pool jar package, the C3P0-config.xml file, the mysql database jar package and the most important DbUtils framework jar package and create a student data table in the mysql database (as shown in the figure).
Insert picture description here

Configuration file

1. Create a lib ask price folder in the project, put the above three jars into it and (click the right mouse button) buildpath produces the effect shown in the figure below.
Insert picture description hereAt this time, our jar package can be used.
2. Modify the relevant path and database name than our c3p0.xml configuration file (as shown)
Insert picture description here

Use code to add, delete, modify, and check data in the database.

Ready for the above work, we can add, delete, check and modify the data table through java code
1. Create a student class javabean

Show some below 内联代码片.

Show some below 内联代码片.

//javabean
public class Student {
    
    
	private int id;
	private String name;
	private int password;
	private String city;
	public Student() {
    
    
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(int id, String name, int password, String city) {
    
    
		super();
		this.id = id;
		this.name = name;
		this.password = password;
		this.city = city;
	}
	public int getId() {
    
    
		return id;
	}
	public void setId(int id) {
    
    
		this.id = id;
	}
	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	public int getPassword() {
    
    
		return password;
	}
	public void setPassword(int password) {
    
    
		this.password = password;
	}
	public String getCity() {
    
    
		return city;
	}
	public void setCity(String city) {
    
    
		this.city = city;
	}
	@Override
	public String toString() {
    
    
		return "Studnt [id=" + id + ", name=" + name + ", password=" + password + ", city=" + city + "]";
	}

2. Load the data pool driver to create a connection

Show some below 内联代码片.

private static DataSource dataSource = new ComboPooledDataSource();

	// 创建连接
	public static DataSource getDataSource() throws Exception {
    
    
		return dataSource;
	}
	public static Connection getConnction() {
    
    
		try {
    
    
			return dataSource.getConnection();
		} catch (SQLException e) {
    
    
			throw new RuntimeException("连接失败");	
		}
	}
	public static void release(Connection connection,Statement statement,ResultSet resultSet) {
    
    
		if (connection!=null) {
    
    
			try {
    
    
				connection.close();
			} catch (SQLException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (statement!=null) {
    
    
			try {
    
    
				statement.close();
			} catch (SQLException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (resultSet!=null) {
    
    
			try {
    
    
				resultSet.close();
			} catch (SQLException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

3. Add, delete, modify and check the data

Show some below 内联代码片.

// 查找一个学生
	public void select() throws Exception {
    
    
		QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
		// 编写sql语句
		String sql = "select * from student where name = ?";
		Object params[] = {
    
    "sws"};
		BeanHandler<Student> beanHandler = new BeanHandler<Student>(Student.class);
		//执行sql语句
		Student student = queryRunner.query(sql,beanHandler ,params);
		System.out.println(student);
	}
	
	//查询全部学生
	public void selectAll() throws Exception {
    
    
		QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
		//编写SQL语句
		String sql = "select * from student";
		BeanListHandler<Student> beanListHandler = new BeanListHandler<Student>(Student.class);
		//执行sql语句
		List<Student> student = queryRunner.query(sql, beanListHandler);
		for(Student s:student) {
    
    
			System.out.println(s);
		}
	}
	//增加学生
	public void update() throws Exception {
    
    
		QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
		//编写sql语句
		String sql = "insert into student(id,name,password,city) values(?,?,?,?)";
		Object parmas[] = {
    
    5,"cz",2222,"曲靖"};
		//执行语句
		int update = queryRunner.update(sql, parmas);
		if (update>0) {
    
    
			System.out.println("添加成功");
		}else {
    
    
			System.out.println("添加失败");
		}
	}
	//删除学生
	public void delete() throws Exception {
    
    
		QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
		//编写sql语句
		String sql = "delete from student where name =?";
		Object parmas[] = {
    
    "cz"};
		//执行语句
		int update = queryRunner.update(sql, parmas);
		if (update>0) {
    
    
			System.out.println("删除成功");
		}else {
    
    
			System.out.println("删除失败");
		}
	}
	//修改学生
	public void insert() throws Exception {
    
    
		QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
		//编写sql语句
		String sql = "update student set password=? where name=? ";
		Object parmas[] = {
    
    9621,"lm"};
		//执行sql语句
		int update = queryRunner.update(sql, parmas);
		if (update>0) {
    
    
			System.out.println("修改成功");
		}else {
    
    
			System.out.println("修改失败");
		}
	}

4. Finally, call the encapsulated addition, deletion, modification, and check method through a Test class

Show some below 内联代码片.

public static void main(String[] args) {
    
    
		StudentDao dao = new StudentDao();
		try {
    
    
//			查询一个学生
//			dao.select();

//			查询全部学生
//			dao.selectAll();

//			修改学生
//			dao.update();

//			删除学生
//			dao.delete();

//			插入学生
			dao.insert();
		} catch (Exception e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

to sum up

In the process of data interaction, you need to pay attention to the configuration and path of the data pool file, followed by the preparation of the related jar package. If you have any friends who don’t understand, you can privately message me.

Guess you like

Origin blog.csdn.net/fdbshsshg/article/details/115362519