java项目连接access数据库

Posted YuQiao0303

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java项目连接access数据库相关的知识,希望对你有一定的参考价值。

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数据库的主要内容,如果未能解决你的问题,请参考以下文章

Java Web项目中连接Access数据库的配置方法

java连接access数据库的三种方式以及远程连接

在 java 中与 MS Access 数据库建立 SQL 连接时找不到合适的驱动程序

Java连接Access数据库改成MySQL连接数据库

java中执行access数据库插入错误!!!急!!!

使用 WPF 成功连接后无法从 Access 数据库获取数据