Guest User

Untitled

a guest
Jan 4th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. package com.student;
  2.  
  3. import java.sql.*;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6.  
  7. public class DBConnection {
  8.  
  9.   private Connection conn;
  10.   private PreparedStatement stm;
  11.   private String connString = "jdbc:mysql://localhost/jsp";
  12.   private String userName = "root";
  13.   private String password = "11111";
  14.  
  15.   public DBConnection() {
  16.  
  17.   }
  18.  
  19.   public int executeUpdate(String SQLQuery, Object... objs) {
  20.     try {
  21.       return execute(SQLQuery, objs).executeUpdate();
  22.     } catch (SQLException ex) {
  23.       Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  24.       return -1;
  25.     }
  26.   }
  27.  
  28.   private PreparedStatement execute(String SQLQuery, Object... objs) {
  29.     try {
  30.       Class.forName("com.mysql.jdbc.Driver");
  31.       conn = DriverManager.getConnection(connString, userName, password);
  32.     } catch (Exception ex) {
  33.       Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  34.     }
  35.     try {
  36.       stm = conn.prepareStatement(SQLQuery);
  37.     } catch (SQLException ex) {
  38.       Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  39.     }
  40.     for (int i = 0; i < objs.length; i++) {
  41.       try {
  42.         stm.setObject(i + 1, objs[i]);
  43.       } catch (SQLException ex) {
  44.         Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  45.         return null;
  46.       }
  47.     }
  48.     return stm;
  49.   }
  50.  
  51.   public ResultSet executeQuery(String SQLQuery, Object... objs) {
  52.     try {
  53.       return execute(SQLQuery, objs).executeQuery();
  54.     } catch (SQLException ex) {
  55.       Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  56.       return null;
  57.     }
  58.   }
  59. }
Add Comment
Please, Sign In to add comment