Advertisement
Guest User

Untitled

a guest
Jun 5th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1.  
  2. /*
  3.  * To change this license header, choose License Headers in Project Properties.
  4.  * To change this template file, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. package test;
  8. import java.util.Scanner;
  9.  import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14.  
  15. /**
  16.  *
  17.  * @author Rafog
  18.  */
  19. public class Test {
  20.    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  21.     static final String DB_URL = "jdbc:mysql://localhost/biodata";
  22.     static final String USER = "root";
  23.     static final String PASS = "";
  24.     static Connection conn;
  25.     static Statement stmt;
  26.     static ResultSet rs;
  27.     /**
  28.      * @param args the command line arguments
  29.      */
  30.     public static void main(String[] args) {
  31.         // TODO code application logic here
  32.         try{
  33.         String input,inputdb;
  34.         Scanner keyboard=new Scanner(System.in);
  35.         Class.forName(JDBC_DRIVER);
  36.         conn = DriverManager.getConnection(DB_URL, USER, PASS);
  37.         System.out.println("silahkan Masukkan Pilihan:");
  38.          System.out.println("1.Masukkan Data");
  39.           System.out.println("2.Tampilkan Data");
  40.           input=keyboard.next();
  41.          
  42.          
  43.           if(input.equals("1"))
  44.           {
  45.              System.out.println("Anda telah Memilih 1");
  46.              System.out.print("Masukkan sesuatu:");
  47.              inputdb=keyboard.next();
  48.              stmt=conn.createStatement();
  49.              String sql="Insert into bio values('"+inputdb+"')";
  50.               System.out.print("Sukses Menginput data");
  51.  
  52.           }
  53.          
  54.           else if(input.equals("2"))
  55.           {
  56.                System.out.println("Anda telah Memilih 2");
  57.                stmt = conn.createStatement();
  58.                  String sql = "SELECT * FROM bio";
  59.                  rs=stmt.executeQuery(sql);
  60.                   while(rs.next()){
  61.                 System.out.println("Nama: " + rs.getString("nama"));
  62.              
  63.             }
  64.           }
  65.  
  66.          
  67.     }
  68.        
  69.         catch(Exception e)
  70.         {
  71.             System.out.println(e.getMessage());
  72.         }
  73.     }
  74.    
  75.    
  76.  
  77.    
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement