Java uses JDBC to connect to the arbitrary type database (MySQL Oracle. )

Source: Internet
Author: User

Package Cn.liz.test;import Java.io.inputstream;import Java.sql.connection;import java.sql.driver;import Java.sql.sqlexception;import Java.util.properties;import Org.junit.test;public class JBDCtest {/** * Write a common method,  Without altering the source program, can get the connection of any database * solution: The database-driven Driver implementation of the class name, URL, user, password into a * configuration file, by changing the configuration file implementation and detailed database decoupling. * @throws Exception */public Connection getconnection () throws exception{string Driverclass = null; String jdbcurl = null; String user = null; String password = null;//reads the jdbc.properties file under the classpath inputstream in = GetClass (). getClassLoader (). getResourceAsStream (" Jdbc.properties "); Properties Properties = new properties ();p roperties.load (in);d Riverclass = Properties.getproperty ("Driver"); Jdbcurl = Properties.getproperty ("Jdbcurl"), user = Properties.getproperty ("user");p Assword = Properties.getproperty (" Password ");//By reflecting common Driver objects. Driver Driver = (Driver) class.forname (Driverclass). newinstance (); Properties Info = new properties (), Info.put ("user", user), Info.put ("PassWOrd ", password);//Get a database connection through the Driver connect method. Connection Connection = Driver.connect (Jdbcurl, info); return Connection;} @Testpublic void Testgetconnection () throws Exception{system.out.println (Getconnection ());}} <span style= "White-space:pre" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" >jdbc.properties</span> configuration file:
#driver =oracle.jdbc.driver.oracledriver#jdbcurl=jdbc:oracle:thin: @localhost: 1521:orcl#user=scott#password= javadriver=com.mysql.jdbc.driverjdbcurl=jdbc:mysql://localhost:3306/testuser=rootpassword=

Java uses JDBC to connect to the arbitrary type database (MySQL Oracle. )

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.