读书人

JAVA数据库连接有关问题

发布时间: 2014-01-21 00:35:39 作者: rapoo

JAVA数据库连接问题
检查密码是否正确的Checkpasswd类

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;//不存在此账户
}
}


连接数据库的父类SQLException

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();
}
}
}

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();


}
}
});


数据库语言为
select count(*) from consumer where consumer_password='12345678' and exists(
select * from consumer where consumer_no=00001 )

查询结果为(列名)无列名:(值)1

问题是checkpasswd里的
try{
i = rs.getInt(1);
System.out.println("exe");
System.out.println(i);
}catch(SQLException e) {
e.getMessage();
}

不执行,达不到我验证密码要求,为什么这段代码程序不执行,很疑惑,下面那些close()却继续执行了
[解决办法]
没看到rs.next()方法,rs是指针,next方法后才是第一个值。
[解决办法]
private static int i =0;

i怎么能用static呢?这个错误非常严重。

读书人网 >J2SE开发

热点推荐