Advertisement
Guest User

Untitled

a guest
May 12th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 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.  
  7. /**
  8. *
  9. * @author andi
  10. */
  11. import java.util.Scanner;
  12. import java.sql.*;
  13. public class Login {
  14. //nama JDBC driver dan URL database
  15. static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
  16. static final String DB_URL="jdbc:mysql://localhost/login";
  17.  
  18. //hak akses database
  19. static String userLogin="";
  20. static String passwordLogin="";
  21. static final String userDB="andi";
  22. static final String passwordDB="belajar";
  23. public static void main (String []args){
  24. Scanner input= new Scanner(System.in);
  25. Connection koneksi=null;
  26. PreparedStatement stmt= null;
  27. try{
  28. //register jdbc driver
  29. Class.forName(JDBC_DRIVER);
  30. //buka koneksinya
  31. System.out.println("Connecting to database . . . .");
  32. koneksi=DriverManager.getConnection(DB_URL,userDB,passwordDB);
  33. System.out.println("Registrasi user ");
  34. System.out.print("Masukan username :");
  35. userLogin=input.nextLine();
  36. System.out.print("Masukan password : ");
  37. passwordLogin=input.nextLine();
  38. //eksekusi querynya
  39. System.out.println("Creating statement . . . ");
  40. String query="INSERT INTO login(username,password) VALUES(?,?)";
  41. stmt=koneksi.prepareStatement(query);
  42. //bind nilai ke parameter values
  43. stmt.setString(1,userLogin);
  44. stmt.setString(2,passwordLogin);
  45. System.out.println("Sukses !");
  46. stmt.executeUpdate(query);
  47. }
  48. catch(SQLException | ClassNotFoundException se){
  49. }
  50. finally{
  51. try{
  52. if(stmt!=null){
  53. stmt.close();
  54. }
  55.  
  56. }
  57. catch(SQLException se){
  58.  
  59. }
  60. try{
  61. if(koneksi!=null){
  62. koneksi.close();
  63. }
  64. }
  65. catch(SQLException se){
  66.  
  67. }
  68. }
  69.  
  70.  
  71.  
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement