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).
4. Click the fourth item to add a data source and select Access Driver.
#5. Form a binding relationship between the data source and the Access file you created.
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(); } } }
7. The running effect (the database created by each person is different, and the running effect is also different)
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!