- package com.work.core.db;
- /**
- *
- * @author wangmingjie
- * @date 2008-12-19上午09:51:27
- * 直接连数据库的时候一定要注意Microsoft Access Driver (*.mdb)中Driver后面必须有空格。
- */
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import com.gongjian.sysjy.model.Fy501;
- import com.work.core.exception.OurException;
- public class AccessConnectionExample {
- public static void main(String args[]) {
- List<Fy501> l = new AccessConnectionExample().getFy501Data("D://gjzdq//FY501.mdb");
- int LEN = l.size();
- for(int i=0;i<LEN;i++){
- System.out.println(l.get(i));
- }
- System.out.println("总共有"+LEN+"条记录!");
- }
- /**
- * 获取到自动缫丝机上的数据。将所有的数据一次性读取到内存中。
- * @param mdb access 文件的全路径。
- * @return
- */
- public List<Fy501> getFy501Data(String mdb){
- Connection conn = null;
- Statement stmt = null;
- ResultSet rst = null;
- List<Fy501> l = new ArrayList<Fy501>();
- try {
- // String strurl="jdbc:odbc:driver={Microsoft Access
- // Driver(*.mdb)};DBQ=D://gjzdq//FY501.mdb";//Driver后面没有空格,所以无法链接
- String strurl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+mdb;// 此为NO-DSN方式
- // String strurl="jdbc:odbc:test"; //需要配DSN,不要使用这种方式
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- System.out.println("before connection");
- conn = DriverManager.getConnection(strurl);
- System.out.println("after connection");
- stmt = conn.createStatement();
- rst = stmt.executeQuery("select * from 501");
- while (rst.next()) {
- Fy501 fy501 = new Fy501();
- fy501.setJyqh(rst.getString("jyqh"));
- fy501.setTh(rst.getString("th"));//台号
- fy501.setJybh(rst.getString("jybh"));//检验编号
- fy501.setGh(rst.getString("gh"));//工号
- fy501.setCs(rst.getString("cs"));//车速
- fy501.setStwd(rst.getString("stwd"));//缫汤温度
- fy501.setSxwd(rst.getString("sxwd"));//索绪温度
- fy501.setJysj(rst.getString("jysj"));//检验时间
- fy501.setGsls(rst.getString("gsls"));//供试粒数
- fy501.setTx(rst.getString("tx"));//添绪
- fy501.setDc(rst.getString("dc"));//吊糙
- fy501.setZc(rst.getString("zc"));//总长
- fy501.setPjls(rst.getString("pjls"));//平均粒数
- fy501.setH(rst.getString("h"));//厚
- fy501.setZ(rst.getString("z"));//中
- fy501.setB(rst.getString("b"));//薄
- fy501.setX(rst.getString("x"));//屑
- fy501.setL(rst.getString("l"));//漏
- fy501.setWd(rst.getString("wd"));//误吊
- fy501.setCsls(rst.getString("csls"));//初试粒数
- fy501.setQd(rst.getString("qd"));//纤度
- l.add(fy501);
- }
- } catch (Exception e) {
- throw new OurException("解析缫丝机输出的access数据出错!",e);
- } finally {
- try {
- rst.close();
- } catch (SQLException e) {
- }
- try {
- stmt.close();
- } catch (SQLException e) {
- }
- try {
- conn.close();
- } catch (SQLException e) {
- }
- }
- return l;
- }
- }
java直接连接access数据库
最新推荐文章于 2024-07-15 02:42:16 发布