How to Connect MySQL from JSP Page

Thread Solved

Join Date: Feb 2006
Posts: 2,387
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 254
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How to Connect MySQL from JSP Page

 
0
  #11
Jan 25th, 2008
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 5
Reputation: jatin29 is an unknown quantity at this point 
Solved Threads: 0
jatin29 jatin29 is offline Offline
Newbie Poster

Re: How to Connect MySQL from JSP Page

 
0
  #12
Jan 29th, 2008
code to upload a image in mysql by jsp
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,191
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: How to Connect MySQL from JSP Page

 
0
  #13
Jan 29th, 2008
Originally Posted by jatin29 View Post
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?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 5
Reputation: jatin29 is an unknown quantity at this point 
Solved Threads: 0
jatin29 jatin29 is offline Offline
Newbie Poster

Re: How to Connect MySQL from JSP Page

 
0
  #14
Jan 29th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,191
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: How to Connect MySQL from JSP Page

 
0
  #15
Jan 29th, 2008
Well I need to point you toward Announcements, that clearly state how this community work. So please read it befor next post
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 36
Reputation: electron33 is an unknown quantity at this point 
Solved Threads: 4
electron33 electron33 is offline Offline
Light Poster

Re: How to Connect MySQL from JSP Page

 
0
  #16
Feb 5th, 2008
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>
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1
Reputation: praveen56 is an unknown quantity at this point 
Solved Threads: 0
praveen56 praveen56 is offline Offline
Newbie Poster

Re: How to Connect MySQL from JSP Page

 
0
  #17
Apr 14th, 2008
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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 177
Reputation: Kusno is an unknown quantity at this point 
Solved Threads: 14
Kusno's Avatar
Kusno Kusno is offline Offline
Junior Poster

Re: How to Connect MySQL from JSP Page

 
0
  #18
May 5th, 2008
Originally Posted by peter_budo View Post
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
  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.
NEVER NEVER NEVER GIVE UP
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 36
Reputation: electron33 is an unknown quantity at this point 
Solved Threads: 4
electron33 electron33 is offline Offline
Light Poster

Re: How to Connect MySQL from JSP Page

 
0
  #19
May 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,191
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: How to Connect MySQL from JSP Page

 
0
  #20
May 5th, 2008
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...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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