📄 linkdb.java
字号:
/*
*建议:在连接数据库后,执行完操作后要关闭连接对象和结果集,目的是提高效率
*Linkdb()
*openConnection(String poolName) 连接数据库
*executeQuery() 执行Select语句
*executeUpdate() 执行Insert,Update语句
*releaseConnection() 关闭连接对象和结果集
*/
//可执行查询,修改,删除,插入等操作
package com.jxyd.sql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Linkdb {
/** 构造器 */
public Linkdb() {
}
/** 连接数据库 */
public void openConnection(String poolName) {
try {
this.poolName = poolName;
/** 从连接池中取出一条连接 */
connMgr = DBConnectionManager.getInstance();
this.con = connMgr.getConnection(poolName);
this.con.setAutoCommit(false);
stmt = con.createStatement();
rs = stmt.executeQuery("select 1 from dual");
con.commit();
// java.util.Calendar date=java.util.Calendar.getInstance();
} catch (Exception e) {
connMgr.release();
System.out.println("报错时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
System.out.println("连接数据库失败,创建数据库连接池失败!请稍后重试");
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
rs = null;
stmt = null;
} catch (Exception e) {
}
}
}
/** 获得一个连接对象Connection */
public Connection getConnection() {
return con;
}
/** 释放一条连接,关闭连接对象和结果集 */
public void releaseConnection() {
try {
/** 关闭结果集 */
// if(rs!=null)rs.close();
/** 关闭Statement对象 */
// if(stmt!=null)stmt.close();
/** 释放一条连接到连接池中 */
connMgr.freeConnection(poolName, con);
} catch (Exception e) {
System.out.print("关闭结果集或数据库失败");
e.printStackTrace();
}
}
/** 关闭连接池 */
public void releasePool() {
connMgr.release();
}
private Connection con = null;
private Statement stmt = null;
ResultSet rs = null;
private DBConnectionManager connMgr;
private String poolName = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -