A simple example of Java Access Database reading

Source: Internet
Author: User

simple as it is, for beginners, if there is no Code that can be successfully executed, really difficult to debug
use access to create a database db1.mdb. There is a table named "Table1" with the fields "name" and "Age".
just enter a few records, save it and create a system data source "Access2000" in ODBC to connect to this database
then you can use the following code to display all records in the database
database. java
------------------
Import Java. SQL. *;

Class database {
Public static void main (string ARGs []) {
Try {
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
String url = "JDBC: ODBC: Access2000 ";
Connection connection = drivermanager. getconnection (URL );
Statement statement = connection. createstatement ();
String SQL = "select * From Table1 ";
Resultset rs = statement.exe cutequery (SQL );
String tt;
While (Rs. Next ()){
System. Out. Print ("name:" + Rs. getstring ("name "));
System. Out. println ("Age:" + Rs. getstring ("Age "));
}
Rs. Close ();
Connection. Close ();
}
Catch (exception ex ){
System. Out. println (Ex );
System. Exit (0 );
}
}
}
------------
OK, compile and run:
E:/Java> javac database. Java

E:/Java> Java Database

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.