在servlet中调用时,Mysql查询返回数据集但是空集

时间:2012-10-05 10:12:51

标签: mysql jdbc

我使用的查询如下:

String newq="select *from account"; 
ResultSet rs = s.executeQuery(newq);

该表包含两行但仅返回空集。我怎么能克服这个?

2 个答案:

答案 0 :(得分:0)

package exotiqueDatabaseConnection;

import java.sql.*;
import java.io.PrintWriter;
import java.io.Serializable;

public class ConnectDatabase implements Serializable {

    Statement stmt;
    Connection conn;
    ResultSet rs;
    String username;
    String password;
    String driver;
    String url;

    public ConnectDatabase()
    {
        try{
        username="root";
        password="does001";

        driver="com.mysql.jdbc.Driver";
        Class.forName(driver);

        url="jdbc:mysql://localhost/exotique";
        conn=DriverManager.getConnection(url, username, password);

        stmt=conn.createStatement();

        }
        catch(ClassNotFoundException cnfe)
        {

        }
        catch (SQLException sq)
        {

        }



    }

    public void populateUsers(String usernameIn, String emailIn, String passwordIn)
    {
        try{

    /*  username="root";
        password="does001";

        driver="com.mysql.jdbc.Driver";
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        url="jdbc:mysql://localhost/exotique";
        conn=DriverManager.getConnection(url, username, password);

        stmt=conn.createStatement();*/


        CallableStatement cs=conn.prepareCall("{call populateusers(?,?,?)}");
        cs.setString(1, usernameIn);
        cs.setString(2, emailIn);
        cs.setString(3, passwordIn);
        cs.execute();
        }
        catch(SQLException sq)
        {
            System.out.print(sq.getMessage());
        }
        catch(Exception ex)
        {
            System.out.print(ex.getMessage());
        }
        finally
        {
             close(conn);
             close(rs);
             close(stmt);
        }
    }

    @SuppressWarnings("finally")
    public ResultSet showAllBooks()
    {
        try{



            CallableStatement cs=conn.prepareCall("{call getAllBooks}");

            cs.execute();
            rs=cs.getResultSet();




        }
        catch(SQLException sq)
        {
            System.out.print(sq.getMessage());
        }
        catch(Exception ex)
        {
            System.out.print(ex.getMessage());
        }
        finally
        {
             close(conn);
             close(rs);
             close(stmt);
             return rs;
        }


    }

    public  void close(Statement stmt)
    {
        if(stmt!=null)
        {
            try{
                stmt.close();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

    public  void close(Connection conn)
    {
        if(conn!=null)
        {
            try
            {
                conn.close();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void close(ResultSet rs)
    {
        if(rs!=null)
        {
            try
            {
                rs.close();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
    }



}

答案 1 :(得分:0)

您可以尝试这样的查询...有时候它可以工作......

   select *from dbname.tablename;