is Admin via MySQL Table - Java java.sql

Java examples for java.sql:MySQL

Description

is Admin via MySQL Table

Demo Code


//package com.java2s;
import java.sql.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        String username = "java2s.com";
        String password = "java2s.com";
        System.out.println(isAdmin(username, password));
    }/* w  w  w.j av  a2  s . co  m*/

    public static boolean isAdmin(String username, String password) {
        ResultSet rs = null;
        PreparedStatement ps = null;
        Connection con = null;
        boolean status = false;

        try {

            //loading drivers for mysql
            Class.forName("com.mysql.jdbc.Driver");

            //creating connection with the database 
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/cs3520", "root", "simi");
            ps = con.prepareStatement("select * from User where username=? and password=? and role_id=?");
            ps.setString(1, username);
            ps.setString(2, password);
            ps.setString(3, "1");
            rs = ps.executeQuery();
            status = rs.next();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (Exception e) {

            }
        }
        return status;
    }
}

Related Tutorials