Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ch12;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class database {
- //class to ask the DB what version it uses
- protected static void version() {
- String query = "SELECT VERSION()";
- database.conn(query);
- }
- protected static void clean() {
- }
- //DB Connection Class (postgres SQL)
- private static void conn( String query ) {
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException ex)
- {
- Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex);
- }
- //set all objects to null
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
- //config
- String url = "jdbc:postgresql://localhost/java";
- String user = "postgres";
- String pass = "postgres";
- //try to establish connection
- try {
- //connect using credentials in config
- con = DriverManager.getConnection(url, user, pass);
- //create statement structure w/ con details
- st = con.createStatement();
- //inject con details + statement structure, apply statement and execute
- rs = st.executeQuery(query);
- //if result is fileld, print it
- if (rs.next()) {
- System.out.println(rs.getString(1));
- }
- } //if shit goes down, log the error and break
- catch (SQLException ex) {
- System.out.println("oh shit");
- Logger lgr = Logger.getLogger(database.class.getName());
- lgr.log(Level.SEVERE, ex.getMessage(), ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement