Java learning: JDBC connection example

Source: Internet
Author: User

The most common database connection technology in Java isJDBC.

At present, almost all mainstream databases provide corresponding JDBC drivers, which can be simply understood.. NET database client DLL ,. net. If netframework does not support databases by default (such as sqllite and MySQL), you must download a dedicated database client DLL file and add a reference to this DLL in vs.net, then you can use it to connect to the database.

JDBC is also the same. After JDK is installed by default, there is almost no JDBC driver. You need to download the corresponding JDBC driver from the website of each database provider. Take MYSQL as an example, download MySQL JDBC Driver (Baidu search "MySQL JDBC" on the line), will get a mysql-connector-java-5.1.22-bin.jar file,Copy it to the \ JDK \ JRE \ Lib \ ext directory(This wayProgramWhen running, you can find the file through classpath)

Then you can writeCodeConnection, sample code:

Package jmyang. jdbctest; import Java. SQL. drivermanager; import Java. SQL. resultset; import Java. SQL. statement; import Java. SQL. connection; public class mysqltest {public static void main (string [] ARGs) {connection conn = NULL; try {// load the MySQL Driver Class. forname ("com. mySQL. JDBC. driver "); // connect to MySQL on localhost and specify to use the test database. The username is root and the password is *** conn = drivermanager. getconnection ("JDBC: mysql: // localhost/test ", "Root", "***"); If (! Conn. isclosed () {system. Out. println ("database connection successful! "); // Verify whether the connection is successful} statement = Conn. createstatement (); // query data resultset rs = statement.exe cutequery ("select * From person"); // output result set (similar. dataset/datatable) while (RS. next () {system. out. println ("ID =" + Rs. getint ("ID") + ", name =" + Rs. getstring ("name");} Rs. close ();} catch (exception e) {e. printstacktrace ();} finally {If (Conn! = NULL) {try {conn. Close (); Conn = NULL;} catch (exception e) {e. printstacktrace ();}}}}}

The following is an example of Oracle 11g express connection:

After Oracle 11g is installed, there will be three packages under the JDBC Directory: ojdbc5.jar, ojdbc6.jar, and ojdbc6_g.jar. ojdbc5 is applicable to JDK and later versions, and the other two are applicable to JDK 1.6 and later versions.

Note: In my actual test, if you cannot figure out your JDK version, you can copy both ojdbc5.jar and ojdbc6.jar files to \ JDK \ JRE \ Lib \ Ext.

 
Class. forname ("oracle. JDBC. driver. oracledriver "); // connect to Oracle on localhost. The username is yangjm, the password is ***, the SID is Xe, the port number is 1521, And the IP address of the Oracle server is localhost conn = drivermanager. getconnection ("JDBC: oracle: thin: @ // localhost: 1521/XE", "yangjm ","***");

The difference with the MySQL sample code is that the loaded driver is different and the URL specified for getconnection is different.

 

Related Article

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.