************连接方式(一)Access_JDBC30.jar,此包由于是免费的,所有限制连接单次不超过50*************************
Connection conn = null;
PreparedStatement stat = null;
ResultSet rs=null;
//连接数据库(添加access数据库的驱动jar包:Access_JDBC30.jar)
public Connection openConn() {
try {
//这种方法连接access不需要access的驱动jar包Access_JDBC30.jar
//String url = "JDBC:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:/eclipse/project/jee/workspace/jubweb2/src/message.mdb";
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//这种方法连接access需要access的驱动jar包Access_JDBC30.jar
String url="jdbc:access:/D:/eclipse/project/jee/workspace/jubweb2/src/message.mdb";//不支持相对路径
Class.forName("com.hxtt.sql.access.AccessDriver").newInstance();
Connection conn = DriverManager.getConnection(url);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//关闭数据库
public void closeConn() {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//添加
public String addMessage(Yjfk yjfk) {
conn =openConn();
try {
String sql = "insert into message values(?,?,?,?,?,?)";
stat =openConn().prepareStatement(sql);
stat.setString(1, UUID.randomUUID().toString());
stat.setString(2, yjfk.getContent());
stat.setString(3, yjfk.getCtype());
stat.setString(4, yjfk.getName());
stat.setString(5, yjfk.getEmail());
stat.setString(6, yjfk.getPhone());
stat.executeUpdate();
return "success";
} catch (Exception e) {
e.printStackTrace();
} finally {
closeConn();
}
return "error";
}
//查询
public List<Yjfk> qryMessage() throws SQLException{
String sql = " select * from message ";
stat = openConn().prepareStatement(sql);
rs=stat.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("content"));
}
closeConn();
return null;
}
************连接方式(二)JDBC-ODBC,jdk自带连接驱动,无需jar,但是需要配置本地电脑环境*************************
package com.qgc.tool;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.dbcp.BasicDataSource;
/**
* 使用abcp连接池
* @author admin
*所需JAR包:commons-pool-1.6.jar、commons-collections-3.2.1.jar、commons-dbcp-1.4.jar
*
*/
public class Dbcp_Jdbc_odbc {
private static final int INITIAL = 50;//初始化50个连接
private static final int MAX_ACTIVE = 500;//最大值500个连接
private static final int MAX_IDLE = 10;//最大空闲10
private static final long MAX_WAIT = 2 * 1000;//超过500个访问,等待的时间
private static final String DRIVER_NAME = "sun.jdbc.odbc.JdbcOdbcDriver";
private static BasicDataSource bds;
static{
if(bds == null){
bds = new BasicDataSource();
}
bds.setDriverClassName(DRIVER_NAME);
bds.setInitialSize(INITIAL);
bds.setMaxActive(MAX_ACTIVE);
bds.setMaxIdle(MAX_IDLE);
bds.setMaxWait(MAX_WAIT);
}
/**
* 打开连接池
* @param path 数据库的路径
* @return
* @throws SQLException
*/
public static Connection getSqliteConnection(String path) throws SQLException{
if(path==null){
path="qqData";
}
/*qqData: 配置方式:控制面板->管理工具->ODBC->系统DSN->添加 Microsoft Access Driver(*.mdb)
数据源名:sun (这个与你写的jdbc:odbc:sun对应)
选择->你建的*.mdb的位置
确定就可以用了
*/
String url = "jdbc:odbc:"+path; //myDS 数据源名称;
bds.setUrl(url);//数据库的路径
return bds.getConnection();
}
/**
* 释放数据库连接
*
* @param rs
* @param st
* @param conn
*/
public static void freeConnection(ResultSet rs, Statement st,
Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}