JDBC database connection tool and Access database connection example

Source: Internet
Author: User

1. When JDBC is connected to each database, there are many identical code parts. Write these parts separately in a class, connect to a specific database based on input parameters such as driver, URL, user, and password. The tool class code is as follows:

Package mine. util. database; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. sqlexception; import Java. SQL. statement;/*** this tool class is used for database connection. A statement object is returned, you can execute an SQL statement to obtain the result */public class databaseconnection {public static final string access_driver = "Sun. JDBC. ODBC. jdbcodbcdriver "; public static final string mysql_driver =" com. mySQL. JDBC. driver "; public static final string marshl E_driver = "oracle. JDBC. driver. oracledriver "; public static final string sqlserver_driver =" com. microsoft. sqlserver. JDBC. sqlserverdriver "; private statement ST = NULL; private connection = NULL; public statement getstatement (string driver, string URL, string user, string password) {try {// 1. Load the driver class. forname (driver); // 2. Establish a connection through the URL and connect to the database connection = drivermanager. getconnection (URL, user, Password); // 3. Create a statement. The connection can be seen in the statement, and the statement can be seen in the cable car ST = connection. createstatement ();} catch (classnotfoundexception e) {e. printstacktrace ();} catch (sqlexception e) {e. printstacktrace ();} return st;} public void close () {try {If (st! = NULL) ST. Close (); If (connection! = NULL) connection. Close () ;}catch (sqlexception e) {e. printstacktrace ();}}}


 

2, the following using the JDBC-ODBC Bridge Connection Access database to test this tool class

 

First, configure the ODBC source as follows:

 

 

 

The St table information in the data source student. mdb is as follows:

 

The Code is as follows:

 

Package demo. database; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement; import mine. util. database. databaseconnection;/** use the tool databaseconnection to connect to the ACCESS database example */public class accessdemo {public void connect () {databaseconnection dbconn = new databaseconnection (); // return the statement object statement ST = dbconn. getstatement (databaseconnection. access_driver, "JDBC: ODBC: Access", "", ""); resultset rs = NULL; try {// execute the SQL statement rs = st.exe cutequery ("select * from St"); // obtain the information in the result set while (RS. next () {// note that Rs currently points to null. You must first move next () to point to the first record system. out. println (RS. getstring ("Sno") + "" + Rs. getstring ("sname") + "" + Rs. getstring ("ssex") + "" + Rs. getint ("Sage") + "" + Rs. getstring ("sdept");} Rs. close ();} catch (sqlexception e) {e. printstacktrace ();} dbconn. close ();} public static void main (string [] ARGs) {New accessdemo (). connect ();}}


 

Running result:

Test Complete

 

3. Common Database URLs

 

4. Database Data Types and corresponding Java Data Types

 

 

 

 

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.