943,871 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 598
  • JSP RSS
Aug 19th, 2009
0

JSP - Database problem

Expand Post »
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:
JSP Syntax (Toggle Plain Text)
  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:
JSP Syntax (Toggle Plain Text)
  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:

Quote ...
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>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TryingToDo is offline Offline
4 posts
since Aug 2009
Aug 19th, 2009
0

Re: JSP - Database problem

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
hajjo is offline Offline
101 posts
since Mar 2008
Aug 20th, 2009
0

Re: JSP - Database problem

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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: cant access servlet context object
Next Thread in JSP Forum Timeline: Jsp with Microsoft Sql Server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC