943,864 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Marked Solved
  • Views: 39082
  • JSP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 25th, 2008
0

Re: How to Connect MySQL from JSP Page

Use a bean, as it is suppossed to be done.

You shouldn't have any scriptlets in your code anyway, so having to duplicate the code over and over again shouldn't be a problem in the first place.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jan 29th, 2008
0

Re: How to Connect MySQL from JSP Page

code to upload a image in mysql by jsp
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jatin29 is offline Offline
5 posts
since Jan 2008
Jan 29th, 2008
0

Re: How to Connect MySQL from JSP Page

Click to Expand / Collapse  Quote originally posted by jatin29 ...
code to upload a image in mysql by jsp
What we are suppost to make out of this word collection as it is not a sentence?
Secondly what relevance it has to original topic discussed here previously?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jan 29th, 2008
0

Re: How to Connect MySQL from JSP Page

I want to insert image in mysql by using jsp I have error in my jsp code so if u know how to insert it help me and if possible send me code.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jatin29 is offline Offline
5 posts
since Jan 2008
Jan 29th, 2008
0

Re: How to Connect MySQL from JSP Page

Well I need to point you toward Announcements, that clearly state how this community work. So please read it befor next post
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Feb 5th, 2008
0

Re: How to Connect MySQL from JSP Page

Don't mix Servlet and JSP. Add the following code into a JSP page. Don't put it inside a function. JSP pages don't use function. Servlets do.

By the way. Database connections and business logic does not belong in the JSP pages. Move them down to Servlet or other specialized classes for the task.

<html>
<head>
    <title></title>
</head>
<body>
    
   <% 
        try{
            java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8009/test", "root", "asma");
            java.sql.PreparedStatement stmt = conn.prepareStatement("SELECT * FROM test");
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){

            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
   %>
</body>
</html>
Reputation Points: 30
Solved Threads: 4
Light Poster
electron33 is offline Offline
36 posts
since Jan 2008
Apr 14th, 2008
0

Re: How to Connect MySQL from JSP Page

hi,
u hve to copy a file fom jdk\lib\tools.jar file and paste it into tomcat 5.0\common\lib
after doing this u can compile your jsp file..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
praveen56 is offline Offline
1 posts
since Apr 2008
May 5th, 2008
0

Re: How to Connect MySQL from JSP Page

Click to Expand / Collapse  Quote originally posted by peter_budo ...
First thing, please do not use JSP to connect to database. Pass data to servlet for validation, or use JavaScript to valildate them in JSP, and setup connection inside servlet and then by the needs go back to previous page to pick up correct/new data or move to nex page
this is usual peace of code which I re-use often
JSP Syntax (Toggle Plain Text)
  1. public class SomeClassName extends HttpServlet
  2. {
  3. Connection conn;
  4. Statement stmt;
  5.  
  6. public void init() throws ServletException
  7. {
  8. try
  9. {
  10. Class.forName("com.mysql.jdbc.Driver").newInstance();
  11. }
  12. catch (Exception ex)
  13. {
  14. System.out.println("Exception is : " + ex.toString() );
  15. }
  16. }
  17.  
  18. public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  19. {
  20. try
  21. {
  22. conn = DriverManager.getConnection("jdbc:mysql://localhost/"
  23. + "database_name?user=my_username&password=my_password");
  24. stmt = conn.createStatement();
  25. }
  26. catch(SQLException ex)
  27. {
  28. System.out.println("SQLException : " + ex.getMessage() );
  29. }

PS: I will request this post to be move to JSP section, it is where it actualy belong and where you can find more similar questions and answers

How do I implement this class in JSP page ?

Thanks,

Kusno.
Reputation Points: 11
Solved Threads: 17
Junior Poster
Kusno is offline Offline
191 posts
since Aug 2007
May 5th, 2008
0

Re: How to Connect MySQL from JSP Page

This class is the JSP. JSP is an extension of Servlet. Simply told. If you see a few pages up on this article you see how to create a connection in JSP. But as i already had sead. Don't use connection or other business related code inside JSP. It makes it more difficult to read Tags and Java code. Move the Connection and Query code down in a DAO layer and use Servlet class as controler. Put the data you want into a JavaBean and put it into Request or session. Get it back in the JSP page and view it. If you need help in how to implement it give me a reply
Reputation Points: 30
Solved Threads: 4
Light Poster
electron33 is offline Offline
36 posts
since Jan 2008
May 5th, 2008
0

Re: How to Connect MySQL from JSP Page

This is a wrong approach!
JSP => presentation purpose
Servlet => logic execution
That mean servlet is in charge of database connection. Get your self up to date...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Unable to connect to tomcat through browser
Next Thread in JSP Forum Timeline: MVC help





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


Follow us on Twitter


© 2011 DaniWeb® LLC