Advertisement
Guest User

Untitled

a guest
Jun 16th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.77 KB | None | 0 0
  1. package sample;
  2.  
  3. import com.sun.rowset.CachedRowSetImpl;
  4.  
  5. import java.sql.*;
  6.  
  7. public class DBUtil {
  8.     private static final String url = "jdbc:postgresql://localhost:5555/student";
  9.     private static final String user = "student";
  10.     private static final String password = "wsiz#1234";
  11.     private static final String JDBC_DRIVER = "org.postgresql.Driver";
  12.     private static Connection conn = null;
  13.  
  14.  
  15.     /**
  16.      * Connect to the PostgreSQL database
  17.      *
  18.      * @return a Connection object
  19.      */
  20.     public static Connection dbConnect() throws ClassNotFoundException {
  21.  
  22.         try {
  23.             Class.forName(JDBC_DRIVER);
  24.         } catch (ClassNotFoundException e) {
  25.             System.out.println("Gdzie jest twój Sterownik JDBC ?");
  26.             e.printStackTrace();
  27.             throw e;
  28.         }
  29.  
  30.         try {
  31.             conn = DriverManager.getConnection(url, user, password);
  32.             System.out.println("Connected to the PostgreSQL server successfully.");
  33.         } catch (SQLException e) {
  34.             System.out.println(e.getMessage());
  35.         }
  36.  
  37.         return conn;
  38.     }
  39.  
  40.     public static void dbDisconnect() throws SQLException {
  41.         try {
  42.             if (conn != null && !conn.isClosed()) {
  43.                 conn.close();
  44.                 System.out.println("Disconnected with the PostgreSQL server successfully.");
  45.             }
  46.         }catch (SQLException e){
  47.             throw e;
  48.         }
  49.     }
  50.     // Wykonanie zapytania do bazy danych
  51.     public static ResultSet dbExecuteQuery(String queryStmt) throws SQLException, ClassNotFoundException {
  52.         Statement stmt = null;
  53.         ResultSet resultSet = null;
  54.         CachedRowSetImpl crs = null;
  55.         try {
  56.             // Połączenie z bazą danych, utworzenie zapytaia, wykonanie zapytania
  57.             dbConnect();
  58.             stmt = conn.createStatement();
  59.             resultSet = stmt.executeQuery(queryStmt);
  60.             crs = new CachedRowSetImpl();
  61.             crs.populate(resultSet);
  62.         }catch (SQLException e){
  63.             System.out.println("Problem accurred at executeQuery operation: " + e);
  64.             throw  e;
  65.         }finally {
  66.             if (resultSet != null) resultSet.close();
  67.             if (stmt != null) stmt.close();
  68.             dbDisconnect();
  69.         }
  70.         return crs;
  71.     }
  72.     // Wykonanie aktualizacji do bazy danych
  73.     public static void dbExecuteUpdate (String sqlStmt) throws SQLException, ClassNotFoundException {
  74.         Statement stmt = null;
  75.         try {
  76.             dbConnect();
  77.             stmt = conn.createStatement();
  78.             stmt.executeUpdate(sqlStmt);
  79.         }catch (SQLException e) {
  80.             System.out.println("Problem accured at executeUpdate operation: " + e);
  81.             throw e;
  82.         }finally {
  83.             if (stmt != null){
  84.                 stmt.close();
  85.             }
  86.             dbDisconnect();
  87.         }
  88.     }
  89.     // Wykonanie zapytania do bazy danych
  90.     public static ResultSet  dbPreparedStatementExecuteQuery(String sqlstmt) throws SQLException, ClassNotFoundException {
  91.         PreparedStatement pst = null;
  92.         ResultSet rs = null;
  93.         CachedRowSetImpl crs = null;
  94.         try {
  95.             dbConnect();
  96.             pst = conn.prepareStatement(sqlstmt);
  97.             rs = pst.executeQuery();
  98.             crs = new CachedRowSetImpl();
  99.             crs.populate(rs);
  100.         }catch (SQLException e) {
  101.             System.out.println("Problem accured at executeQuery (PreparedStatement): \n" + e);
  102.             throw e;
  103.         }finally {
  104.             if (pst != null)pst.close();
  105.             dbDisconnect();
  106.         }
  107.         return crs;
  108.     }
  109.  
  110.     public static void getActors() {
  111.  
  112.         String SQL = "SELECT * FROM serwispc1.czesci";
  113.  
  114.         try (Statement stmt = conn.createStatement();
  115.              ResultSet rs = stmt.executeQuery(SQL)) {
  116.             // display actor information
  117.             displayParts(rs);
  118.         } catch (SQLException ex) {
  119.             System.out.println(ex.getMessage());
  120.         }
  121.     }
  122.  
  123.     private static void displayParts(ResultSet rs) throws SQLException {
  124.         while (rs.next()) {
  125.             System.out.println(
  126.                             rs.getString("id_czesc") + "\t" +
  127.                             rs.getString("nazwa") + "\t" +
  128.                             rs.getString("koszt") + "\t" +
  129.                             rs.getString("czasnaprawy") +  "\t" +
  130.                             rs.getString("sposobnaprawy"));
  131.  
  132.         }
  133.     }
  134.  
  135.     /**
  136.      * @param args the command line arguments
  137.      */
  138.     public static void main(String[] args) throws ClassNotFoundException, SQLException {
  139.         DBUtil dbUtil = new DBUtil();
  140.         dbUtil.dbConnect();
  141.         dbUtil.getActors();
  142.         dbUtil.dbDisconnect();
  143.  
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement