日期:2014-05-20  浏览次数:20964 次

小弟跪求SQL查询问题(附完整代码)
package com.sdzyg.dao;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;

import java.sql.SQLException;
import java.sql.Statement;


public class QueryLogin{
public Connection conn;
String table;
String username;
String password;
String url;
public QueryLogin(String table , String username , String password) throws SQLException, ClassNotFoundException 
{
this.table = table;
this.username = username;
this.password = password;

}
public boolean query() throws SQLException, ClassNotFoundException
{

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/dms?user=root&password=1234"; //连接ODBC数据源
this.conn = DriverManager.getConnection(url); //连接url指定的数据库
Statement stmt = this.conn.createStatement(); //创建语句对象
ResultSet rset = stmt.executeQuery("SELECT * FROM " + table); //执行数据查询select语句,系统走到这步在不往下执行

for(int i = 0; rset.next(); i++) //从前向后访问获得每行数据
if(rset.getString(0).equals(username) &&rset.getString(2).equals(password)) //获得当前行指定列的值
return true;
else
return false;
rset.close();
stmt.close();
return false;
}
public void finalize() throws SQLException
{
this.conn.close();
}
}

------解决方案--------------------
表被锁了吧,或着数据量太大吧
------解决方案--------------------
Java code

public boolean query() {
        ResultSet rset = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/dms?user=root&password=1234"; // 连接ODBC数据源
            conn = DriverManager.getConnection(url); // 连接url指定的数据库
            stmt = conn.createStatement();//创建语句对象
            rset = stmt.executeQuery("SELECT * FROM " + table); // 执行数据查询select语句,系统走到这步在不往下执行

            while (rset.next()) {
                // 从前向后访问获得每行数据
                if (rset.getString(0).equals(username)
                        && rset.getString(2).equals(password)){ // 获得当前行指定列的值
                    return true;
                }
            }
            return false;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rset.close();
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }