Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xml from jsp

 
vartika vaish
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

We are trying to generate an xml file using jsp. the code written is attached below. this si what we have been doing is in MyEclipse.

When we run this it gives us unable to load xml xml format invalid.

kindly look at the code and see what changes need to take.

this is being used for dhtmlxGrid dynamic rendering.

please help..

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ page import = "java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

</head>

<body>

<%

//String db_ipp_addr = "localhost";
//String db_username = "root";
//String db_password = "1";
//String db_name = "sampleDB";

// set content type and xml tag

response.setContentType("text/xml");
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

// define variables from incoming values
String posStart = "";
if (request.getParameter("posStart") != null){
posStart = request.getParameter("posStart");
}else{
posStart = "0";
}

String count = "";
if (request.getParameter("count") != null){
count = request.getParameter("count");
}else{
count = "100";
}

// connect to database
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
//String connectionURL = "jdbc:mysql://" + db_ipp_addr + ":3306/" + db_name;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


//com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbcdbc:data");


//, db_username, db_password);

// query to products table
String sql = "SELECT * FROM Employee";

// if this is the first query - get total number of records in the query result
String totalCount = "";
if (posStart.equals("0")){
String sqlCount = "Select count(*) as cnt from Employee";
statement = connection.createStatement();
rs = statement.executeQuery(sqlCount);
rs.next();
totalCount = rs.getString("cnt");

rs.close();
} else {
totalCount = "";
}

// add limits to query to get only rows necessary for output
// sql += " LIMIT " + posStart + "," + count;

// Execute the query
statement = connection.createStatement();
rs = statement.executeQuery(sql);

// output data in XML format
out.println("<rows total_count='" + totalCount + "' pos='" + posStart + "'>");
int id = 0;
while (rs.next()) {
String Str_id = id + " ";

out.println("<row id=\"" + Str_id + "\">");
out.println("<cell>");
out.println(rs.getString("empId")); // value for product name
out.println("</cell>");
out.println("<cell>");
out.println(rs.getString("empName")); // value for internal code
out.println("</cell>");
out.println("<cell>");
out.println(rs.getString("empPass")); // value for price
out.println("</cell>");
out.println("</row>");
id++;
}
out.write("</rows>");
rs.close();
%>
</body>
</html>


please help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic