Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.student;
- import java.sql.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class DBConnection {
- private Connection conn;
- private PreparedStatement stm;
- private String connString = "jdbc:mysql://localhost/jsp";
- private String userName = "root";
- private String password = "11111";
- public DBConnection() {
- }
- public int executeUpdate(String SQLQuery, Object... objs) {
- try {
- return execute(SQLQuery, objs).executeUpdate();
- } catch (SQLException ex) {
- Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
- return -1;
- }
- }
- private PreparedStatement execute(String SQLQuery, Object... objs) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(connString, userName, password);
- } catch (Exception ex) {
- Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- stm = conn.prepareStatement(SQLQuery);
- } catch (SQLException ex) {
- Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- for (int i = 0; i < objs.length; i++) {
- try {
- stm.setObject(i + 1, objs[i]);
- } catch (SQLException ex) {
- Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
- return null;
- }
- }
- return stm;
- }
- public ResultSet executeQuery(String SQLQuery, Object... objs) {
- try {
- return execute(SQLQuery, objs).executeQuery();
- } catch (SQLException ex) {
- Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
- return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment