Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application.dao;
- import application.configuration.Configuration;
- import org.apache.log4j.Logger;
- import java.sql.*;
- import java.time.LocalDate;
- public class BaseDAO {
- private final static Logger LOGGER = Logger.getLogger(BaseDAO.class);
- private final static String DBURL = "jdbc:mysql://" + Configuration.database + "/baza?autoReconnect=true&useSSL=false";
- // private final static String DBUSER = "iemz";
- // private final static String DBPASS = "*I3m7#";
- private final static String DBUSER = "root";
- private final static String DBPASS = "root";
- private final static String DBDRIVER = "com.mysql.jdbc.Driver";
- public Connection connect() {
- Connection conn = null;
- try {
- //STEP 1: Register JDBC driver
- Class.forName(DBDRIVER);
- //STEP 2: Open a connection
- conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
- return conn;
- } catch (ClassNotFoundException | SQLException e) {
- LOGGER.error("Błąd utworzenia połączenia\n", e);
- e.printStackTrace();
- }
- return null;
- }
- public void queryFinish(Connection con, ResultSet rs) {
- try {
- if (rs != null) {
- rs.close();
- }
- } catch (SQLException se2) {
- }// nothing we can do
- try {
- if (con != null) {
- con.close();
- }
- } catch (SQLException se) {
- LOGGER.error("Błąd zamykania połączenia\n", se);
- se.printStackTrace();
- }
- }
- public static void setNullOrString(PreparedStatement pstmt, int column, String value) throws SQLException {
- if (value != null && !value.trim().isEmpty() && !value.equals("null")) {
- pstmt.setString(column, value);
- } else {
- pstmt.setNull(column, Types.VARCHAR);
- }
- }
- public static void setNullOrDate(PreparedStatement pstmt, int column, LocalDate value) throws SQLException {
- if (value != null) {
- pstmt.setString(column, value.toString());
- } else {
- pstmt.setNull(column, Types.DATE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement