JAVA在原有数据库通讯的基础上新增与Access数据库的简单通讯

第一步:首先我们需要写一个Access数据库接口连接类

/**
* Created by Knigh on 2017/6/26.
*/
public class AccessJoinAction {

    Connection con = null;
    Statement s = null;
    ResultSet rs=null;

    /*连接*/
    public Connection getConnection(){
        try {
            // 加载JDBC-ODBC桥驱动程序
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            // 方式一  通过驱动联接
            con=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=***.mdb; pwd=******");
            // 方式二 通过数据源与数据库建立连接
            //con = DriverManager.getConnection("jdbc:odbc:AccessDatabase");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    /*关闭数据源*/
    public void CloseConnection(Connection con,ResultSet rs,Statement s){
        try {
            if (rs!=null) {
                rs.close();
            }
            if (s!=null) {
                s.close();
            }
            if (con!=null) {
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

第二步:我们写具体的增删改查方法类继承第一步的连接类,通过main方法可测试数据

/**
 * Created by Knigh on 2017/6/26.
 */
public class AccessAction extends AccessJoinAction {

    Connection con = null;
    Statement s = null;
    ResultSet rs=null;

    public String seach(Integer taskID,Integer status){
        String isOK = null;
        try {
            con=getConnection();
            s = con.createStatement();
            rs=s.executeQuery("select `TaskID`,`conTent1` from Task_MCGS order by `TaskID` ");
            while (rs.next()) {
                Integer a = rs.getInt("TaskID");
                String b = rs.getString("conTent1");
                if((a > taskID) && (status == 2) ){
                    isOK = a + "-" + b;
                    break;
                }else{

                }
            }
            System.out.println("查询结束");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CloseConnection(con, rs, s);
        }
        return isOK;
    }

    public void add(String code,Integer taskID, Integer partOK,Integer partNOK,String time){
        try {
            int result=0;
            con=getConnection();//调用继承类 方法
            s = con.createStatement();// 创建SQL语句对象
            result=s.executeUpdate("INSERT INTO OpHist_MCGS(`ProdName`,`EquipOpRelID`,`IsCompleted`,`UserChar2`,`UserChar3`,`IsOK`,`IsNOK`,`MCGS_Time`)VALUES('"+code+"',15,1,'"+code+"',"+taskID+","+partOK+","+partNOK+",'"+time+"')");
            if (result>0) {
                System.out.println("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CloseConnection(con, rs, s);
        }
    }

    public void updata(){
        try {
            int result=0;
            con=getConnection();
            s = con.createStatement();
            result=s.executeUpdate("update Table1 set name='aa' where id=1");
            if (result>0) {
                System.out.println("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CloseConnection(con, rs, s);
        }
    }

    public void delete(){
        try {
            int result=0;
            con=getConnection();
            s = con.createStatement();
            result=s.executeUpdate("delete from Table1 where id=3");
            if (result>0) {
                System.out.println("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CloseConnection(con, rs, s);
        }
    }
    public static void main(String[] args) {
          AccessAction accessAction=new AccessAction();
//        accessAction.seach();
          accessAction.add("adwadwadaw",120,0,1, "2017-09-08 08:19:10");
//        accessAction.updata();
//        accessAction.delete();
    }
}

觉得辛苦就扫一扫: 

 20190329111927164.png

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Knight-Key

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值