Today, a student asked me how to connect to the access database in java, so I wrote an example. It is not difficult to write jsp code. An exception has plagued me for a long time, because I have never used ODBC to connect to the database and it is not very useful. This is the exception. java-SQL-sqlexception-microsoft-odbc-driver-manager-data-source-name-not-foun
Today, a student asked me how to connect to the access database in java, so I wrote an example. It is not difficult to write jsp code. An exception has plagued me for a long time, because I have never used ODBC to connect to the database and it is not very useful. This is the exception. java-SQL-sqlexception-microsoft-odbc-driver-manager-data-source-name-not-foun
Today, a student asked me how to connect to the access database in java, so I wrote an example. It is not difficult to write jsp code. An exception has plagued me for a long time, because I have never used ODBC to connect to the database and it is not very useful.
This is the exception. java-SQL-sqlexception-microsoft-odbc-driver-manager-data-source-name-not-found indicates that the data source cannot be found.
The following describes the procedure.
1. Open access and create databases and tables
My database name is test, and the table name is user.
Finally, save the file ending with. mdb to the desktop (I saved it to the desktop because of testing)
2. Open the computer control panel, set the display mode to a small icon, and find ODBC
Click ODBC, then click System DSN, click add, and select Microsoft access Driver.
3. Enter the data source name and write a test (this should be the test in "jdbc: odbc: test" in the java code)
Select test. mdb saved in the first part, click "advanced" to set the user name and password, and click "OK ".
4. The above data source is configured. See the jsp code below.
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ page import =" java. SQL. * "%> <% String path = request. getContextPath (); %><% String url = "jdbc: odbc: test"; String SQL = "select * from user"; Class. forName ("sun. jdbc. odbc. jdbcOdbcDriver "); Connection conn = DriverManager. getConnection (url, "linjian", "110110"); // enter the username and password you set in step 3. prepareStatement (SQL); ResultSet rs = ps.exe cuteQuery (); %>
Id |
Age |
Username |
<% While (rs. next () {%>
<% = Rs. getString (1) %> |
<% = Rs. getString (2) %> |
<% = Rs. getString (3) %> |
<%} Conn. close (); %>
Because ODBC is used to connect to the database, the corresponding sun. jdbc. odbc. JdbcOdbcDriver is available in rt. jar in jdk, so no third-party jar package is required.
The following is a step image that combines the above text.