Guest User

Untitled

a guest
Jun 21st, 2017
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.45 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.vernost.vedatabase;
  7.  
  8. //import com.mysql.jdbc.CallableStatement;
  9. import com.vernost.velogger.veLogger;
  10. import java.io.File;
  11. import java.sql.CallableStatement;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17. import java.io.FileOutputStream;
  18. import java.io.InputStream;
  19. import java.util.Properties;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.naming.InitialContext;
  23. import javax.naming.NamingException;
  24. import javax.sql.DataSource;
  25. //import com.mysql.jdbc.MySQLConnection;
  26. //import com.mysql.jdbc.Driver;
  27. //import logger.LogginEg;
  28.  
  29. /**
  30. *
  31. * @author Nitin
  32. */
  33. public class Connection {
  34.  
  35. // private static java.sql.Connection connection;
  36. private static String ApplicationId;
  37.  
  38. private static DataSource ds = null;
  39.  
  40. public static String getApplicationId() {
  41. return ApplicationId;
  42. }
  43.  
  44. public static void setApplicationId(String ApplicationId) {
  45. Connection.ApplicationId = ApplicationId + ".properties";
  46. //veLogger.setApplicationid(ApplicationId);
  47. }
  48.  
  49. public static void getApplicationName() {
  50. Connection.ApplicationId = ApplicationId;
  51. }
  52.  
  53. public static DataSource getDataSource() {
  54. if (ds == null) {
  55. InitialContext ctx = null;
  56. try {
  57. ctx = new InitialContext();
  58. ds = (DataSource) ctx.lookup("jdbc/jpplqe");
  59. } catch (NamingException ex) {
  60. ex.printStackTrace();
  61. veLogger.addError(ex.getMessage());
  62. }
  63. return ds;
  64. } else {
  65. return ds;
  66. }
  67. }
  68.  
  69. public java.sql.Connection open1() {
  70.  
  71. //The JDBC Data source that we just created
  72. try {
  73. java.sql.Connection conn = getDataSource().getConnection();
  74. if (conn == null) {
  75. ds = null;
  76. } else if (conn.isClosed()) {
  77. ds = null;
  78. }
  79. return getDataSource().getConnection();
  80. } catch (Exception ex) {
  81. ex.printStackTrace();
  82. veLogger.addError(ex.getMessage());
  83. }
  84. return null;
  85. }
  86.  
  87. public java.sql.Connection open() {
  88.  
  89. //The JDBC Data source that we just created
  90. InitialContext ctx = null;
  91. java.sql.Connection conn = null;
  92. DataSource datasource;
  93. try {
  94. ctx = new InitialContext();
  95. datasource = (DataSource) ctx.lookup("jdbc/jpplqe");
  96. try {
  97. conn = datasource.getConnection();
  98. return conn;
  99. } catch (SQLException ex) {
  100. Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  101. }finally{
  102.  
  103. }
  104. } catch (NamingException ex) {
  105. ex.printStackTrace();
  106. veLogger.addError(ex.getMessage());
  107. }
  108.  
  109. return null;
  110. }
  111.  
  112. // public static java.sql.Connection open() {
  113. //
  114. // try {
  115. // //URL resource = getClass().getResource("/");
  116. // String dbconfig = "DatabaseConnection.properties";
  117. // if (getApplicationId() != null) {
  118. // dbconfig = getApplicationId();
  119. // }
  120. // veLogger.addInfo("File Path-->" + new File(dbconfig).getAbsoluteFile());
  121. //
  122. // Properties PropReader = veProperty.readProperty(dbconfig);
  123. // String Driver = PropReader.getProperty("driver");
  124. // String Url = PropReader.getProperty("connection");
  125. // String User = PropReader.getProperty("dbuser");
  126. // String Password = PropReader.getProperty("dbpassword");
  127. //
  128. // try {
  129. // Class.forName(Driver);
  130. // } catch (ClassNotFoundException e) {
  131. //// LogginEg.callLogger(e);
  132. // veLogger.addError("Where is your MySQL JDBC Driver?" + e.getMessage());
  133. // return null;
  134. // }
  135. //
  136. // veLogger.addInfo("MySQL JDBC Driver Registered!");
  137. // veLogger.addInfo("---Connection ---" + Url + "-" + User + "-" + Password);
  138. // //connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/leela_kidel3", "root", "password");
  139. // connection = DriverManager.getConnection(Url, User, Password);
  140. //// l33l@$123#
  141. //
  142. // } catch (SQLException e) {
  143. //// LogginEg.callLogger(e);
  144. // veLogger.addError("Connection Failed! Check output console" + e.getMessage());
  145. // e.printStackTrace();
  146. // return null;
  147. // }
  148. //
  149. // if (connection != null) {
  150. // veLogger.addError("You made it, take control your database now!");
  151. // return connection;
  152. // } else {
  153. // System.out.println("Failed to make connection!");
  154. // return null;
  155. // }
  156. // }
  157. public java.sql.Connection temp_open() {
  158.  
  159. try {
  160. //URL resource = getClass().getResource("/");
  161. String dbconfig = "DatabaseConnection.properties";
  162. if (getApplicationId() != null) {
  163. dbconfig = getApplicationId();
  164. }
  165. veLogger.addInfo("File Path-->" + new File(dbconfig).getAbsoluteFile());
  166.  
  167. Properties PropReader = veProperty.readProperty(dbconfig);
  168. String Driver = PropReader.getProperty("driver");
  169. String Url = PropReader.getProperty("connection");
  170. String User = PropReader.getProperty("dbuser");
  171. String Password = PropReader.getProperty("dbpassword");
  172.  
  173. try {
  174. Class.forName(Driver);
  175. } catch (ClassNotFoundException e) {
  176. // LogginEg.callLogger(e);
  177. veLogger.addError("Where is your MySQL JDBC Driver?" + e.getMessage());
  178. return null;
  179. }
  180.  
  181. veLogger.addInfo("MySQL JDBC Driver Registered!");
  182. veLogger.addInfo("---Connection ---" + Url + "-" + User + "-" + Password);
  183. //connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/leela_kidel3", "root", "password");
  184. return DriverManager.getConnection(Url, User, Password);
  185. // l33l@$123#
  186.  
  187. } catch (SQLException e) {
  188. // LogginEg.callLogger(e);
  189. veLogger.addError("Connection Failed! Check output console" + e.getMessage());
  190. e.printStackTrace();
  191. return null;
  192. }
  193. }
  194.  
  195. // public static java.sql.Connection getConnection() throws SQLException {
  196. // if (connection == null) {
  197. // open();
  198. // }
  199. // if (connection.isClosed()) {
  200. // open();
  201. // }
  202. // return connection;
  203. // }
  204. public static void close(java.sql.Connection connection) throws SQLException {
  205. connection.close();
  206. }
  207.  
  208. public static String ExecuteQuery(String Query) {
  209. PreparedStatement statment = null;
  210. java.sql.Connection connection = null;
  211. ResultSet getKeyid = null;
  212. try {
  213. connection = new Connection().open();
  214. statment = connection.prepareStatement(Query);
  215. statment.execute();
  216. getKeyid = statment.getGeneratedKeys();
  217. String data = getKeyid.getLong(1) + "";
  218.  
  219. return data;
  220. } catch (SQLException e) {
  221. System.out.println("Errror" + e.toString());
  222. return e.toString();
  223. }
  224. finally{
  225. try {
  226. getKeyid.close();
  227. statment.close();
  228. close(connection);
  229. } catch (SQLException ex) {
  230. Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  231. }
  232. }
  233. }
  234.  
  235. public static ResultSet Sql_select(String query) {
  236. java.sql.Connection connection = null;
  237. Statement st = null;
  238. ResultSet rs = null ;
  239. try {
  240. connection = new Connection().open();
  241. veLogger.addInfo("***Query-" + query);
  242. st = connection.createStatement();
  243. rs = st.executeQuery(query);
  244.  
  245. return rs;
  246. } catch (Exception e) {
  247. // LogginEg.callLogger(e);
  248. veLogger.addError("//-" + query);
  249. return null;
  250. }finally{
  251. try {
  252. st.close();
  253. rs.close();
  254. close(connection);
  255. } catch (SQLException ex) {
  256. Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  257. }
  258. }
  259. }
  260.  
  261. public static ResultSet Sql_Procedure(String Query) {
  262. java.sql.Connection connection = null;
  263. CallableStatement st = null;
  264. ResultSet rs = null ;
  265. try {
  266. connection = new Connection().open();
  267. Query = "{call " + Query + "}";
  268. veLogger.addInfo("***Query-" + Query);
  269. st = (CallableStatement) connection.prepareCall(Query);
  270. rs = st.executeQuery(Query);
  271.  
  272. return rs;
  273. } catch (Exception e) {
  274. // LogginEg.callLogger(e);
  275. e.printStackTrace();
  276. veLogger.addError("//-" + Query + "\n" + e.getMessage());
  277. return null;
  278. }finally{
  279. try {
  280. st.close();
  281. rs.close();
  282. close(connection);
  283. } catch (SQLException ex) {
  284. Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  285. }
  286. }
  287. }
  288.  
  289. public static CallableStatement Sql_ProcedureStatment(String Query) {
  290. java.sql.Connection connection = null;
  291. CallableStatement st = null;
  292. try {
  293. connection = new Connection().open();
  294. Query = "{call " + Query + "}";
  295. veLogger.addInfo("***Query-" + Query);
  296. st = (CallableStatement) connection.prepareCall(Query);
  297. //ResultSet rs = st.executeQuery(Query);
  298. //st.getMoreResults()
  299. //rs.next();
  300.  
  301. return st;
  302. } catch (Exception e) {
  303. // LogginEg.callLogger(e);
  304. veLogger.addError("//-" + Query);
  305. return null;
  306. }finally{
  307. try {
  308. st.close();
  309. close(connection);
  310. } catch (SQLException ex) {
  311. Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  312. }
  313. }
  314. }
  315.  
  316. public static DataTable SelectSQL(String Query) {
  317. java.sql.Statement st = null;
  318. java.sql.Connection connection = null;
  319. ResultSet rs = null;
  320. try {
  321. connection = new Connection().open();
  322. Query = "{call " + Query + "}";
  323. veLogger.addInfo("***Query-" + Query);
  324. //CallableStatement st = (CallableStatement) connection.prepareCall(Query);
  325. st = connection.createStatement();
  326. rs = st.executeQuery(Query);
  327.  
  328. //st.getMoreResults()
  329. //rs.next();
  330. DataTable dataTable = new DataTable();
  331. dataTable.fillResult(rs);
  332.  
  333. return dataTable;
  334. } catch (Exception e) {
  335. // LogginEg.callLogger(e);
  336. veLogger.addError("//-" + Query + "=" + e.getMessage());
  337. return null;
  338. }finally{
  339. try {
  340. st.close();
  341. rs.close();
  342. close(connection);
  343. } catch (SQLException ex) {
  344. Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  345. }
  346. }
  347. }
  348.  
  349. public static DataTable SelectSQLQuery(String Query) {
  350. java.sql.Statement st = null;
  351. java.sql.Connection connection = null;
  352. ResultSet rs = null;
  353. try {
  354. connection = new Connection().open();
  355. veLogger.addInfo("***Query-" + Query);
  356. st = connection.createStatement();
  357. rs = st.executeQuery(Query);
  358. //st.getMoreResults()
  359. //rs.next();
  360. DataTable dataTable = new DataTable();
  361. dataTable.fillResult(rs);
  362.  
  363. return dataTable;
  364. } catch (Exception e) {
  365. e.printStackTrace();
  366. veLogger.addError("//-" + Query + "=" + e.getMessage());
  367. return null;
  368. }finally{
  369. try {
  370. st.close();
  371. rs.close();
  372. close(connection);
  373. } catch (SQLException ex) {
  374. Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  375. }
  376. }
  377. }
  378.  
  379. public static DataTable SelectSQLQueryTempConnectiton(String Driver, String Url, String User, String Password, String Query) {
  380. java.sql.Statement st;
  381. java.sql.Connection tmpconnection = null;
  382. try {
  383. try {
  384. Class.forName(Driver);
  385.  
  386. } catch (ClassNotFoundException e) {
  387. // LogginEg.callLogger(e);
  388. veLogger.addError("Where is your MySQL JDBC Driver?" + e.getMessage());
  389. return null;
  390. }
  391. tmpconnection = DriverManager.getConnection(Url, User, Password);
  392. //connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/leela_kidel3", "root", "password");
  393. st = tmpconnection.createStatement();
  394. ResultSet rs = st.executeQuery(Query);
  395. //st.getMoreResults()
  396. //rs.next();
  397. DataTable dataTable = new DataTable();
  398. dataTable.fillResult(rs);
  399. tmpconnection.close();
  400. return dataTable;
  401. } catch (Exception e) {
  402. e.printStackTrace();
  403. veLogger.addError("//-" + Query + "=" + e.getMessage());
  404. return null;
  405. }
  406. }
  407.  
  408. public static File getBlob(String Query, String filename_colname, String filename_blobname) {
  409. ResultSet rs = Sql_select(Query);
  410. File file = null;
  411. try {
  412. if (rs.next()) {
  413. file = new File(rs.getString(filename_colname));
  414. FileOutputStream output = new FileOutputStream(file);
  415.  
  416. veLogger.addInfo("Writing to file " + file.getAbsolutePath());
  417. while (rs.next()) {
  418. InputStream input = rs.getBinaryStream(filename_blobname);
  419. byte[] buffer = new byte[1024];
  420. while (input.read(buffer) > 0) {
  421. output.write(buffer);
  422. }
  423. }
  424. }
  425. } catch (Exception ex) {
  426. ex.printStackTrace();
  427. veLogger.addError(ex.getMessage());
  428. }
  429. return file;
  430. }
  431.  
  432. public static String NullToString(Object value) {
  433. if (value == null) {
  434. return "";
  435. } else {
  436. return value.toString();
  437. }
  438. }
  439.  
  440. public static void main(String[] args) throws SQLException {
  441. java.sql.Connection connection = new Connection().open();
  442. System.out.println("--Connection--" + connection);
  443. }
  444. }
Add Comment
Please, Sign In to add comment