SHARE
TWEET

Untitled

a guest Dec 6th, 2016 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ch12;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. public class database {
  17.  
  18.     //class to ask the DB what version it uses  
  19.     protected static void version() {
  20.    
  21.        
  22.         String query = "SELECT VERSION()";
  23.         database.conn(query);
  24.        
  25.     }
  26.    
  27.     protected static void clean() {
  28.        
  29.        
  30.     }
  31.    
  32.     //DB Connection Class (postgres SQL)
  33.     private static void conn( String query ) {
  34.         try {
  35.             Class.forName("org.postgresql.Driver");
  36.         } catch (ClassNotFoundException ex)
  37.                  {
  38.             Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex);
  39.         }
  40.                
  41.         //set all objects to null        
  42.         Connection con = null;
  43.         Statement st = null;
  44.         ResultSet rs = null;
  45.  
  46.         //config
  47.         String url = "jdbc:postgresql://localhost/java";
  48.         String user = "postgres";
  49.         String pass = "postgres";
  50.        
  51.        
  52.         //try to establish connection
  53.         try {
  54.  
  55.             //connect using credentials in config
  56.             con = DriverManager.getConnection(url, user, pass);
  57.             //create statement structure w/ con details
  58.             st = con.createStatement();
  59.             //inject con details + statement structure, apply statement and execute
  60.             rs = st.executeQuery(query);
  61.  
  62.             //if result is fileld, print it
  63.             if (rs.next()) {
  64.                 System.out.println(rs.getString(1));
  65.             }
  66.  
  67.         } //if shit goes down, log the error and break
  68.         catch (SQLException ex) {
  69.             System.out.println("oh shit");
  70.             Logger lgr = Logger.getLogger(database.class.getName());
  71.             lgr.log(Level.SEVERE, ex.getMessage(), ex);
  72.         }
  73.  
  74.     }
  75.  
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top