• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

displaying data from mysql database.

 
Harshal Gurav
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In main.jsp i wish to display the data from the database( user information stored in user table ). my codein main.jsp is:

<h2> description about user:</h2>
<% Statement st = null;
ResultSet rs = null;

rs = st.executeQuery("Select description from user ");
%>
<option value = "<%=rs.getString("description")%>" >
</option>
when i run the above code it shows the error:
java.lang.NullPointerException
org.apache.jsp.main_jsp._jspService(main_jsp.java:72)
like rs=st.execute....
can anybody have solution for above or any alternative code.
Thanks and regards
Harshal
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't be doing JDBC code in a JSP. That's a very bad practice.

But how can you look at the following two lines of code:

and not see how that will create a null pointer exception?

I've moved this to the JDBC forum as it has nothing to do with JSP.
 
Jetendra Ivaturi
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As breat told we are not supposed to use Jdbc code in Jsp.

If you would like to retrive the data from a table(irrespective or database).

These things are manadatory.

1. Importing the package. import java.sql.*;

2. Loading the Driver Class.forName("the driver class which yu want to use");

3. Connecting to database

Connection con=DriverManager.getConnection(URL);
4. Creatign the statement(which you did not do)

Statement st=con.createStatement();

5. Query

for trasactional query
ResultSet ra=st.executeQuery("select..");
6. Close the connection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic