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!
1. Configure the data source
Go to the C:\Windows\System32
folder and find the odbcad32.exe
file, double-click it.
must correspond to the data source name in the code.
##If you want to set an account name and password:
DriverManager.getConnection("jdbc:odbc:Card","账户名","密码");
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.7Access Tutorial"
Access database cannot use jdbc-odbc in JDK1.8 version and later
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
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.
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!