Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Jsp Error Cant Get Data from database

Thread view: 
lucifer - 22 Jun 2007 08:10 GMT
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");
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/
mysql","root","aaaa");

   if(conn==null)
    {
       out.print("not connected");
    }
    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"+"<br />.");
        y= rt.getString("id2"+"<br />.");
        str= rt.getString("id3"+"<br />.");
        out.print(x);
        out.print(y);
        out.print(str);
    }
}
catch(Exception ex)
{

       out.print("<br>");
out.print("Timeoooo  "+"<br />");
}
finally
{
    if (rt != null) rt.close();
   if (st != null) st.close();
   if (conn != null) conn.close();
   out.print("<br>");
out.print("Timeoooo"+"<br />");
}

%>
<%
conn = null;
st = null;
ResultSet rs = null;

try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   conn =
     DriverManager.getConnection("jdbc:mysql://localhost:
3306/","root","aaaa");

   st = conn.createStatement();
   rs = st.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();
   %>
</TABLE>
Ooops, something bad happened:
<%
   }  finally {
   if (rs != null) rs.close();
   if (st != null) st.close();
   if (conn != null) conn.close();
   }
%>
</body>
</html>
stefanomnn - 22 Jun 2007 09:05 GMT
> x=rt.getInt("id1"+"<br />.");

what's this???
Lew - 22 Jun 2007 12:22 GMT
>> x=rt.getInt("id1"+"<br />.");
>
> what's this???

What stefanomnn points out is that you are trying to get a value from a column
named
"id1<br />"
and that there isn't any such column in the query.

Is there?  Perhaps if the SELECT listed the columns rather than "*" it would
help to debug the code.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.