Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.amazon.db;
- import java.sql.*;
- public class QueryExecutor {
- static Connection conn;
- public static ResultSet executeQuery(String query) throws SQLException {
- if (conn == null) {
- try {
- String userName = "foo";
- String userPassword = "bar";
- String url = "jdbc:mysql://petrescu.desktop.amazon.com:3306/foo";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection (url, userName, userPassword);
- } catch (Exception e) {
- System.err.println("Cannot connect to database server" + e);
- } finally {
- if (conn != null)
- {
- try
- {
- conn.close ();
- System.out.println ("Database connection terminated");
- }
- catch (Exception e) { /* ignore close errors */ }
- }
- }
- }
- try {
- Statement s = conn.createStatement();
- return s.executeQuery(query);
- } catch (SQLException e) {
- System.err.println("Something wrong with the query " + query);
- }
- return null;
- }
- public static int executeUpdate(String update) throws SQLException {
- if (conn == null) {
- try {
- String userName = "foo";
- String userPassword = "bar";
- String url = "jdbc:mysql://petrescu.desktop.amazon.com:3306/foo";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection (url, userName, userPassword);
- } catch (Exception e) {
- System.err.println("Cannot connect to database server" + e);
- } finally {
- if (conn != null)
- {
- try
- {
- conn.close ();
- System.out.println ("Database connection terminated");
- }
- catch (Exception e) { /* ignore close errors */ }
- }
- }
- }
- try {
- Statement s = conn.createStatement();
- return s.executeUpdate(update);
- } catch (SQLException e) {
- System.err.println("Something wrong with the update " + update);
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement