Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.sql.*;
- import java.util.Properties;
- public class DatabaseConnectionWithProperties {
- public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
- FileInputStream fileInputStream = new FileInputStream("connection.properties");
- Properties properties = new Properties();
- properties.load(fileInputStream);
- String DB_URL = (String) properties.get ("DB_URL");
- String DB_USER = (String) properties.get("DB_USER");
- String DB_PASS = (String) properties.get("DB_PASS");
- Class.forName(DB_URL);
- Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
- Statement statement = connection.createStatement();
- ResultSet resultSet = statement.executeQuery("select * from conferencedb");
- while (resultSet.next()) {
- System.out.println(resultSet.getInt(1)+ " " + resultSet.getString(2)+ " " + resultSet.getString(3));
- }
- resultSet.close();
- statement.close();
- connection.close();
- }
- }
Add Comment
Please, Sign In to add comment