Guest User

Untitled

a guest
Jan 4th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class MySQLConnection
  4. {
  5.     // Default database connection info.
  6.     private static final String SERVER = "localhost",
  7.                                 DATABASE = "test",
  8.                                 USERNAME = "root",
  9.                                 PASSWORD = "";
  10.    
  11.     private static final String ATTRIBUTE_SEPARATOR = ", ";
  12.    
  13.     private Connection con;
  14.    
  15.     /**
  16.      * Default constructor.
  17.      * Uses the static default server, database, username and password.
  18.      */
  19.     public MySQLConnection()
  20.     {
  21.         this(SERVER, DATABASE, USERNAME, PASSWORD);
  22.     }
  23.    
  24.     /**
  25.      * Custom constructor.
  26.      * Uses the input strings to connect to a custom database.
  27.      * @param server The server IP
  28.      * @param db The database
  29.      * @param user A username
  30.      * @param pass A password
  31.      */
  32.     public MySQLConnection(String server, String db, String user, String pass)
  33.     {
  34.         try
  35.         {
  36.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  37.             con = java.sql.DriverManager.getConnection("jdbc:mysql://" + server + "/" + db, user, pass);
  38.         }
  39.         catch (Exception e)
  40.         {
  41.             System.out.println("An error occurred: " + e);
  42.             System.exit(0);
  43.         }
  44.         System.out.println("Connection OK.");
  45.     }
  46.    
  47.     /**
  48.      * Get the Connection object of this MySQLConnection
  49.      * @return A Connection
  50.      */
  51.     public Connection getConnection()
  52.     {
  53.         return con;
  54.     }
  55.    
  56.     /**
  57.      * Execute the input string as a query.
  58.      * @param sql The query
  59.      * @return A ResultSet if the query was successful, null otherwise
  60.      */
  61.     public ResultSet query(String sql)
  62.     {
  63.         try
  64.         {
  65.             PreparedStatement s = con.prepareStatement(sql);
  66.             return s.executeQuery();
  67.         }
  68.         catch (Exception e)
  69.         {
  70.             System.out.println("An error occurred: " + e);
  71.         }
  72.         return null;
  73.     }
  74.    
  75.     /**
  76.      * Execute a select query with the input variables.
  77.      * @param select The SELECT clause
  78.      * @param from The FROM clause
  79.      * @param where The WHERE clause
  80.      * @return A ResultSet, if the query was successful, null otherwise.
  81.      */
  82.     public ResultSet select(String select, String from, String where)
  83.     {
  84.         String query = "SELECT " + select +
  85.                        "FROM "   + from +
  86.                        "WHERE "  + where;
  87.        
  88.         return query(query);
  89.     }
  90.    
  91.     /**
  92.      * Execute an update using the input SQL statement
  93.      * @param sql An update statement
  94.      * @return The row count or 0 for successful updates, -1 otherwise
  95.      */
  96.     public int update(String sql)
  97.     {
  98.         try
  99.         {
  100.             PreparedStatement s = con.prepareStatement(sql);
  101.             return s.executeUpdate();
  102.         }
  103.         catch (Exception e)
  104.         {
  105.             System.out.println("An error occurred: " + e);
  106.         }
  107.         return -1;
  108.     }
  109.    
  110.     /**
  111.      * Given a ResultSet, print all the attribute values of the attributes in the String array
  112.      * @param r A ResultSet
  113.      * @param attrs An arbitrary list of attributes
  114.      */
  115.     public void printValues(ResultSet r, String... attrs)
  116.     {
  117.         try
  118.         {
  119.             while(r.next())
  120.             {
  121.                 StringBuffer buffy = new StringBuffer();
  122.                 for (int i = 0; i < attrs.length; i++)
  123.                 {
  124.                     buffy.append(r.getString(attrs[i]));
  125.                     if (i < attrs.length - 1)
  126.                         buffy.append(ATTRIBUTE_SEPARATOR);
  127.                 }
  128.                 System.out.println(buffy);
  129.             }
  130.         }
  131.         catch (Exception e)
  132.         {
  133.             System.out.println("Brown donkey.");
  134.         }
  135.     }
  136. }
Add Comment
Please, Sign In to add comment