帮忙看看这个程序,怎么结果出不来

shanyou 2004-01-14 06:47:18
DBConnection.java
package my;
import java.sql.*;


public class DBConnection
{
String createConn;
Connection con = null;
Statement stmt;
ResultSet rs;

public DBConnection()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ljn","SYSTEM","manager");
}catch(ClassNotFoundException e)
{
System.out.println(e);
}
catch(SQLException e)
{
System.out.println(e);
}
}

public ResultSet getRs(String s)
{

if(con == null)
{
try
{
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ljn","SYSTEM","manager");
stmt = con.createStatement();
rs = stmt.executeQuery(s);
return rs;
}catch(SQLException e)
{
System.err.println(e);
rs = null;
close();
return rs;
}
}
else
{
try
{
stmt = con.createStatement();
rs = stmt.executeQuery(s);
return rs;
}catch(SQLException e)
{
System.err.println(e);
rs = null;
close();
return rs;
}
}
}

public void close()
{

try
{
if(con != null)
{
con.close();
}
}
catch(SQLException e)
{
System.out.print(e);
}
}
}

AdminLogin.java
package my;
import java.sql.*;


public class AdminLogin
{

String adminName;
String adminPwd;
String success="false",message="";
private DBConnection dbConnection;
ResultSet rs = null;


public AdminLogin()
{
dbConnection = new DBConnection();
}

public void setAdminName(String name)
{
adminName = name;
}
public String getAdminName()
{
return adminName;
}

public void setAdminPwd(String password)
{
adminPwd = password;
}
public String getAdminPwd()
{
return adminPwd;
}
//查询数据库的administrator表
public String getMessage()
{
try
{
String condition = "select * from administrator where adminName = '"+adminName+"'";


rs = dbConnection.getRs(condition);
String name = null;
String ps = null;

while(rs.next())
{
name = rs.getString("adminName");
ps = rs.getString("adminPwd");
name=name.trim();
ps = ps.trim();
System.out.println(name);
System.out.println(ps);
}

if((adminName.equals(name))&&(adminPwd.equals(ps)))
{
message = "OK";
success = "true";
}
else
{
message = "ERROR";
success = "false";
}
dbConnection.close();
return message;
}catch(SQLException e)
{
message = "输入的用户名或密码错误";
System.out.println(e);
return message;
}
}
public static void main(String args[])
{
AdminLogin admin = new AdminLogin();
String s;

admin.setAdminName("ljn");
admin.setAdminPwd("ljn");
s = admin.getMessage();
System.out.println(s);
}

}
rs.next的结果总是false,那个地方出问题了呢,帮忙看看



...全文
45 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
BillyW 2004-01-15
  • 打赏
  • 举报
回复
连接写在程序里可太不灵活了。准备一个xml配置文件吗!
zhhlk 2004-01-14
  • 打赏
  • 举报
回复
应该加上注释,这样才能让大家看的明白!
hejianling305 2004-01-14
  • 打赏
  • 举报
回复
关注一下a
Leemaasn 2004-01-14
  • 打赏
  • 举报
回复
楼主,,
你的程序写的很不规范啊。
return rs;
}catch(SQLException e)
{
System.err.println(e);
rs = null;
close();
return rs;
}

我估计是你的程序里抛出异常了。。。。
你自己看看是不是。。。


另外,你得好好理一下你的程序的结构,很不规范。
Leemaasn 2004-01-14
  • 打赏
  • 举报
回复
这个怎么看啊。

和数据库结合的。我们也无法再现啊。


自己调调吧。。。

stonecsdn 2004-01-14
  • 打赏
  • 举报
回复
太长了,没时间看了,自己做一个junit测试吧。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧