Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Scanner;
- public class Login {
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/Account"; // Account -> Connection Name
- static final String USERNAME = "root"; // User name
- static final String PASSWORD = "password"; // Password
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- Scanner s = new Scanner(System.in);
- System.out.print("ID : ");
- String id = s.next();
- System.out.print("PW : ");
- String pw = s.next();
- try{
- Class.forName(JDBC_DRIVER);
- conn = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
- System.out.println("\n- MySQL Connection");
- stmt = conn.createStatement();
- String sql;
- sql = "SELECT number,id, pw FROM account";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- int num = rs.getInt("number");
- String ID = rs.getString("ID");
- String PW = rs.getString("PW");
- if(ID.equals(id) || PW.equals(pw)) {
- System.out.println("Login Success!!");
- System.out.println("Num : " + num + " ID : " + ID + " PW : " + PW);
- break;
- }
- System.out.println("Login Failed!!");
- }
- rs.close();
- stmt.close();
- conn.close();
- }catch(SQLException se1){
- se1.printStackTrace();
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- try{
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2){
- }
- try{
- if(conn!=null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }
- }
- System.out.println("\n\n- MySQL Connection Close");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement