使用java在一个连接中进行多个SQL查询

时间:2013-10-07 09:54:21

标签: java mysql sql

我正在尝试在单个数据库连接中进行不同的查询,但我找不到方法。 查询是根据之前结果的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();
}

}

我正在接受这个问题:

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)

问候。

3 个答案:

答案 0 :(得分: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");

另外,了解prepared statements,因为通过连接参数将参数传递给查询会使您的代码容易受到SQL注入攻击,广告只会在一个字符串参数包含单个参数时立即中断报价。

答案 1 :(得分:0)

您的ResultSet c永远不会移动到第一个结果(如果有的话)。你应该使用类似

之类的东西来包装你的代码
if (c.next()) {
  // your original code here
} else {
  // whatever you want to do in this case
}

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

答案 2 :(得分:0)

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

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