1

我试图在单个数据库连接中进行不同的查询,但我找不到方法。查询是根据之前结果中的 wery 进行的。这是代码:

String query_disco="SELECT * FROM disco";
    String name = "";
    String adress="";
    String web="";
    int disco_id=0;

    String query_disco_var="SELECT * FROM disco_var";
    float price=0;
    String style="";
    String hin="";
    String hout="";
    String party_name="";
    int day=0;

    String docType="";
    String docTable="";
    String docBody="";

    Calendar calendar = Calendar.getInstance();     

    try {

        DAO a= new DAO();
        ResultSet b =a.executarSQL(query_disco);            
        day = calendar.get(Calendar.DAY_OF_WEEK);

        docType ="<!DOCTYPE HTML PUBLIC\"--�//W3C//DTD HTML 4.0       " +"Transitional//EN\">\n";
        docBody =
                "<HTML>\n"+
                "<HEAD><TITLE>HelloWorld!</TITLE></HEAD>\n"+
                "<BODY BGCOLOR=\"#FDF5E6\">\n"+
                "<H1>Disco</H1>\n"+
                "<table border=1>"+
                "<tr> <th> name </th> <th> adress </th> <th> web </th> <th> price </th>" +
                "<th> style </th> <th> horari </th> <th> party name </th> </tr>";


        while(b.next()){

            disco_id=b.getInt("disco_id");
            name=b.getString("name");
            adress=b.getString("adress");               
            web=b.getString("web");             

            query_disco_var = query_disco_var + " WHERE disco_id = " + disco_id + " AND day = " + day;
            DAO d= new DAO();
            ResultSet c = d.executarSQL(query_disco_var);
            price=c.getFloat("price");
            style=c.getString("style");
            hin=c.getString("hin");
            hout=c.getString("hout");
            party_name=c.getString("party_name");

            docTable = docTable + "<tr> <td> "+name+" </td> <td> "+adress+" </td> <th>" +web+ "</td> </tr>"+
                       price+ "</td> <td>" +style+ "</td> <td>" +hin+ "-" +hout+ "</td> <td>" +party_name+ "</td> </tr>";
            query_disco_var="SELECT * FROM disco_var";

        } 

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    out.println(docType+docBody+docTable+
            "</table>" +
            "</BODY></HTML>");
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

}

与 de db 的连接适用于此脚本:

public class DAO {
private Connection connexio;
private Statement sentencia;
/**
 * 
 * @throws Exception
 */
public DAO() throws Exception {
    String user="trmcfunt";
    String password="44445555";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connexio=
    DriverManager.getConnection("jdbc:mysql://localhost/iguanaparty_db_test?user="+user+"&password="+password); 
        sentencia=connexio.createStatement();
}
/**
 * 
 * @param query
 * @return
 * @throws SQLException
 */
public ResultSet executarSQL(String query)throws SQLException{
    return sentencia.executeQuery(query);
}

/**
 * 
 * @throws SQLException
 */

public void desconnectarBD() throws SQLException {
sentencia.close();
connexio.close();
}

}

我得到这个exepcions:

java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5772)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5692)
at com.mysql.jdbc.ResultSetImpl.getFloat(ResultSetImpl.java:2615)
at com.mysql.jdbc.ResultSetImpl.getFloat(ResultSetImpl.java:2635)
at controllers.HelloWorld.doGet(HelloWorld.java:87)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)

问候。

4

3 回答 3

2
ResultSet c = d.executarSQL(query_disco_var);
price = c.getFloat("price");

在从结果集中获取值之前,您必须调用 next(),将光标放在第一行。否则,如异常中所述,您将在结果集开始之前:

ResultSet c = d.executarSQL(query_disco_var);
if (c.next()) {
    price = c.getFloat("price");

此外,了解预准备语句,因为通过连接参数将参数传递给查询会使您的代码容易受到 SQL 注入攻击,例如,只要其中一个字符串参数包含单引号,ad 就会简单地中断。

于 2013-10-07T09:58:36.977 回答
0

ResultSet c永远不会移动到第一个结果(如果有)。你应该用类似的东西包装你从 c 读取的代码

if (c.next()) {
  // your original code here
} else {
  // whatever you want to do in this case
}

此外,如果找到多个结果,您可能需要考虑该怎么做。

于 2013-10-07T09:59:05.840 回答
0

执行任何 SQL 后,游标位于第一行之前。这就是你得到执行的原因,因为没有可用的数据。

c.next();在执行第二个 SQL 查询后尝试 a 。这会将光标定位在第一个结果上。请记住,如果没有返回数据,则此调用将返回false并且任何后续的列提取都将失败,因此您应该抓住它。

于 2013-10-07T10:01:00.380 回答