Advertisement
Guest User

Untitled

a guest
May 13th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. package com.amazon.db;
  2. import java.sql.*;
  3.  
  4. public class QueryExecutor {
  5.     static Connection conn;
  6.  
  7.     public static ResultSet executeQuery(String query) throws SQLException {
  8.         if (conn == null) {
  9.             try {
  10.                 String userName = "foo";
  11.                 String userPassword = "bar";
  12.                 String url = "jdbc:mysql://petrescu.desktop.amazon.com:3306/foo";
  13.                 Class.forName("com.mysql.jdbc.Driver").newInstance();
  14.                 conn = DriverManager.getConnection (url, userName, userPassword);
  15.             } catch (Exception e) {
  16.                 System.err.println("Cannot connect to database server" + e);
  17.             } finally {
  18.                 if (conn != null)
  19.                 {
  20.                     try
  21.                     {
  22.                         conn.close ();
  23.                         System.out.println ("Database connection terminated");
  24.                     }
  25.                     catch (Exception e) { /* ignore close errors */ }
  26.                 }
  27.             }
  28.         }
  29.  
  30.         try {
  31.             Statement s = conn.createStatement();
  32.             return s.executeQuery(query);
  33.         } catch (SQLException e) {
  34.             System.err.println("Something wrong with the query " + query);
  35.         }
  36.        
  37.         return null;
  38.     }
  39.    
  40.     public static int executeUpdate(String update) throws SQLException {
  41.         if (conn == null) {
  42.             try {
  43.                 String userName = "foo";
  44.                 String userPassword = "bar";
  45.                 String url = "jdbc:mysql://petrescu.desktop.amazon.com:3306/foo";
  46.                 Class.forName("com.mysql.jdbc.Driver").newInstance();
  47.                 conn = DriverManager.getConnection (url, userName, userPassword);
  48.             } catch (Exception e) {
  49.                 System.err.println("Cannot connect to database server" + e);
  50.             } finally {
  51.                 if (conn != null)
  52.                 {
  53.                     try
  54.                     {
  55.                         conn.close ();
  56.                         System.out.println ("Database connection terminated");
  57.                     }
  58.                     catch (Exception e) { /* ignore close errors */ }
  59.                 }
  60.             }
  61.         }
  62.  
  63.         try {
  64.             Statement s = conn.createStatement();
  65.             return s.executeUpdate(update);
  66.         } catch (SQLException e) {
  67.             System.err.println("Something wrong with the update " + update);
  68.         }
  69.        
  70.         return -1;
  71.     }
  72.    
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement