Java Connection database (properties configuration file) __java

Source: Internet
Author: User

Database connection Tool Class

public class Dbutil {//Get input stream
private static InputStream in = DBUtil.class.getClassLoader (). getResourceAsStream ("config.properties");
private static Properties Prop = new properties ();



public static Connection getconnection () throws ClassNotFoundException, sqlexception{
try {
Prop.load (in);
catch (IOException e) {

E.printstacktrace ();
}
Connection Conn=null;
Load Driver
String driver=prop.getproperty ("Jdbc.driverclassname");//"Oracle.jdbc.OracleDriver";
Class.forName (driver);
Establish a connection
String url=prop.getproperty ("Jdbc.url");//"Jdbc:oracle:thin:@10.1.55.241:1521:test";
String username=prop.getproperty ("Jdbc.username");
String password=prop.getproperty ("Jdbc.password");
Conn=drivermanager.getconnection (Url,username,password);
Return conn;

}

}

My config.properties is placed under the SRC directory of the Web project.

Properties configuration file Contents

Jdbc.driverclassname=oracle.jdbc.driver.oracledriver
Jdbc.url=jdbc:oracle:thin:@10.1.55.241:1521:test
Jdbc.username=bomc
Jdbc.password=bomc
Jdbc.show_sql=true

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.