Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author andi
- */
- import java.util.Scanner;
- import java.sql.*;
- public class Login {
- //nama JDBC driver dan URL database
- static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
- static final String DB_URL="jdbc:mysql://localhost/login";
- //hak akses database
- static String userLogin="";
- static String passwordLogin="";
- static final String userDB="andi";
- static final String passwordDB="belajar";
- public static void main (String []args){
- Scanner input= new Scanner(System.in);
- Connection koneksi=null;
- PreparedStatement stmt= null;
- try{
- //register jdbc driver
- Class.forName(JDBC_DRIVER);
- //buka koneksinya
- System.out.println("Connecting to database . . . .");
- koneksi=DriverManager.getConnection(DB_URL,userDB,passwordDB);
- System.out.println("Registrasi user ");
- System.out.print("Masukan username :");
- userLogin=input.nextLine();
- System.out.print("Masukan password : ");
- passwordLogin=input.nextLine();
- //eksekusi querynya
- System.out.println("Creating statement . . . ");
- String query="INSERT INTO login(username,password) VALUES(?,?)";
- stmt=koneksi.prepareStatement(query);
- //bind nilai ke parameter values
- stmt.setString(1,userLogin);
- stmt.setString(2,passwordLogin);
- System.out.println("Sukses !");
- stmt.executeUpdate(query);
- }
- catch(SQLException | ClassNotFoundException se){
- }
- finally{
- try{
- if(stmt!=null){
- stmt.close();
- }
- }
- catch(SQLException se){
- }
- try{
- if(koneksi!=null){
- koneksi.close();
- }
- }
- catch(SQLException se){
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement