Advertisement
Guest User

Untitled

a guest
Jan 26th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package application.dao;
  2.  
  3. import application.configuration.Configuration;
  4. import org.apache.log4j.Logger;
  5.  
  6. import java.sql.*;
  7. import java.time.LocalDate;
  8.  
  9. public class BaseDAO {
  10.  
  11.     private final static Logger LOGGER = Logger.getLogger(BaseDAO.class);
  12.  
  13.     private final static String DBURL = "jdbc:mysql://" + Configuration.database + "/baza?autoReconnect=true&useSSL=false";
  14. //    private final static String DBUSER = "iemz";
  15. //    private final static String DBPASS = "*I3m7#";
  16.  
  17.     private final static String DBUSER = "root";
  18.     private final static String DBPASS = "root";
  19.     private final static String DBDRIVER = "com.mysql.jdbc.Driver";
  20.  
  21.     public Connection connect() {
  22.  
  23.         Connection conn = null;
  24.         try {
  25.             //STEP 1: Register JDBC driver
  26.             Class.forName(DBDRIVER);
  27.             //STEP 2: Open a connection
  28.             conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
  29.             return conn;
  30.         } catch (ClassNotFoundException | SQLException e) {
  31.             LOGGER.error("Błąd utworzenia połączenia\n", e);
  32.             e.printStackTrace();
  33.         }
  34.         return null;
  35.     }
  36.  
  37.     public void queryFinish(Connection con, ResultSet rs) {
  38.         try {
  39.             if (rs != null) {
  40.                 rs.close();
  41.             }
  42.         } catch (SQLException se2) {
  43.         }// nothing we can do
  44.         try {
  45.             if (con != null) {
  46.                 con.close();
  47.             }
  48.         } catch (SQLException se) {
  49.             LOGGER.error("Błąd zamykania połączenia\n", se);
  50.             se.printStackTrace();
  51.         }
  52.     }
  53.  
  54.     public static void setNullOrString(PreparedStatement pstmt, int column, String value) throws SQLException {
  55.         if (value != null && !value.trim().isEmpty() && !value.equals("null")) {
  56.             pstmt.setString(column, value);
  57.         } else {
  58.             pstmt.setNull(column, Types.VARCHAR);
  59.         }
  60.     }
  61.  
  62.     public static void setNullOrDate(PreparedStatement pstmt, int column, LocalDate value) throws SQLException {
  63.         if (value != null) {
  64.             pstmt.setString(column, value.toString());
  65.         } else {
  66.             pstmt.setNull(column, Types.DATE);
  67.         }
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement