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