Advertisement
shifaniadr

CobaDatabase.java

Jun 1st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. import javax.swing.plaf.nimbus.State;
  2. import java.sql.*;
  3. import java.util.HashMap;
  4.  
  5. public class CobaDatabase {
  6.     private static final String DB_NAME = "dbcs_1";
  7.     private static final String DB_USERNAME = "root";
  8.     private static final String DB_PASSWORD = "";
  9.  
  10.     private Connection connection;
  11.  
  12.     public CobaDatabase() throws SQLException {
  13.         this.connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + DB_NAME, DB_USERNAME, DB_PASSWORD);
  14.     }
  15.  
  16.     public void select(String tableName, String[] column) {
  17.         try {
  18.             Statement statement = this.connection.createStatement();
  19.  
  20.             String query = "SELECT ";
  21.             for (int i = 0; i < column.length; i++) {
  22.                 query += column[i];
  23.                 if (i != column.length - 1)
  24.                     query += ",";
  25.             }
  26.  
  27.             query += " FROM " + tableName;
  28.  
  29.             ResultSet result = statement.executeQuery(query);
  30.             this.print(result);
  31.  
  32.             result.close();
  33.             statement.close();
  34.         } catch(SQLException e) {
  35.             System.out.println("Error : " + e.getMessage());
  36.         }
  37.     }
  38.  
  39.     public void update(String tableName, String set, String where) {
  40.         try {
  41.             Statement statement = this.connection.createStatement();
  42.  
  43.             String query = "UPDATE " + tableName + " SET " + set;
  44.  
  45.             if (where != null)
  46.                 query += " WHERE " + where;
  47. //            System.out.println(query);
  48.             if (statement.executeUpdate(query) > 0)
  49.                 System.out.println("Successfully updated database");
  50.         } catch (SQLException e) {
  51.             System.out.println("Error : " + e.getMessage());
  52.         }
  53.     }
  54.     public void print(ResultSet result) throws SQLException {
  55.  
  56.         // Ambil daftar kolom dari hasil
  57.         String[] resColumn = new String[result.getMetaData().getColumnCount()];
  58.         for (int i = 1; i <= result.getMetaData().getColumnCount(); i++) {
  59.             resColumn[i - 1] = result.getMetaData().getColumnLabel(i);
  60.         }
  61.  
  62.         while (result.next()) {
  63.             for (int i = 0; i < resColumn.length; i++)
  64.                 System.out.println(resColumn[i] + " : " + result.getString(resColumn[i]));
  65.         }
  66.     }
  67.  
  68.     public static void main(String[] args) {
  69.  
  70.         try {
  71.             CobaDatabase database = new CobaDatabase();
  72.             database.select("mahasiswa", new String[]{"nim", "nama", "jenis_kelamin as \"Jenis Kelamin\"",
  73.                     "IF(kewarganegaraan = 'I', 'Warga Negara Indonesia', 'Warga Negara Asing') as Kewarganegaraan"});
  74.             database.update("mahasiswa", "nama = \"MAMAK\"", "nim = '140101002'");
  75.         } catch (SQLException e) {
  76.             e.printStackTrace();
  77.         }
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement