java項目串連access資料庫

來源:互聯網
上載者:User

標籤:ref   bool   ima   右鍵   nis   word   etc   .sql   資料庫   

1.匯入Access_JDBC30.jar到項目中

jar包百度雲連結:https://pan.baidu.com/s/10HFM3HomMArvfHjklA_1MA 密碼:0qxp

項目名稱-》右鍵-》build path-》add extra jar -》Access_JDBC30.jar

2.代碼如下:

package userpackage;import java.sql.*;import topicpackage.*;import actorpackage.*;import administratorpackage.*;import answerpackage.*;import exceptionpackage.*;public class UserDA {  //connection DB    public static Connection initialize()    {    try            {        //loading jdbc-odbc bridge driver        try{        Class.forName("com.hxtt.sql.access.AccessDriver").newInstance();        }        catch(InstantiationException e){            e.printStackTrace();        }        catch(IllegalAccessException e){            e.printStackTrace();        }        //        aConnection=DriverManager.getConnection("jdbc:Access:///D:/UserT.accdb");        aStatement=aConnection.createStatement();            }    catch(ClassNotFoundException e)            {            System.out.println(e);            }    catch(SQLException e)            {            System.out.println(e);            }    return aConnection;        }    /*釋放資源*/    public static void terminate(){        try{            aStatement.close();            aConnection.close();        }        catch(SQLException e){            System.out.println(e);        }    }    /*添加一個新記錄*/    public static void add(User aUser) throws DuplicateException{            String sql = "INSERT INTO UserT "        +"VALUES(‘"+userID+"‘,‘"+name+"‘,‘"+passWord+"‘,‘"+gender+"‘,‘"+image+"‘,‘"+isFrozen+"‘)";        System.out.println(sql);                try{            User c = find(userID);            throw(new DuplicateException("該使用者已存在"));        }                catch (NotFoundException e){            try{                int result = aStatement.executeUpdate(sql);            }            catch(SQLException ee){                System.out.println(ee);            }        }        }        public static User find(String key) throws NotFoundException        {            //retrieve Answer            aUser=null;            //define the SQL query statement using the phone number key            String sql="SELECT UserID,passWord,uname,image,gender,isFrozen FROM UserT"+" WHERE UserID= ‘"+key+"‘";            //execute the SQL query statement            System.out.println(sql);            try            {                ResultSet rs =aStatement.executeQuery(sql);                                //next method sets cursor & returns true if there is data                boolean gotIt=rs.next();                if(gotIt)                {                //extract the data                    userID=rs.getString(1);                    name=rs.getString(2);                    passWord=rs.getString(3);                    gender=rs.getString(4);                    image=rs.getString(5);                    isFrozen=rs.getString(6);                                                            aUser=new User(userID,name,passWord,gender,image,Boolean.valueOf(isFrozen));                                    }                else throw(new NotFoundException("沒有發現這個記錄"));                rs.close();                }                            catch(SQLException e)                {                    System.out.println(e);                }            return aUser;            }    public static void delete(User aUser){        userID = aUser.getUserID();        String sql = "DELETE FROM UserDT"+"WHERE userID = ‘"+userID+"‘";        try{            int result = aStatement.executeUpdate(sql);        }        catch (SQLException e){            System.out.println(e);        }    }    public static void update(User aUser) throws NotFoundException    {        userID=aUser.getUserID();        name=aUser.getName();     //全部轉化為字串類型        passWord=aUser.getPassWord();        gender=aUser.getGender();        image=aUser.getImage();        isFrozen= ""+aUser.getIsFrozen();            String sql="Update AnswerT SET isFrozen = ‘"+isFrozen+"‘,"+        " Name = ‘"+name+"‘,"+        " PassWord = ‘"+passWord+"‘,"+        " Gender = ‘"+gender+"‘,"+        " Image = ‘"+image+"‘,"+                "WHERE id=‘"+userID+"‘";        System.out.println(sql);        try        {            int result=aStatement.executeUpdate(sql);        }        catch(SQLException e)        {            System.out.println(e);        }        }}

java項目串連access資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.