Home > Common Problem > body text

How to correctly connect to the access database

王林
Release: 2021-01-22 10:43:30
forward
6628 people have browsed it

How to correctly connect to the access database

Due to work needs, I need to connect to the access database. It took me a long time to sort out the correct connection steps, and now I will share them with you.

(Learning video sharing: Introduction to Programming)

1. First install JDK1.7. You cannot use JDK1.8, otherwise an exception will be reported.

2. Create an Access file with the suffix (*.mdb), create a table, the table name must be an English name, and add a field, which is also an English name.

3. Find the Control Panel under (Windows 10), find the management tools, and enter the data source (as shown in the figure below).

How to correctly connect to the access database

How to correctly connect to the access database

4. Click the fourth item to add a data source and select Access Driver.

How to correctly connect to the access database

#5. Form a binding relationship between the data source and the Access file you created.

How to correctly connect to the access database

6. Add the running code

import java. sql.*;
public class Database {
    public static void main(String[] args) {

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:Database";//Database就是刚刚添加的数据源名称
            Connection con = DriverManager.getConnection(url, "", "");//账户,密码在高级选项里,默认为空
            Statement sta = con.createStatement();
            ResultSet rst = sta.executeQuery("select * from Student");//demoTable为access数据库中的一个表名
            if (rst.next()) {
                while (rst.next()) {
                    System.out.println(rst.getString("ID"));//ID为字段名称
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

7. The running effect (the database created by each person is different, and the running effect is also different)

How to correctly connect to the access database

Related recommendations: access database tutorial

The above is the detailed content of How to correctly connect to the access database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template