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