Several Methods for connecting to the database using JDBC and jdbc

Source: Internet
Author: User

Several Methods for connecting to the database using JDBC and jdbc

I. Oldest method (directly connect to the database through the Driver Interface)

 

Instance code:

1 @ Test 2 public void testDriver () throws SQLException {3 // 1. create a Driver implementation Class Object 4 Driver dirver = new com. mysql. jdbc. driver (); 5 6 // 2. prepare the basic information for connecting to the database: url, user, password 7 String url = "jdbc: mysql: // 127.0.0.1: 3306/java_jdbc"; 8 Properties info = new Properties (); 9 info. put ("user", "root"); 10 info. put ("password", "123456"); 11 12 // 3. call the connect (url, info) on the Driver excuse to obtain the database Connection 13 connection Connection = dirver. connect (url, info); 14 System. out. println (connection); 15}
View Code

 

 

2. Compile a general method (throughDriver Interface implementation)

 

Instance code:

1 public Connection getConnection () throws Exception {2 String driverClass = null; 3 String jdbcUrl = null; 4 String user = null; 5 String password = null; 6 7 // read jdbc in the class path. properties file 8 InputStream in = getClass (). getClassLoader (). getResourceAsStream ("jdbc. properties "); 9 Properties properties = new Properties (); 10 properties. load (in); 11 12 driverClass = properties. getProperty ("driver"); 13 jdbcUrl = properties. getProperty ("jdbcUrl"); 14 user = properties. getProperty ("user"); 15 password = properties. getProperty ("password"); 16 17 // create the Driver object 18 Driver driver = (Driver) Class through reflection. forName (driverClass ). newInstance (); 19 20 Properties info = new Properties (); 21 info. put ("user", user); 22 info. put ("password", password); 23 24 // connect to the database through the connect Method of the Driver. 25 Connection connection = driver. connect (jdbcUrl, info); 26 27 return connection; 28} 29 30 @ Test31 public void testGetConnection () throws Exception {32 System. out. println (getConnection (); 33}
View Code

 

3. Obtain the database connection through the DriverManager driver management class

 

 

 

1). Create a properties object

1 Properties properties = new Properties();

 

2). Obtain the input stream corresponding to jdbc. properties.

1 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");

 

3) load the corresponding input stream

1     properties.load(inputStream);

 

4). The user, password, url, and driver strings are determined.

1 String driver = properties.getProperty("driver");2 String jdbcUrl = properties.getProperty("jdbcUrl");3 String user = properties.getProperty("user");4 String password = properties.getProperty("password");

2. Load the database driver

1 Class.forName(driver);

 

3. Get the database connection through the getConnection () method of Drivermanager

1 return DriverManager.getConnection(jdbcUrl, user, password);

 

 

Instance code:

 1     @Test 2     public void testGetConnection23() throws Exception { 3         System.out.println(getConnection2()); 4     } 5     public Connection getConnection2() throws IOException, ClassNotFoundException, SQLException{ 6          7         Properties properties = new Properties(); 8          9         InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");10         11         properties.load(inputStream);12         13         String driver = properties.getProperty("driver");14         String jdbcUrl = properties.getProperty("jdbcUrl");15         String user = properties.getProperty("user");16         String password = properties.getProperty("password");17         18         Class.forName(driver);19         20         return DriverManager.getConnection(jdbcUrl, user, password);21     }
View Code

 

This article is just the author's note and is for reference only. If you have any mistakes, please note it! 2017-10-28 11:14:03

 

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.