Java program reads the properties file

Source: Internet
Author: User

Create a new database properties file Datasource.properties

Create a new test class

Import Java.io.InputStream;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.util.ArrayList;
Import java.util.List;
Import java.util.Properties;

public class Datatest {

private static String FileName = "Datasource.properties";

public static void Main (string[] args) {
Properties prop = new properties ();
try {

Read Properties File

InputStream InputStream = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream (FileName); prop.load (InputStream);///Load Property list

String name = Prop.getproperty ("name");

String url = prop.getproperty ("url");
String user = Prop.getproperty ("user");
String Password = prop.getproperty ("password");
Class.forName (name);//Get connection type
Connection con = drivermanager.getconnection (Url,user,password);//Get connection
String sql = "SELECT * from user";
PreparedStatement PST = con.preparestatement (SQL);//Prepare to execute statement
ResultSet result = Pst.executequery ();//Executes the statement to get the results set
list<string> list = new ArrayList ();
while (Result.next ()) {
List.add (result.getstring (2));
System.out.println (result.getstring (1));
System.out.println (result.getstring (2));
System.out.println (Result.getstring (3));
}
Result.close ();
} catch (Exception e) {

E.printstacktrace ();
}

}

}

Java program reads the properties file

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.