Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.vernost.vedatabase;
- //import com.mysql.jdbc.CallableStatement;
- import com.vernost.velogger.veLogger;
- import java.io.File;
- import java.sql.CallableStatement;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.sql.DataSource;
- //import com.mysql.jdbc.MySQLConnection;
- //import com.mysql.jdbc.Driver;
- //import logger.LogginEg;
- /**
- *
- * @author Nitin
- */
- public class Connection {
- // private static java.sql.Connection connection;
- private static String ApplicationId;
- private static DataSource ds = null;
- public static String getApplicationId() {
- return ApplicationId;
- }
- public static void setApplicationId(String ApplicationId) {
- Connection.ApplicationId = ApplicationId + ".properties";
- //veLogger.setApplicationid(ApplicationId);
- }
- public static void getApplicationName() {
- Connection.ApplicationId = ApplicationId;
- }
- public static DataSource getDataSource() {
- if (ds == null) {
- InitialContext ctx = null;
- try {
- ctx = new InitialContext();
- ds = (DataSource) ctx.lookup("jdbc/jpplqe");
- } catch (NamingException ex) {
- ex.printStackTrace();
- veLogger.addError(ex.getMessage());
- }
- return ds;
- } else {
- return ds;
- }
- }
- public java.sql.Connection open1() {
- //The JDBC Data source that we just created
- try {
- java.sql.Connection conn = getDataSource().getConnection();
- if (conn == null) {
- ds = null;
- } else if (conn.isClosed()) {
- ds = null;
- }
- return getDataSource().getConnection();
- } catch (Exception ex) {
- ex.printStackTrace();
- veLogger.addError(ex.getMessage());
- }
- return null;
- }
- public java.sql.Connection open() {
- //The JDBC Data source that we just created
- InitialContext ctx = null;
- java.sql.Connection conn = null;
- DataSource datasource;
- try {
- ctx = new InitialContext();
- datasource = (DataSource) ctx.lookup("jdbc/jpplqe");
- try {
- conn = datasource.getConnection();
- return conn;
- } catch (SQLException ex) {
- Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
- }finally{
- }
- } catch (NamingException ex) {
- ex.printStackTrace();
- veLogger.addError(ex.getMessage());
- }
- return null;
- }
- // public static java.sql.Connection open() {
- //
- // try {
- // //URL resource = getClass().getResource("/");
- // String dbconfig = "DatabaseConnection.properties";
- // if (getApplicationId() != null) {
- // dbconfig = getApplicationId();
- // }
- // veLogger.addInfo("File Path-->" + new File(dbconfig).getAbsoluteFile());
- //
- // Properties PropReader = veProperty.readProperty(dbconfig);
- // String Driver = PropReader.getProperty("driver");
- // String Url = PropReader.getProperty("connection");
- // String User = PropReader.getProperty("dbuser");
- // String Password = PropReader.getProperty("dbpassword");
- //
- // try {
- // Class.forName(Driver);
- // } catch (ClassNotFoundException e) {
- //// LogginEg.callLogger(e);
- // veLogger.addError("Where is your MySQL JDBC Driver?" + e.getMessage());
- // return null;
- // }
- //
- // veLogger.addInfo("MySQL JDBC Driver Registered!");
- // veLogger.addInfo("---Connection ---" + Url + "-" + User + "-" + Password);
- // //connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/leela_kidel3", "root", "password");
- // connection = DriverManager.getConnection(Url, User, Password);
- //// l33l@$123#
- //
- // } catch (SQLException e) {
- //// LogginEg.callLogger(e);
- // veLogger.addError("Connection Failed! Check output console" + e.getMessage());
- // e.printStackTrace();
- // return null;
- // }
- //
- // if (connection != null) {
- // veLogger.addError("You made it, take control your database now!");
- // return connection;
- // } else {
- // System.out.println("Failed to make connection!");
- // return null;
- // }
- // }
- public java.sql.Connection temp_open() {
- try {
- //URL resource = getClass().getResource("/");
- String dbconfig = "DatabaseConnection.properties";
- if (getApplicationId() != null) {
- dbconfig = getApplicationId();
- }
- veLogger.addInfo("File Path-->" + new File(dbconfig).getAbsoluteFile());
- Properties PropReader = veProperty.readProperty(dbconfig);
- String Driver = PropReader.getProperty("driver");
- String Url = PropReader.getProperty("connection");
- String User = PropReader.getProperty("dbuser");
- String Password = PropReader.getProperty("dbpassword");
- try {
- Class.forName(Driver);
- } catch (ClassNotFoundException e) {
- // LogginEg.callLogger(e);
- veLogger.addError("Where is your MySQL JDBC Driver?" + e.getMessage());
- return null;
- }
- veLogger.addInfo("MySQL JDBC Driver Registered!");
- veLogger.addInfo("---Connection ---" + Url + "-" + User + "-" + Password);
- //connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/leela_kidel3", "root", "password");
- return DriverManager.getConnection(Url, User, Password);
- // l33l@$123#
- } catch (SQLException e) {
- // LogginEg.callLogger(e);
- veLogger.addError("Connection Failed! Check output console" + e.getMessage());
- e.printStackTrace();
- return null;
- }
- }
- // public static java.sql.Connection getConnection() throws SQLException {
- // if (connection == null) {
- // open();
- // }
- // if (connection.isClosed()) {
- // open();
- // }
- // return connection;
- // }
- public static void close(java.sql.Connection connection) throws SQLException {
- connection.close();
- }
- public static String ExecuteQuery(String Query) {
- PreparedStatement statment = null;
- java.sql.Connection connection = null;
- ResultSet getKeyid = null;
- try {
- connection = new Connection().open();
- statment = connection.prepareStatement(Query);
- statment.execute();
- getKeyid = statment.getGeneratedKeys();
- String data = getKeyid.getLong(1) + "";
- return data;
- } catch (SQLException e) {
- System.out.println("Errror" + e.toString());
- return e.toString();
- }
- finally{
- try {
- getKeyid.close();
- statment.close();
- close(connection);
- } catch (SQLException ex) {
- Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static ResultSet Sql_select(String query) {
- java.sql.Connection connection = null;
- Statement st = null;
- ResultSet rs = null ;
- try {
- connection = new Connection().open();
- veLogger.addInfo("***Query-" + query);
- st = connection.createStatement();
- rs = st.executeQuery(query);
- return rs;
- } catch (Exception e) {
- // LogginEg.callLogger(e);
- veLogger.addError("//-" + query);
- return null;
- }finally{
- try {
- st.close();
- rs.close();
- close(connection);
- } catch (SQLException ex) {
- Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static ResultSet Sql_Procedure(String Query) {
- java.sql.Connection connection = null;
- CallableStatement st = null;
- ResultSet rs = null ;
- try {
- connection = new Connection().open();
- Query = "{call " + Query + "}";
- veLogger.addInfo("***Query-" + Query);
- st = (CallableStatement) connection.prepareCall(Query);
- rs = st.executeQuery(Query);
- return rs;
- } catch (Exception e) {
- // LogginEg.callLogger(e);
- e.printStackTrace();
- veLogger.addError("//-" + Query + "\n" + e.getMessage());
- return null;
- }finally{
- try {
- st.close();
- rs.close();
- close(connection);
- } catch (SQLException ex) {
- Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static CallableStatement Sql_ProcedureStatment(String Query) {
- java.sql.Connection connection = null;
- CallableStatement st = null;
- try {
- connection = new Connection().open();
- Query = "{call " + Query + "}";
- veLogger.addInfo("***Query-" + Query);
- st = (CallableStatement) connection.prepareCall(Query);
- //ResultSet rs = st.executeQuery(Query);
- //st.getMoreResults()
- //rs.next();
- return st;
- } catch (Exception e) {
- // LogginEg.callLogger(e);
- veLogger.addError("//-" + Query);
- return null;
- }finally{
- try {
- st.close();
- close(connection);
- } catch (SQLException ex) {
- Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static DataTable SelectSQL(String Query) {
- java.sql.Statement st = null;
- java.sql.Connection connection = null;
- ResultSet rs = null;
- try {
- connection = new Connection().open();
- Query = "{call " + Query + "}";
- veLogger.addInfo("***Query-" + Query);
- //CallableStatement st = (CallableStatement) connection.prepareCall(Query);
- st = connection.createStatement();
- rs = st.executeQuery(Query);
- //st.getMoreResults()
- //rs.next();
- DataTable dataTable = new DataTable();
- dataTable.fillResult(rs);
- return dataTable;
- } catch (Exception e) {
- // LogginEg.callLogger(e);
- veLogger.addError("//-" + Query + "=" + e.getMessage());
- return null;
- }finally{
- try {
- st.close();
- rs.close();
- close(connection);
- } catch (SQLException ex) {
- Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static DataTable SelectSQLQuery(String Query) {
- java.sql.Statement st = null;
- java.sql.Connection connection = null;
- ResultSet rs = null;
- try {
- connection = new Connection().open();
- veLogger.addInfo("***Query-" + Query);
- st = connection.createStatement();
- rs = st.executeQuery(Query);
- //st.getMoreResults()
- //rs.next();
- DataTable dataTable = new DataTable();
- dataTable.fillResult(rs);
- return dataTable;
- } catch (Exception e) {
- e.printStackTrace();
- veLogger.addError("//-" + Query + "=" + e.getMessage());
- return null;
- }finally{
- try {
- st.close();
- rs.close();
- close(connection);
- } catch (SQLException ex) {
- Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static DataTable SelectSQLQueryTempConnectiton(String Driver, String Url, String User, String Password, String Query) {
- java.sql.Statement st;
- java.sql.Connection tmpconnection = null;
- try {
- try {
- Class.forName(Driver);
- } catch (ClassNotFoundException e) {
- // LogginEg.callLogger(e);
- veLogger.addError("Where is your MySQL JDBC Driver?" + e.getMessage());
- return null;
- }
- tmpconnection = DriverManager.getConnection(Url, User, Password);
- //connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/leela_kidel3", "root", "password");
- st = tmpconnection.createStatement();
- ResultSet rs = st.executeQuery(Query);
- //st.getMoreResults()
- //rs.next();
- DataTable dataTable = new DataTable();
- dataTable.fillResult(rs);
- tmpconnection.close();
- return dataTable;
- } catch (Exception e) {
- e.printStackTrace();
- veLogger.addError("//-" + Query + "=" + e.getMessage());
- return null;
- }
- }
- public static File getBlob(String Query, String filename_colname, String filename_blobname) {
- ResultSet rs = Sql_select(Query);
- File file = null;
- try {
- if (rs.next()) {
- file = new File(rs.getString(filename_colname));
- FileOutputStream output = new FileOutputStream(file);
- veLogger.addInfo("Writing to file " + file.getAbsolutePath());
- while (rs.next()) {
- InputStream input = rs.getBinaryStream(filename_blobname);
- byte[] buffer = new byte[1024];
- while (input.read(buffer) > 0) {
- output.write(buffer);
- }
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- veLogger.addError(ex.getMessage());
- }
- return file;
- }
- public static String NullToString(Object value) {
- if (value == null) {
- return "";
- } else {
- return value.toString();
- }
- }
- public static void main(String[] args) throws SQLException {
- java.sql.Connection connection = new Connection().open();
- System.out.println("--Connection--" + connection);
- }
- }
Add Comment
Please, Sign In to add comment