Java connection to access database

Source: Internet
Author: User
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 (); %>
 
 <% While (rs. next () {%> 
   <%} Conn. close (); %> 
  
Id Age Username
<% = Rs. getString (1) %> <% = Rs. getString (2) %> <% = Rs. getString (3) %>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.