MySQL DB连接速度无法正常工作

时间:2014-12-20 06:43:12

标签: java mysql velocity

我一直在尝试使用Velocity创建一个HTML页面,该页面显示存储在名为users的SQL表中的名称。当我运行我的程序时,它会打印以下堆栈跟踪:

java.sql.SQLException
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5528)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
    at com.sarvika.velocity.HelloWorld.CfNMHTMLParser.main(CfNMHTMLParser.java:41)

我确信我确实导入了所有必需的库和MySQL连接的驱动程序。我正在使用mysql-connector-java-5.1.6。我的代码是:

package com.sarvika.velocity.dbtovelocity;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.velocity.*;
import org.apache.velocity.app.VelocityEngine;

public class CfNMHTMLParser {

private final static String URL = "jdbc:mysql://localhost/coffeenet";
private final static String USER = "root";
private final static String PASS = "root";

private static final String BOOL_EXC = "exception";
private static final String EXC = "exc";
private static final String USER_LIST = "usersArray";

    public static void main (String [] args)
            throws IOException {

        VelocityEngine engine = new VelocityEngine();
        Template template = engine.getTemplate("HTMLTemplate.vm");
        VelocityContext context = new VelocityContext();

        CfNMHTMLParser parser = new CfNMHTMLParser();

        String [] data = null ;
        try {

            Connection conn = DriverManager.getConnection(URL, USER, PASS);
            Statement stmt = conn.createStatement();

            String query = "SELECT count(*) FROM users ;";
            ResultSet rs = stmt.executeQuery(query);

            int count = Integer.parseInt(rs.getString("count(*)"));

            data = new String[count];

            query = "SELECT AuthID FROM users ;";
            rs = stmt.executeQuery(query);

            for (int i = 0 ; i < count ; i++) {
                rs.next();
                data[i] = rs.getString("AuthID");
            }

    //          data = new String [] {
    //                  "developers",
    //                  "developers",
    //                  "developers",
    //                  "developers"
    //          };

        } catch (SQLException ex) {

            ex.printStackTrace();
            context.put(BOOL_EXC, true);
            context.put(EXC, ex.getMessage());

        }

        context.put(USER_LIST, data);

        StringWriter templateData = new StringWriter();
        template.merge(context, templateData);

        File htmlFile = new File("page.html");
        FileWriter fileWriter = new FileWriter(htmlFile);
        BufferedWriter writer = new BufferedWriter(fileWriter);
        writer.write(templateData.toString());

        writer.close();

    }

}

文件HTMLTemplate.vm

#macro( makeUserTable $userListObjArray )
    #foreach( $user in $userListObjArray )
        <font size="7" color="ffccaa">
            $user
        </font>
        <BR>
    #end
#end

<html>

    <title>CoffeeNet Messenger Users</title>

    #if ($exception == true)
        <font size="48">Exception : $exc </font>
    #else
        #makeUserTable( $usersArray )
    #end

</html>

请告诉我我犯了哪个错误......

2 个答案:

答案 0 :(得分:1)

错误在此行rs.getString(“count(*)”)中,因此您可以更改这两行:

String query = "SELECT count(*) FROM users ;";
...            
int count = Integer.parseInt(rs.getString("count(*)"));

分别在:

String query = "SELECT count(*) as tot FROM users ;";
...            
int count = Integer.parseInt(rs.getString("tot"));

对于Velocity我认为你必须是init才能添加这一行:

    VelocityEngine engine = new VelocityEngine();
    engine.init(); // line to add
    Template template = engine.getTemplate("HTMLTemplate.vm");

答案 1 :(得分:0)

我发现了问题所在。这不是因为

    String query = "SELECT count(*) FROM users ;";      

    ResultSet rs = stmt.executeQuery(query);

    int count = Integer.parseInt(rs.getString("count(*)"));

这里,堆栈轨道说什么

    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815)

它告诉我们检查行位置,在这种情况下什么都不是。 那么正确的代码是

    String query = "SELECT count(*) FROM users ;"; 

    rs.next();     

    ResultSet rs = stmt.executeQuery(query);

    int count = Integer.parseInt(rs.getString("count(*)"));

使用列别名不算什么。