在写web登录 使用c3p0连接数据库的时候 一直报错
连续好几个 java.sql.SQLException: No suitable driver
…
Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception:
java.sql.SQLException: No suitable driver
…
java.sql.SQLException: Connections could not be acquired from the underlying database!
…
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
折腾了两天 网上查出都是说配置文件的问题
最后把 c3p0-config.xml 文件 从com.xx.login子包移动到 src下
重启 tomcat 登录 登录成功
最后贴上代码~
DataSourceUtils.java
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @Author: Cangwu
* @Date: 2019/3/27 23:54
*/
public class DataSourceUtils {
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
// 获取数据源
public static DataSource getDataSource() {
return dataSource;
}
public static void CloseResource(Connection conn , Statement stmt, ResultSet rs) {
closeResultSet(rs);
closeStaement(stmt);
closeConn(conn);
}
public static void closeConn(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null ;
}
}
}
/**
* 释放语句执行者
* @param st
* 语句执行者
*/
public static void closeStaement(Statement st){
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
st = null ;
}
}
}
/**
* 释放结果集
* @param rs
* 结果集
*/
public static void closeResultSet(ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null ;
}
}
}
}
ServletLogin.java
import com.cangwu.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
/**
* @Author: Cangwu
* @Date: 2019/3/27 20:59
*/
@WebServlet(name = "ServletLogin",urlPatterns = "/login")
public class ServletLogin extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String email = request.getParameter("email");
String password = request.getParameter("password");
// 验证码调用
String inputCode = request.getParameter("inputCode");
String randomCode = (String)request.getSession().getAttribute("changecode");
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from tb_web where email=? and password=?";
List<User> user = null;
try {
user = runner.query(sql, new BeanListHandler<User>(User.class), email, password);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(user);
if (user != 0 && inputCode.equals(randomCode)) {
// 跳转到成功页面
} else {
// 失败
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
c3p0-config.xml
<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>
<default-config>
<property name="user">root</property>
<property name="password">123</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db_01</property>
<property name="initialPoolSize">6</property>
<property name="maxIdleTime">1000</property>
<property name="maxPoolSize">50</property>
<property name="minPoolSize">10</property>
</default-config>
</c3p0-config>
User.java
/**
* @Author: Cangwu
* @Date: 2019/3/23 20:27
*/
public class User {
private String email;
private String password;
private String repassword;
private String number;
private String username;
private String sex;
private String phonenum;
private String birthday;
private String code;
public User() {
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
@Override
public String toString() {
return "User{" +
"email='" + email + '\'' +
", password='" + password + '\'' +
", repassword='" + repassword + '\'' +
", number='" + number + '\'' +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
", phonenum='" + phonenum + '\'' +
", birthday='" + birthday + '\'' +
", code='" + code + '\'' +
'}';
}
}
多多指教