一个让我吐血的程序````

colorfulxy 2007-12-06 11:59:55
import java.io.*;
import java.util.*;
import java.sql.*;
public class bean
{
Connection Con=null;
public bean(){BuildConnection();}
public void BuildConnection(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Con = DriverManager.getConnection("jdbc:odbc:xxy","sa","");

}catch (Exception e) {
System.out.println(e.getMessage());}
}

public Connection getConnection()
{
if(Con==null)
BuildConnection();
return this.Con;
}
public void close()
{try{
Con.close();
Con=null;
}
catch(SQLException e){
System.out.println(e.getMessage());
}
}
try{
String pass;

Statement stm=Con.createStatement();




public String checkd(String username,String password)
{

String sql="select password from dl where name='"+ username + "'";
ResultSet rs=stm.executeQuery(sql);
if(rs==null)
return("请输入用户名!");
while(rs.next()){
pass = rs.getString(2);

}
if(pass.equals(password))
return("登录成功!");
else
return("密码错误!");

}
}
catch(SQLException e){
System.out.println(e.getMessage());
}



}

哪位高手帮忙指点下错误````小弟感激不尽


...全文
446 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
colorfulxy 2007-12-06
  • 打赏
  • 举报
回复
楼上说的我试过了``还是不行啊``
你们还是亲自调试一下吧``
seaforce 2007-12-06
  • 打赏
  • 举报
回复
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
这句话怎么没有对象呀,只是一个表达式,你把你要定义的类加在前面就OK了!!!
这样试试:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
colorfulxy 2007-12-06
  • 打赏
  • 举报
回复
这个程序就这么难啊```
都没人能调试出来么
olivesoup 2007-12-06
  • 打赏
  • 举报
回复
另外修改了一处逻辑错误
while (rs.next()) → if (rs.next())
colorfulxy 2007-12-06
  • 打赏
  • 举报
回复
晕``4个错误``它说的try那是非法的表达试开始```
高手些帮我调试一下嘛``拜托了
olivesoup 2007-12-06
  • 打赏
  • 举报
回复
只能说没语法错误了

public class bean {
Connection Con = null;

public bean() {
BuildConnection();
}

public void BuildConnection() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Con = DriverManager.getConnection("jdbc:odbc:xxy", "sa", "");

} catch (Exception e) {
System.out.println(e.getMessage());
}
}

public Connection getConnection() {
if (Con == null)
BuildConnection();
return this.Con;
}

public void close() {
try {
Con.close();
Con = null;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

public String checkd(String username, String password) {
String pass = null;
try {

Statement stm = Con.createStatement();

String sql = "select password from dl where name='"
+ username + "'";
ResultSet rs = stm.executeQuery(sql);
if (rs == null)
return ("请输入用户名!");
if (rs.next()) {
pass = rs.getString(2);
}

} catch (SQLException e) {
System.out.println(e.getMessage());
}
if (pass.equals(password))
return ("登录成功!");
else
return ("密码错误!");

}

}
colorfulxy 2007-12-06
  • 打赏
  • 举报
回复
呵呵``我要说严重点才有人帮我啊```我昨天弄了一个晚上都没弄出来``
谢谢了``我去试下``
对了``怎么给你们加分的啊`?
for_cyan 2007-12-06
  • 打赏
  • 举报
回复
如果是try块里面错误,将Exception范围改小为ClassNotFoundException
如果是这个异常,就是你的classpath中没有这个驱动
for_cyan 2007-12-06
  • 打赏
  • 举报
回复
从你给出的信息根本看不出你吐血的理由
colorfulxy 2007-12-06
  • 打赏
  • 举报
回复
xxy是数据源名称啊``那个没错吧``就是提示try那有错``
搞的我莫名其妙
macrochao 2007-12-06
  • 打赏
  • 举报
回复
xxy是什么呀?你到底连的是什么数据库啊?其他应该没什么问题,数据库驱动写错了吧?
yanqiaofang 2007-12-06
  • 打赏
  • 举报
回复
太大意了!
lanhai6666 2007-12-06
  • 打赏
  • 举报
回复
哈哈
colorfulxy 2007-12-06
  • 打赏
  • 举报
回复
20楼``高手
w11x22b33 2007-12-06
  • 打赏
  • 举报
回复
rs.getString(1)
raojl 2007-12-06
  • 打赏
  • 举报
回复
try{
String pass;

Statement stm=Con.createStatement();
public String checkd(String username,String password) //try都没完,你就来个函数,肯定报非法表达式错误落!悲哀。。。
不懂编程 2007-12-06
  • 打赏
  • 举报
回复
我用的是MyEclipse测的
不懂编程 2007-12-06
  • 打赏
  • 举报
回复
我用“冬瓜”的程序修改了一下,测试通过了,你的select语句只到了一个列值,pass = rs.getString(2)有错,改成pass = rs.getString(1)就可以了,我把库连接变了,你自己改回来看看吧

import java.sql.*;

public class pp {
Connection Con = null;

public pp() {
BuildConnection();
}

public void BuildConnection() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Con = DriverManager.getConnection("jdbc:odbc:wd", "system", "manager");

} catch (Exception e) {
System.out.println(e.getMessage());
}
}

public Connection getConnection() {
if (Con == null)
BuildConnection();
return this.Con;
}

public void close() {
try {
Con.close();
Con = null;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

public String checkd(String username, String password) {
String pass = null;
try {
Statement stm = Con.createStatement();
String sql = "select aa from wdtest where aa='" + username + "'";
ResultSet rs = stm.executeQuery(sql);
if (rs == null)
return ("Please insert user name!");
if (rs.next()) {
pass = rs.getString(1);
}

} catch (SQLException e) {
System.out.println(e.getMessage());
}
if (pass.equals(password))
return ("OK!");
else
return ("password error!");

}

public static void main(String[] args){
pp z = new pp();
String info = z.checkd("test", "test");
System.out.println(info);
z.close();
}
}
zm_hs 2007-12-06
  • 打赏
  • 举报
回复
import java.io.*;
import java.util.*;
import java.sql.*;

public class bean {
Connection Con = null;

public bean() {
BuildConnection();
}

public void BuildConnection() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Con = DriverManager.getConnection("jdbc:odbc:xxy", "sa", "");

} catch (Exception e) {
System.out.println(e.getMessage());
}
}

public Connection getConnection() {
if (Con == null)
BuildConnection();
return this.Con;
}

public void close() {
try {
Con.close();
Con = null;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

public String checkd(String username, String password) {
try {
String pass=null;

Statement stm = Con.createStatement();
String sql = "select password from dl where name='"
+ username + "'";
ResultSet rs = stm.executeQuery(sql);
if (rs == null)
return ("请输入用户名!");
while (rs.next()) {
pass = rs.getString(2);

}
if (pass.equals(password))
return ("登录成功!");
else
return ("密码错误!");

} catch (SQLException e) {
System.out.println(e.getMessage());
return "";
}
}

}
zm_hs 2007-12-06
  • 打赏
  • 举报
回复
你close()方法下面的那个try 写在方法外面了..肯定有错啊
加载更多回复(7)

62,623

社区成员

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

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