| lucifer |
Jun 22nd, 2007 3:55 AM |
Jsp Error Cant Get Data from database
Hi
i am writing a JSP page which connects to Mysql DataBase
i can get the connection but i cant get to execute the query on the database
plz help
the code of jsp file
:
<%@ page import ="java.lang.*" %>
<%@ page import ="java.sql.*" %>
<html>
<head>
<title>
Index Page
</title>
</head>
<body>
jsp pages is better than ASP.NET
<%
int x;
Connection conn=null;
Statement st=null;
ResultSet rt=null;
String query="select * from user";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","aaaa");
out.print("<br>"+"Connection"+conn+"<br>");
try
{
st = conn.createStatement();
rt=st.executeQuery("select * from a.a");
while(rt.next())
{
String y;
String str;
x=rt.getInt("id1");
y= rt.getString("id2");
str= rt.getString("id3");
out.print("id1 is"+x+"<br />");
out.print("id2 is"+y+"<br />");
out.print("id3 is"+str+"<br />");
}
}
catch(Exception ex)
{
out.print("<br>");
out.print("Timeoooo "+"<br />"+ex);
}
finally
{
if (rt != null) rt.close();
if (st != null) st.close();
if (conn != null) conn.close();
out.print("<br>");
}
Connection con = null;
Statement str = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","aaaa");
str = con.createStatement();
rs = str.executeQuery("select * from a.employees");
while(rs.next()) {
%>
<TR><TD><%= rs.getString("lname_txt") %></TD>
<TD><%= rs.getString("fname_txt") %></TD></TR>
<%
}
%>
</TABLE>
<%
} catch (Exception ex) {
ex.printStackTrace();
out.print(ex);
%>
</TABLE>
Ooops, something bad happened:
<%
} finally {
if (rs != null) rs.close();
if (str != null) str.close();
if (con != null) con.close();
}
%>
</body>
</html>
|