Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class MySQLConnection
- {
- // Default database connection info.
- private static final String SERVER = "localhost",
- DATABASE = "test",
- USERNAME = "root",
- PASSWORD = "";
- private static final String ATTRIBUTE_SEPARATOR = ", ";
- private Connection con;
- /**
- * Default constructor.
- * Uses the static default server, database, username and password.
- */
- public MySQLConnection()
- {
- this(SERVER, DATABASE, USERNAME, PASSWORD);
- }
- /**
- * Custom constructor.
- * Uses the input strings to connect to a custom database.
- * @param server The server IP
- * @param db The database
- * @param user A username
- * @param pass A password
- */
- public MySQLConnection(String server, String db, String user, String pass)
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = java.sql.DriverManager.getConnection("jdbc:mysql://" + server + "/" + db, user, pass);
- }
- catch (Exception e)
- {
- System.out.println("An error occurred: " + e);
- System.exit(0);
- }
- System.out.println("Connection OK.");
- }
- /**
- * Get the Connection object of this MySQLConnection
- * @return A Connection
- */
- public Connection getConnection()
- {
- return con;
- }
- /**
- * Execute the input string as a query.
- * @param sql The query
- * @return A ResultSet if the query was successful, null otherwise
- */
- public ResultSet query(String sql)
- {
- try
- {
- PreparedStatement s = con.prepareStatement(sql);
- return s.executeQuery();
- }
- catch (Exception e)
- {
- System.out.println("An error occurred: " + e);
- }
- return null;
- }
- /**
- * Execute a select query with the input variables.
- * @param select The SELECT clause
- * @param from The FROM clause
- * @param where The WHERE clause
- * @return A ResultSet, if the query was successful, null otherwise.
- */
- public ResultSet select(String select, String from, String where)
- {
- String query = "SELECT " + select +
- "FROM " + from +
- "WHERE " + where;
- return query(query);
- }
- /**
- * Execute an update using the input SQL statement
- * @param sql An update statement
- * @return The row count or 0 for successful updates, -1 otherwise
- */
- public int update(String sql)
- {
- try
- {
- PreparedStatement s = con.prepareStatement(sql);
- return s.executeUpdate();
- }
- catch (Exception e)
- {
- System.out.println("An error occurred: " + e);
- }
- return -1;
- }
- /**
- * Given a ResultSet, print all the attribute values of the attributes in the String array
- * @param r A ResultSet
- * @param attrs An arbitrary list of attributes
- */
- public void printValues(ResultSet r, String... attrs)
- {
- try
- {
- while(r.next())
- {
- StringBuffer buffy = new StringBuffer();
- for (int i = 0; i < attrs.length; i++)
- {
- buffy.append(r.getString(attrs[i]));
- if (i < attrs.length - 1)
- buffy.append(ATTRIBUTE_SEPARATOR);
- }
- System.out.println(buffy);
- }
- }
- catch (Exception e)
- {
- System.out.println("Brown donkey.");
- }
- }
- }
Add Comment
Please, Sign In to add comment