Guest User

Untitled

a guest
Oct 30th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.sql.*;
  5. import java.util.Properties;
  6.  
  7. public class DatabaseConnectionWithProperties {
  8.  
  9.     public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
  10.  
  11.         FileInputStream fileInputStream = new FileInputStream("connection.properties");
  12.         Properties properties = new Properties();
  13.         properties.load(fileInputStream);
  14.         String DB_URL = (String) properties.get ("DB_URL");
  15.         String DB_USER = (String) properties.get("DB_USER");
  16.         String DB_PASS = (String) properties.get("DB_PASS");
  17.         Class.forName(DB_URL);
  18.         Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
  19.         Statement statement = connection.createStatement();
  20.         ResultSet resultSet = statement.executeQuery("select * from conferencedb");
  21.         while (resultSet.next()) {
  22.             System.out.println(resultSet.getInt(1)+ " " + resultSet.getString(2)+ " " + resultSet.getString(3));
  23.         }
  24.         resultSet.close();
  25.         statement.close();
  26.         connection.close();
  27.     }
  28. }
Add Comment
Please, Sign In to add comment