Java project to connect to an Access database

Source: Internet
Author: User
Tags key string access database

1. Import Access_jdbc30.jar into the project

Jar package Baidu Cloud Link: Https://pan.baidu.com/s/10HFM3HomMArvfHjklA_1MA Password: 0qxp

Project name-"Right-click" Build path-"add extra jar-" Access_jdbc30.jar

2. The code is as follows:

Package Userpackage;import Java.sql.*;import topicpackage.*;import actorpackage.*;import administratorpackage.*; Import Answerpackage.*;import exceptionpackage.*;p ublic class Userda {//connection DB public static connection Initia Lize () {try {//loading Jdbc-odbc bridge driver try{class.forname ("Com.hxtt.sql.ac Cess.        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; }/* FREE resources */public static void Terminate () {try{aStAtement.close ();        Aconnection.close ();        } catch (SQLException e) {System.out.println (e); } */* Add a new record */public static void Add (User auser) throws duplicateexception{String sql = "INSERT INT        O Usert "+" VALUES (' "+userid+" ', ' "+name+" ', ' "+password+" ', ' "+gender+" ', ' "+image+" ', ' "+isfrozen+" ') ";                SYSTEM.OUT.PRINTLN (SQL);            try{User C = Find (UserID);        Throw (New Duplicateexception ("The user already exists"));            } 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 Ans            Wer 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 ("No Record Found"));                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 ();        Convert all to String type 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 project to connect to an Access database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.