durid.properties:
Код: Выделить всё
#key=value
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db02?rewriteBatchedStatements=true&serverTimezone=UTC
#url=jdbc:mysql://localhost:3306/girls
username=root
password=password
#initial connection Size
initialSize=10
#min idle connecton size
minIdle=5
#max active connection size
maxActive=20
#max wait time (5000 mil seconds)
maxWait=5000
Код: Выделить всё
package com.jdbc.c3p0_Druid;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* @author Alan
* @version 1.0
*/
public class utilsByDruid {
private static DataSource ds;
static{
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src\\druid.properties"));
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws Exception {
return ds.getConnection();
}
public static void close(ResultSet resultSet, Statement statement, Connection connection){
try {
if(resultSet != null){
resultSet.close();
}
if(statement != null){
statement.close();
}
if(connection != null){
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Код: Выделить всё
package com.jdbc.apDB;
/**
* @author Alan
* @version 1.0
*/
public class Actor{
public static Integer id;
public static String name;
public static String sex;
public static String phone;
public Actor(){
}
public Actor(Integer id, String name, String sex, String phone){
this.id = id;
this.name = name;
this.sex = sex;
this.phone = phone;
}
public static Integer getId() {
return id;
}
public static void setId(Integer id) {
Actor.id = id;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Actor.name = name;
}
public static String getSex() {
return sex;
}
public static void setSex(String sex) {
Actor.sex = sex;
}
public static String getPhone() {
return phone;
}
public static void setPhone(String phone) {
Actor.phone = phone;
}
@Override
public String toString() {
return " " + id + " " + name + " " + sex + " " + phone;
}
}
Код: Выделить всё
package com.jdbc.apDB;
import com.jdbc.c3p0_Druid.utilsByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.util.List;
/**
* @author Alan
* @version 1.0
*/
public class DBUtils_USE {
@Test
public void testQueryMany() throws Exception {
Connection connection = utilsByDruid.getConnection();
QueryRunner queryRunner = new QueryRunner();
String sql = "select * from actor where name = ?";
List list = queryRunner.query(connection, sql, new BeanListHandler(Actor.class), "Lele");
for(Actor actor : list){
System.out.println(actor);
}
}
}
Код: Выделить всё
+-----+------+-----+-------+
| id | name | sex | phone |
+-----+------+-----+-------+
| 10 | Lele | M | 949 |
| 11 | Lele | M | 949 |
| 12 | jack | M | 86 |
| 101 | polo | f | 991 |
+-----+------+-----+-------+
Код: Выделить всё
null null null null
null null null null
Код: Выделить всё
10 Lele M 949
11 Lele M 949
Подробнее здесь: https://stackoverflow.com/questions/785 ... ing-apache
Мобильная версия