java无数据源连接Access数据库实例
1.连接数据库ConnDB()类
package tool;
/****************************
**
**属性文件与数据库均在tool包下面
**
*****************************/
/* 数据访问组件 */
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class ConnDB{
private static ConnDB instance=null;
String db_driver=null;
String db_url=null;
String db_user=null;
String db_psw=null;
String db_name=null;
String proPath="conn.properties";
Connection conn=null;
public ConnDB(){
InputStream in=getClass().getResourceAsStream(proPath);
Properties prop=new Properties();
try {
prop.load(in);
db_driver=prop.getProperty("db_driver",db_driver);
db_url=prop.getProperty("db_url",db_url);
db_user=prop.getProperty("db_user",db_user);
db_psw=prop.getProperty("db_psw", db_psw);
db_name=prop.getProperty("db_name",db_name);
db_url=db_url+getDBPath();
} catch (IOException e) {
e.printStackTrace();
}
}
//获得安全连接
public synchronized Connection getConnection(){
if(instance==null){
instance=new ConnDB();
}
return instance._getConnection();
}
//释放资源
public static void dbClose(Connection conn,PreparedStatement ps,ResultSet rs){
try{
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
//创建连接
private Connection _getConnection() {
try {
Class.forName(db_driver).newInstance();
conn=DriverManager.getConnection(db_url, db_user, db_psw);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//返回数据库的绝对路径
public String getDBPath(){
String dbpath=getClass().getResource(db_name).getFile();
dbpath=db
相关文档:
单例模式,顾名思义,只能有一个实例。
一.从多线程安全说起,如下图代码,此问题可以用synchronized关键字来解决。该方法缺点:每一个线程在获取实例对象之前都要在synchronized上同步的对象上进行等待,因此效率不高。
二.Double Check方法,见下图代码。Double Check的初衷是只有当instance为NULL时执行的线程才需要在 ......
目标:实现一个汉字字符串按汉语拼音字典顺序排序。
原理:在windows环境的gbk字符集里,汉字是按汉语拼音字典顺序编码的,如“础”是B4A1,“储”是B4A2。这里有个问题就像上面的储和础这样的同音字只能遵照编码的顺序了,另外多音字也得遵照编码顺序。设计思路是先拆分汉字字符串为字符数组,获得每 ......
(1)根据xml文件来管理线程池的最大最小线程数
(2)对线程池通过Timer定期扫描以防止线程未激活;
(3)通过某一个变量(本程序中是freeThreadCount)来得到空闲线程的数目;
一、配置xml(listen.xml)是:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<Con ......
1、首先安装JDK(如安装在C:\Program Files\Java\jdk1.6.0)
2、设置系统环境参数:
1)右击“我的电脑”,选“属性”。
2)选择“高级”选项卡,点击“环境变量”按钮
3)点击系统变量内的“新建&rd ......