Gets the tool class for the database connection object

Source: Internet
Author: User
Tags stmt

MySQL Connection object

Jdbc.driver=com.mysql.jdbc.driver
Files found by jdbc.url=jdbc:mysql://localhost:3306/
Jdbc.user=root
jdbc.password= The password you set yourself

Oracle Connection Object

Jdbc.driver=oracle.jdbc.driver.oracledriver
Jdbc.url=jdbc:oracle:thin: @localhost: 1521:ORCL
Jdbc.user=scott
jdbc.password= The password you set yourself

Gets the tool class for the database connection object (made into a package to make it easy to call)

Package Com.etc.untl;import Java.sql.*;import Java.util.properties;import java.io.filenotfoundexception;import Java.io.filereader;import java.io.ioexception;/** * Tool class to get database connection objects * @author Administrator * @version 1.0 */public Class JD Bcuntl {private static string Driverclass = null;private static string url = null;private static string user = Null;privat e static String password = null;//Gets the database-driven information in the jdbc.properties through a static block and initializes the static member variable static{properties props = new Properties (); try {props.load (new FileReader ("Jdbc.properties"));d Riverclass = Props.getproperty ("Jdbc.driver"); URL = Props.getproperty ("Jdbc.url"), user = Props.getproperty ("Jdbc.user");p Assword = Props.getproperty ("Jdbc.password");} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}} /** * Creates a database connection object based on the database drive information obtained and returns * @return Connection object * @throws Exception */public static Connection getconnection () throws Excep Tion{connection conn = null; Class.forName (driverclass); conn = Drivermanager.getconnection (url, user, password); return conn;} /** * Method of unifying JDBC Resources off * @param RS Result set object * @param stmt Statement Object * @param conn Connection object * @throws SQLException */public static void cl OSE (ResultSet rs,statement stmt,connection conn) throws Sqlexception{if (rs! = null) {Rs.close (); rs = null;} if (stmt! = null) {stmt.close (); stmt = null;} IF (conn! = null) {conn.close (); conn = null;}}}

  

Gets the tool class for the database connection object

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.