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
相关文档:
Java NIO API详解
在JDK
1.4以前,Java的IO操作集中在java.io这个包中,是基于流的阻塞(blocking)API。对于大多数应用来说,这样的API使用很方
便,然而,一些对性能要求较高的应用,尤其是服务端应用,往往需要一个更为有效的方式来处理IO。从JDK 1.4起,NIO
API作为一个基于缓冲区,并能提供非阻塞(non-blo ......
单例模式,顾名思义,只能有一个实例。
一.从多线程安全说起,如下图代码,此问题可以用synchronized关键字来解决。该方法缺点:每一个线程在获取实例对象之前都要在synchronized上同步的对象上进行等待,因此效率不高。
二.Double Check方法,见下图代码。Double Check的初衷是只有当instance为NULL时执行的线程才需要在 ......
dnd是drag and drop的缩写.
java中的dnd主要涉及到3个类:TransferHandler(用来处理数据的拖放过程),Transferable(用来包装拖放的数据),和DataFlavor(用来表示拖放的数据的类型).下面来介绍这3个类的方法
1.javax.swing.TransferHandler
它有两个构造函数:
TransferHandler() 子类的便捷构造方法。
TransferHandler(St ......
Seasar2
一个项目中要用到seasar+flex,据说这个东西在小日本那里已经大量用于企业级B/S应用,当然这个开源组件也是他们开发的,所以收集一些相关资料,备忘..
所谓“Seasar2”就是一个“轻量级容器”,面向无法摆脱“Java 应用开发”之烦恼的所谓“开发者”,它能够保证开发的&ldq ......
第一种:
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
效率高,以后一定要使用此种方式!
第二种:
Map map = new HashMap();
Iterato ......