在phpmyAdmin中验证用户名和密码

时间:2015-04-13 09:02:38

标签: java mysql phpmyadmin

我正在尝试使用用户名和密码验证数据库(phpMyadmin)。我需要搜索所有表(我的数据库中有25个表),以检查是否存在给定的用户名和密码(身份验证)。任何人都可以使用给定的用户名和密码提供查询以搜索整个表吗?

以下是我的代码

public class Dbclass {

    /**
     * @param args
     */
    Connection conn = null;
    Statement statement = null;
    ResultSet resultSet = null;
    ResultSetMetaData metaData = null;
    String DB_URL="com.mysql.jdbc.Driver";
    String USER="java";
    String PASS="redhat";
    String username;
    String password;
    ResultSet rs;
    public ResultSet dbConnection(String query)
    {

        //STEP 2: Register JDBC driver
        try {
            Class.forName("com.mysql.jdbc.Driver");

            //STEP 3: Open a connection
            System.out.println("Connecting to database...");
            try {
                conn = DriverManager.getConnection(DB_URL,USER,PASS);

                //STEP 4: Execute a query
                System.out.println("Creating statement...");
                Statement stmt = conn.createStatement();

                //sql = "SELECT username,password FROM Employees";
                rs = stmt.executeQuery(query);

                while(rs.next()){
                    //Retrieve by column name
                    username  = rs.getString("user_name");
                    password = rs.getString("password");

                    //Display values
                    System.out.print("User_name: " + username);
                    System.out.print(",Password: " + password);
                }
                Authentication obj=new Authentication();
                obj.userLogin(username,password);

                //STEP 6: Clean-up environment
                //rs.close();
                //return rs;
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                //TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (ClassNotFoundException es) {
            //TODO Auto-generated catch block
            es.printStackTrace();
        }
        finally{
            //finally block used to close resources
            try{
                if(conn!=null){
                    conn.close();
                }
                if (rs != null) {
                    rs.close();
                }
            }catch(SQLException se){
                se.printStackTrace();
            }//end finally try
        }//end try
        return rs;
    }
}

1 个答案:

答案 0 :(得分:0)

我猜你正在寻找一个where子句。像下面的东西。

  

SELECT用户名,密码FROM Employees,其中username ='usernm',password ='pwd'

如果需要,您可以添加UNION以合并更多此类查询,但由于性能问题,不推荐使用。