JSP - Database problem

Reply

Join Date: Aug 2009
Posts: 4
Reputation: TryingToDo is an unknown quantity at this point 
Solved Threads: 0
TryingToDo TryingToDo is offline Offline
Newbie Poster

JSP - Database problem

 
0
  #1
Aug 19th, 2009
Before you killing me, I know about the:
http://www.daniweb.com/forums/thread141776.html , but for the moment, I DON"T have the time.

Secondly, I'm new to Java platform and JSP.

I'm trying to make a simple application, but I don't get it.

I'm using Intellij 8, Tomcat 6, MySQL

I have a java class which makes the connection with the database and a jsp file, but is not working.

The java file:
  1. package com.packages;
  2.  
  3. import java.sql.*;
  4.  
  5. public class conexiune
  6. {
  7. String URL = "jdbc:mysql://localhost:3306/agenda";
  8. String user = "root";
  9. String pass = "root";
  10. Connection con;
  11. Statement stat;
  12. PreparedStatement pre;
  13. ResultSet res;
  14.  
  15.  
  16. public void creeaza()
  17. {
  18. try
  19. {
  20. Class.forName("com.mysql.jdbc.Driver");
  21. con = DriverManager.getConnection(URL, user, pass);
  22. }
  23. catch(Exception ex)
  24. {
  25.  
  26. }
  27. }
  28.  
  29. public void inchide()
  30. {
  31. try
  32. {
  33. con.close();
  34. }
  35. catch(Exception ex)
  36. {
  37.  
  38. }
  39. }
  40.  
  41.  
  42. public ResultSet interogheaza(String sql)
  43. {
  44. try
  45. {
  46. stat = con.createStatement();
  47. res = stat.executeQuery(sql);
  48. return res;
  49. }
  50. catch(Exception ex)
  51. {
  52. return null;
  53. }
  54. }
  55.  
  56. public ResultSet interogheazaPrepared(String Sql, String Sql2)
  57. {
  58. try
  59. {
  60. pre = con.prepareStatement(Sql);
  61. pre.setString(1, Sql2);
  62. res = pre.executeQuery ();
  63. return res;
  64. }
  65. catch(Exception ex)
  66. {
  67. return null;
  68. }
  69. }
  70. }

The JSP file:
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <%@ page import="java.sql.*" %>
  3. <jsp:useBean id="conex" scope="page" class="com.packages.conexiune" />
  4.  
  5. <html>
  6. <head>
  7. <title>
  8. Lista contactelor
  9. </title>
  10.  
  11. <SCRIPT LANGUAGE="JavaScript">
  12.  
  13. </SCRIPT>
  14. </head>
  15.  
  16. <body>
  17.  
  18.  
  19.  
  20. <form name="form" action="index.jsp" method="get">
  21. Cautare
  22. <input name="cauta" type="text">
  23. <input type="submit" name="nume" value="Nume">
  24. <input type="submit" name="oras" value="Oras">
  25. </form>
  26. <form name="form" action="afisare.jsp" method="get">
  27.  
  28.  
  29. <table border="0" cellspacing=0 width="100%">
  30. <tr>
  31. <th>Lista contactelor</th>
  32.  
  33. </tr>
  34. </table>
  35. <br/>
  36.  
  37. <table border="1" cellspacing=0 width="100%">
  38. <tr>
  39. <th>Nr. crt.</th>
  40. <th>Nume</th>
  41. <th>Prenume</th>
  42. <th>Adresa</th>
  43. <th>Zip</th>
  44. <th>Oras</th>
  45. <th>Telefon</th>
  46. <th>Civila</th>
  47. <th>Nr. copii</th>
  48. <th>Adresa mail</th>
  49. </tr>
  50.  
  51.  
  52. <%
  53.  
  54. conex.creeaza();
  55. int i = 1;
  56. ResultSet res;
  57. res = conex.interogheaza("SELECT * from adresa");
  58. while (res.next())
  59. {
  60. %>
  61. <tr>
  62.  
  63. <td align="center"> <input type="radio" name="punct" checked="checked" value="<%=res.getString("id") %>"><%=i %></td>
  64. <td align="center"><%=res.getString("Nume") %></td>
  65. <td align="center"><%=res.getString("Prenume")%></td>
  66. <td align="center"><%=res.getString("Adresa")%></td>
  67. <td align="center"><%=res.getString("Cod_Zip")%></td>
  68. <td align="center"><%=res.getString("Oras")%></td>
  69. <td align="center"><%=res.getString("Nr_telefon")%></td>
  70. <td align="center"><%=res.getString("Stare_civila")%></td>
  71. <td align="center"><%=res.getString("Nr_copii")%></td>
  72. <td align="center"><%=res.getString("Adresa_Mail")%></td>
  73. <%
  74. i++;
  75. }
  76. %>
  77. </tr>
  78.  
  79. </table>
  80.  
  81. <br/>
  82. <input type="submit" name="adaug" value="Adauga">
  83. <input type="submit" name="modific" value="Modifica" >
  84. <input type="submit" name="sterg" value="Sterge" onclick="return confirm('Doriti stergerea persoanei selectate?');" />
  85.  
  86. </form> </body>
  87. </html>

When I'm trying to run the app, I receive this error:

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 65

62: int i = 1;
63: ResultSet res;
64: res = conex.interogheaza("SELECT * from adresa");
65: while (res.next())
66: {
67: %>
68: <tr>
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: hajjo is an unknown quantity at this point 
Solved Threads: 0
hajjo hajjo is offline Offline
Junior Poster in Training

Re: JSP - Database problem

 
0
  #2
Aug 19th, 2009
i dont know mysql..

but with toad. you have to open toad for oracle. then click on tnsnames editor. then click on orcl.

there is something called host= something, port="number" and servicename=something

"jdbc:mysql://something:number/something";


you have to replace those. plus also the user pass you have to put the username and password for the database. how do you enter your sql? you type a username and password. you have to put thoser two.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: JSP - Database problem

 
0
  #3
Aug 20th, 2009
There are quite a few things I would like to tell you in this post:-
  • Firstly never ever use the root account in mysql, apart from for performing administrative tasks.
  • Telling that you do not have the time for following Good Design (and following MVC) is like telling you don't have the time to do any research before spending millions on buying a lavish new sports car, eventually you are going to regret it.
  • Your catch blocks are not only for show, or just to stop the compiler from complaining of uncaught exceptions, they are there so that you handle those exceptional situations by writing contingency plans there or at least print a simple stack trace of the exception so that you can signal yourself or the rest of the program of problems like not able to establish a database connection etc.
  • Next is follow consistent indentation, if not for anyone else, it will ultimately aid you in reading your own code.
  • Do not return a ResultSet directly from a function, a ResultSet requires that its associated database connection be open throughout the time the ResultSet is needed, Use a CachedRowSet in case you have to return a ResultSet, or the best approach would be what Peter followed in the MVC thread.
Last edited by stephen84s; Aug 20th, 2009 at 5:01 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC