Home >Topics >Access >Let's talk about how to configure ODBC in Java and connect to Access database

Let's talk about how to configure ODBC in Java and connect to Access database

青灯夜游
青灯夜游forward
2022-03-30 20:13:452698browse

This article introduces how to configure the ODBC data source and use ODBC to connect to the Access database in Java. I hope it will be helpful to everyone!

Let's talk about how to configure ODBC in Java and connect to Access database

Java uses ODBC to connect to the Access database

1. Configure the data source

Go to the C:\Windows\System32 folder and find the odbcad32.exe file, double-click it.

Lets talk about how to configure ODBC in Java and connect to Access database

Lets talk about how to configure ODBC in Java and connect to Access database

Lets talk about how to configure ODBC in Java and connect to Access database

must correspond to the data source name in the code.

Lets talk about how to configure ODBC in Java and connect to Access database

Lets talk about how to configure ODBC in Java and connect to Access database

Lets talk about how to configure ODBC in Java and connect to Access database

##If you want to set an account name and password:

DriverManager.getConnection("jdbc:odbc:Card","账户名","密码");

Lets talk about how to configure ODBC in Java and connect to Access database

2. Run the test

#If the following situation occurs, it means that you are using JDK1.7 or above, and you need to change the JDK Just change to 1.7


Related recommendations: "

Access Tutorial"

Access database cannot use jdbc-odbc in JDK1.8 version and later

java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

Test code:

import java.sql.*;public class TestMain{
	public static void main(String[] args){
		Connection con;
		Statement sql;
		ResultSet rs;
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// 注意这里Card就是上一步设置的数据源名称
			con = DriverManager.getConnection("jdbc:odbc:Card","","");
			sql = con.createStatement();
			rs = sql.executeQuery("SELECT * FROM Card");

			while(rs.next()){
				String name = rs.getString("Name");
				String cardId = rs.getString("CardID");
				String sex = rs.getString("Sex");
				System.out.println("Name="+name+"CardID="+cardId+"Sex="+sex);
			}

			con.close();
		} catch(Exception e){
			System.out.println(e);
		}
	}}
The data output test passed.


Lets talk about how to configure ODBC in Java and connect to Access database

For more programming-related knowledge, please visit:

Programming Teaching! !

The above is the detailed content of Let's talk about how to configure ODBC in Java and connect to Access database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete