Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 916703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:05:33+00:00 2026-05-15T18:05:33+00:00

I created a Java Servlet that get query result from mySQL database and print

  • 0

I created a Java Servlet that get query result from mySQL database and print it in XML format.
the problem is that it takes a very long time, something about three minutes to print the xml result while in my PHP script it takes 5 seconds.

My Servlet relevant function is:
( run a query and return the xml in a String variable, then, print it to the page )

public String QueryResult(String query)
{
    String retStr;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection ("jdbc:mysql://"+this.host+":"+this.port+"/"+this.db, this.user, this.pass);
        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery(query);
        ResultSetMetaData rsMetaData = rset.getMetaData();

        retStr = "<Result>\n";
        while (rset.next())
        {
            retStr += "\t<Item>\n";
            for (int i=1;i<=rsMetaData.getColumnCount();i++)
            {
                retStr += "\t\t<"+rsMetaData.getColumnName(i)+">"+ rset.getString(i) + "</"+rsMetaData.getColumnName(i)+">\n";
            }
            retStr += "\t</Item>\n";
        }
        retStr += "</Result>\n";
        stmt.close();
        conn.close();
    }
    catch(Exception e) 
    {
        return "<Result><Error>"+e.toString()+"</Error></Result>";
    }
    return retStr;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:05:34+00:00Added an answer on May 15, 2026 at 6:05 pm

    String Concatenation:

    First of all, when speed matters, you do not want to concatenate strings the way you do. Each time you concatenate a String you are creating a new one.

    Better use StringBuilder with a well planned capacity, as the default one won’t probably suit your need judging from the code snippet you show.

    Also See:

    String, StringBuffer, and StringBuilder

    Why to use StringBuffer in Java instead of the string concatenation operator

    XML:

    XStream is a simple library to
    serialize objects to XML and back
    again.

    Might not be related to your performance issue but might come in handy: XStream

    “Performance” does figure in their features list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a servlet in java that will give me a xml response when
I created Java object and I used JAXB to convert that object into XML.
I have recently just created Java project using Eclipse that requires 2 JAR files
I created a Java program which sends an email from my gmail account. I
I've created a Java class that connects to an IIS website requiring NTLM authentication.
I created a simple XML web service using NetBeans 7's RESTful Web Services from
I have a YUI dialog that submits a form to a Java servlet. The
I have a Java EE 6 servlet that creates an instance of FooBarModelImpl and
I just wonder why does only one copy of a servlet get created. And
I understand that Servlet requests are by default multithreaded. I created a simple servlet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.