简体   繁体   English

如何使用oracle数据库中的值验证我的密码,以及java中return 0和return 1有什么区别

[英]How to validate my password with the value in the oracle database and also what is the difference between return 0 and return 1 in java

I am not so good in Java. 我的Java不太好。 I am doing an an application and I want to validate the value entered as password in a text box with the value stored in the database for the password. 我正在做一个应用程序,我想用存储在数据库中的密码值来验证在文本框中输入为密码的值。 If both the values do not match, I want to show an error message. 如果两个值都不匹配,我想显示一条错误消息。 I am creating a web service of this Java code that I will be using. 我正在创建将要使用的Java代码的Web服务。 What is the difference between return 0 , return 1 , return (value) ?? return 0return 1return (value)什么区别? How can I do this? 我怎样才能做到这一点? Can someone give me a sample code example? 有人可以给我示例代码示例吗? I have created the connection in a separate class and created an object for it called- dbsource. 我在单独的类中创建了连接,并为其创建了一个名为dbsource的对象。

package com.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.dto.LoginBean;

 public class ApplicationStatusDAO {

DBSource dbsouce = new DBSource();

public List<LoginBean> userLogin()
  {
    List<LoginBean> listloginBean=new ArrayList<LoginBean>();
    LoginBean loginbean=null;
    Statement stmt= null;
    try {

        String query = "select * from user_self";

        try {
            stmt = dbsouce.getConnection().createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while(rs.next()) {
                loginbean=new LoginBean();
                 System.out.println("results: " + rs.getString("uname")+" "+        rs.getString("s_gender")+" "+ rs.getString("s_lname"));

                 loginbean.setUname(rs.getString("uname"));
                 loginbean.setPass(rs.getString("pass"));
                 loginbean.setGender(rs.getString("s_gender"));
                 loginbean.setLname(rs.getString("s_lname"));
                 listloginBean.add(loginbean);
            }

            System.out.println("size of list"+ listloginBean.size());
        } catch (SQLException e ) {
            System.out.println("result set Error: " + e);
        }


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return listloginBean;
    }

The second class used: 第二类使用:

import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import com.daoImpl.ApplicationStatusDAO;
import com.dto.LoginBean;



@Path("/selfservice")
public class ServiceDispatcher {
@GET
@Produces("application/json")
@Path("login/")
public LoginBean loginAuth(@QueryParam("uname") String uname,@QueryParam("pass")    String pass) 
{
    LoginBean loginBean=new LoginBean();
    System.out.println("loginAuth- user name=" + uname + " password=" + pass);
    ApplicationStatusDAO applicationStatusDAO=new ApplicationStatusDAO();

    List<LoginBean> bean =applicationStatusDAO.userLogin();


    for(LoginBean aa:bean)
    {
    if(uname.equals(aa.getUname())&& pass.equals(aa.getPass()))
          {
        System.out.println("ssssssssssssssssssssssss");

        loginBean.setGender(aa.getGender());
        loginBean.setUname(aa.getUname());
        loginBean.setLname(aa.getLname());
        return loginBean;
           }

    else
    {
        System.out.println("ffffffffffffffffffffffffff");
        //LoginBean loginBean1=new LoginBean();
        //loginBean1.setFname("user name not ok");
        //return loginBean1; 
    }

    }



    return loginBean;
   }
  1. What a method returns really depends on what it does. 方法返回的内容实际上取决于它的功能。 Unlike in C, you have a primitive boolean type, so if you want to return success/failure of a method, you should return true / false , respectively. 与C语言不同,您有一个原始的boolean类型,因此,如果要返回方法的成功/失败,则应分别返回true / false However, you may also want to have a void method that throws some Exception instead of returning a value instead. 但是,您可能还希望有一个void方法,该方法抛出一些Exception而不是返回值。

  2. Get the value of the password from the database and compare it to the input string with the equals() method. 从数据库获取密码的值,然后使用equals()方法将其与输入字符串进行比较。

Get the password from user and store it in String object. 从用户那里获取密码并将其存储在String对象中。

String pass = "yourpassword"; // or get it from System using Scanner
String username = "username";

Now make a function where you will Fetch the data from table 现在创建一个函数,从中获取表中的数据

   public int present(){

   // make query for Oracle data base
       String yourQry = "select * from yourtable where password = '"+pass+"' and username='"+username+"'" . 
    //Use this query when you are fetching data from user.
    Resultset rs;// fetch your data in resultset
         if(rs.next()){
               // if data got fetched 
               return 1;// In other languages(C,C++) 0 and 1 are treated as true or false
               // 1 means true
          }
         else{
                  return 0; // 0 means false (No datafound) 
         }

        }

Now call this function from main() 现在从main()调用此函数

int a = present();

if (a==0){
  System.out.print("data not found")
}
else{
    System.out.print("data found")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM