IDEA中C3P0链接数据库出现的错误——An attempt by a client to checkout a Connection has timed out.
今天课堂主要讲eclipse连接数据库(JDBC)以及IDEA连接数据库。
eclipse连接数据库步骤
1.导入相关驱动包并且进行部署
2.加载驱动
3.获得数据库连接对象(java.sql.Connection)
4.获得编译sql语句对象(Statement PreparedStatement)
5.处理结果集
6.释放资源
项目目录
连接示例
连接MySql:
public class JDBCDemo {
public static void main(String[] args) {
Connection conn= null;
Statement st = null;
ResultSet rs = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获得数据库连接对象,127.0.0.1即localhost,yyb为数据库表名,第一个root为数据库用户名,第二个为密码
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/yyb", "root", "root");
System.out.println(conn);
//获得编译SQL语句对象
st = conn.createStatement();
//获得结果集
rs = st.executeQuery("select ename,sal from emp");
while(rs.next()){
String ename = rs.getString("ename");
double sal = rs.getDouble("sal");
System.out.println(ename+","+sal);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//释放资源
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
连接Oracle同理:
public class OracleDemo {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tigger");
System.out.println(conn);
st = conn.createStatement();
rs = st.executeQuery("select ename,sal from emp");
while(rs.next()){
String ename = rs.getString("ename");
double sal = rs.getDouble("sal");
System.out.println(ename+","+sal);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
连接数据库实现增、删、改、查操作
1.如果要实现查询操作:
定义Connection Statement ResultSet
2.如果是增、删、改操作:
定义Connection Statement
3.执行查询sql语句:
rs=编译对象.executeQuery(String sql)
4.执行增、删、改的sql语句:
int i = 编译对象.executeUpdate(String sql)
删除 :
public class Demo01 {
/*
* 连接数据库实现增、删、改、查操作
* 注意:
* 1.如果要实现查询操作
* 定义Connection Statement ResultSet
* 2.如果是增、删、改操作
* 定义Connection Statement
* 3.执行查询sql语句
* rs=编译对象.executeQuery(String sql)
* 4.执行增、删、改的sql语句
* int i = 编译对象.executeUpdate(String sql)
*/
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:///yyb", "root", "root");
Statement st = conn.createStatement();
int i = st.executeUpdate("delete from account where id=1");
if(i>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
System.out.println(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
添加:
public class Demo02 {
/*
* 连接数据库实现增、删、改、查操作
* 注意:
* 1.如果要实现查询操作
* 定义Connection Statement ResultSet
* 2.如果是增、删、改操作
* 定义Connection Statement
* 3.执行查询sql语句
* rs=编译对象.executeQuery(String sql)
* 4.执行增、删、改的sql语句
* int i = 编译对象.executeUpdate(String sql)
*/
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:///yyb", "root", "root");
Statement st = conn.createStatement();
int i = st.executeUpdate("insert into account(name,money) values ('xiaoxiao',10056.6)");
if(i>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
System.out.println(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
修改:
public class Demo03 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdvc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:///yyb", "root", "root");
Statement st = conn.createStatement();
int i = st.executeUpdate("update account set name='wangwu' where id=2");
if (i>0) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
System.out.println(conn);
} catch (Exception e) {
// TODO: handle exception
}
}
}
IDEA连接数据库
项目目录:
连接示例:
出现的问题
错误示例:
正确示例
解决方法
1.查看配置文件,将连接池参数中的maxPoolSize以及checkoutTimeout中的参数加大
2.打开任务管理器中的服务,看MySQL是否运行
3.完成1.2点后进行重试。