2 clannadzy CLANNADZY 于 2013.12.18 23:29 提问

JAVA数据库连接程序跳过某段代码执行了

检查密码是否正确的Checkpasswd类
[code=java]package com.qdgxy.sql;

import java.sql.SQLException;

public class Checkpasswd extends SQL_connection{
private static int i =0;
//select count(*) from consumer where consumer_password='12345678' and exists(
//select * from consumer where consumer_no=00001 )
//final String querystatement="select "+s1+" from "+s2;

    public static void query(String s1,String s2)
    {
        SQL_connection.query(s1,s2);
        System.out.println("query");

/* final String querystatement="select "+s1+" from "+s2;
try {
rs=stmt.executeQuery(querystatement);
} catch (SQLException e) {
e.getMessage();
}*/
}
public static void exe()
{
System.out.println("exe");
try{
i = rs.getInt(1);
System.out.println("exe");
System.out.println(i);
}catch(SQLException e) {
e.getMessage();
}

}
public static int isexist()
{
System.out.println("isexist");
if(i !=0)
return 0;//存在此账户
else
return 1;//不存在此账户
}
}
[/code]

连接数据库的父类SQLException

[code=java]package com.qdgxy.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQL_connection {

/**
 * @param args
 * @throws ClassNotFoundException 
 * @throws SQLException 
 */

/**
 * @param args
 */

    // TODO Auto-generated method stub
    final static String Drivername="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    final static String Connection="jdbc:sqlserver://localhost:1433;DatabaseName=LaboratoryDatabase";
    final static String Username="sa";
    final static String Passwd="123";
    protected static Connection conn=null;
    protected static Statement stmt=null;
    protected static ResultSet rs=null;
    public static void classforname()
    {
        try {
            Class.forName(Drivername);
        }catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.getMessage();
        }
    }
    public static void connection()
    {
        try{
             conn=DriverManager.getConnection(Connection,Username,Passwd);
             System.out.println("conn");
             System.out.println(conn);
            }catch(SQLException e){
                e.getMessage();
            }
    }
    public static void statement()
    {
        try {
            stmt=conn.createStatement();
            System.out.println("stmt");
            System.out.println(stmt);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.getMessage();
        }
    }
     public static void query(String s1,String s2)
     //s1代表列名,s2代表表名
     {
         final String querystatement="select "+s1+" from "+s2;
         try {
                rs=stmt.executeQuery(querystatement);
                System.out.println("rs");
                System.out.println(rs);
            } catch (SQLException e) {
                // TODO Auto-generated catch block 
                e.getMessage();
            }
     }
     public static  void exe()
     {

/* try{
while(rs.next())
{
String s1=rs.getString(1);
System.out.println(s1);
}
}catch(SQLException e) {
e.getMessage();
} /
}
public static void close()
{
System.out.println("close");
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}[/code]
[code=java]btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
String name=text.getText();
String passwd=text_1.getText();
final String s1="count(
)";
final String s2="consumer where consumer_password='"+passwd+"' and exists(select * from consumer where consumer_no='"+name+"');";
Checkpasswd.classforname();
Checkpasswd.connection();
Checkpasswd.statement();
Checkpasswd.query(s1,s2);
Checkpasswd.exe();
Checkpasswd.close();
if(Checkpasswd.isexist() ==0)
{
System.out.println("new mainmenu");
shell.dispose();
new Mainmenu();

            } 
            else{
                create_worrymessagebox();
            }
        }
    });[/code]

数据库语言为[code=sql]select count(*) from consumer where consumer_password='12345678' and exists(
select * from consumer where consumer_no=00001 )[/code]
查询结果为(列名)无列名:(值)1

问题是checkpasswd里的[code=java]try{
i = rs.getInt(1);
System.out.println("exe");
System.out.println(i);
}catch(SQLException e) {
e.getMessage();
} [/code]
不执行,达不到我验证密码要求,为什么这段代码程序不执行,很疑惑,下面那些close()却继续执行了

2个回答

wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2015.12.31 09:47

你调试过了,这段不执行的代码是不是进入异常分支了呢?你把异常堆栈日志打印出来看看。

abcjwg
abcjwg   2016.01.08 08:06

i=rs.getInt(1)这句应该是有异常的,debug调试一下

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!