Skip to content

Instantly share code, notes, and snippets.

@Nabid
Last active July 18, 2022 16:07

Revisions

  1. Nabid revised this gist Oct 24, 2020. No changes.
  2. Nabid revised this gist Feb 10, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion DBHelper.java
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ public class DBHelper {
    public DBHelper() {
    //STEP 2: Register JDBC driver
    try {
    Class.forName("JDBC_DRIVER");
    Class.forName(JDBC_DRIVER);
    } catch(Exception e) {
    e.printStackTrace();
    }
  3. Nabid revised this gist Jan 1, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions DBHelper.java
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,8 @@
    db.test();
    4. Close connection
    db.close;
    + jdbc driver can be found at: http://www.java2s.com/Code/Jar/c/Downloadcommysqljdbc515jar.htm
    */

    import java.sql.Connection;
  4. Nabid created this gist Dec 30, 2015.
    86 changes: 86 additions & 0 deletions DBHelper.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    /*
    Assuming that you have proper MySQL JDBC driver:
    How to use this class?
    1. Create new object
    DBHelper db = new DBHelper();
    2. Open connection
    db.open();
    3. Call corresponding method
    db.test();
    4. Close connection
    db.close;
    */

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    /**
    * @author nabid
    */
    public class DBHelper {
    // JDBC driver name and database URL
    private final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private final String DB_URL = "jdbc:mysql://localhost/database_name";

    // Database credentials
    private static final String USER = "username";
    private static final String PASS = "password";

    private Connection conn = null;
    private Statement stmt = null;

    public DBHelper() {
    //STEP 2: Register JDBC driver
    try {
    Class.forName("JDBC_DRIVER");
    } catch(Exception e) {
    e.printStackTrace();
    }
    }

    public void open() {
    try {
    //System.out.println("Connecting to database...");
    conn = DriverManager.getConnection(DB_URL, USER,PASS);
    //System.out.println("Creating statement...");
    stmt = conn.createStatement();
    } catch (SQLException ex) {
    Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    public void close() {
    try {
    stmt.close();
    conn.close();
    } catch (SQLException ex) {
    Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    public void test() {
    try {
    String sql;
    sql = "SELECT * FROM table_name";
    ResultSet rs = stmt.executeQuery(sql); // DML
    // stmt.executeUpdate(sql); // DDL

    //STEP 5: Extract data from result set
    while(rs.next()){
    //Display values
    System.out.print(rs.getString(1));
    System.out.print(rs.getString(2));
    }
    //STEP 6: Clean-up environment
    rs.close();
    } catch (SQLException ex) {
    Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    }
    }