如何使用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
问题描述
我不太擅长 Java.我正在做一个应用程序,我想验证在文本框中作为密码输入的值,并使用存储在数据库中的密码值.如果两个值不匹配,我想显示错误消息.我正在创建我将使用的此 Java 代码的 Web 服务.return 0
, return 1
, return (value)
有什么区别??我怎样才能做到这一点?有人可以给我一个示例代码示例吗?我在一个单独的类中创建了连接,并为它创建了一个名为 dbsource 的对象.
I am not so good in 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.
What is the difference between return 0
, return 1
, return (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.
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;
}
使用的第二个类:
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;
}
推荐答案
从用户那里获取密码并存储在 String 对象中.
Get the password from user and store it in String object.
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)
}
}
现在从 main()
int a = present();
if (a==0){
System.out.print("data not found")
}
else{
System.out.print("data found")
}
这篇关于如何使用oracle数据库中的值验证我的密码以及java中return 0和return 1之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!