Advertisement
Guest User

konekdb

a guest
Dec 6th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package konekdb;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.Scanner;
  15.  
  16.  
  17. /**
  18.  *
  19.  * @author LABCOM3-25
  20.  */
  21. public class KonekDb {
  22.  
  23.     /**
  24.      * @param args the command line arguments
  25.      */
  26.     public static void main(String[] args) {
  27.         // TODO code application logic here
  28.         String url = "jdbc:mysql://localhost:3306/indonesia";
  29.         String username = "root";
  30.         String password = "";
  31.        
  32.         System.out.println("Connecting database...");
  33.        
  34.         Scanner sc = new Scanner(System.in);
  35.         System.out.print("Masukkan ID Kelurahan : ");
  36.         String idKelurahan = sc.nextLine();
  37.         System.out.print("Masukkan Nama Kelurahan : ");
  38.         String namaKelurahan = sc.nextLine();
  39.        
  40.         try (Connection connection = DriverManager.getConnection(url, username, password)) {
  41.             System.out.println("Database connected!");
  42.            
  43.             String query = "INSERT INTO kelurahan "
  44.                     + "(ID_KELURAHAN, ID_KOTA_KABUPATEN, "
  45.                     + "NAMA_KELURAHAN) VALUES "
  46.                     + "('"+idKelurahan+"', '1101', \""+namaKelurahan+"\");";
  47.            
  48.             Statement st = connection.createStatement();
  49.             st.executeUpdate(query);
  50.            
  51.            
  52.            
  53.             /*
  54.             String query = "SELECT * FROM kelurahan";
  55.             ResultSet rs = st.executeQuery(query);
  56.            
  57.             ArrayList<Kelurahan> k = new ArrayList<>();
  58.  
  59.             while (rs.next()) {
  60.                 Kelurahan newK = new Kelurahan(rs.getString(1), rs.getString(2), rs.getString(3));
  61.                 k.add(newK);
  62.             }*/
  63.            
  64.         } catch (SQLException e) {
  65.             throw new IllegalStateException("Cannot connect the database!", e);
  66.         }
  67.  
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement