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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Knight_Key/article/details/82760358

第一步:首先我们需要写一个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();
    }
}

猜你喜欢

转载自blog.csdn.net/Knight_Key/article/details/82760358