User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 426,493 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,239 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 12003 | Replies: 33 | Solved
Reply
Join Date: Feb 2006
Posts: 1,460
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: How to Connect MySQL from JSP Page

  #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  
Join Date: Jan 2008
Posts: 5
Reputation: jatin29 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jatin29 jatin29 is offline Offline
Newbie Poster

Re: How to Connect MySQL from JSP Page

  #12  
Jan 29th, 2008
code to upload a image in mysql by jsp
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,458
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 11
Solved Threads: 296
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

  #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)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Jan 2008
Posts: 5
Reputation: jatin29 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jatin29 jatin29 is offline Offline
Newbie Poster

Re: How to Connect MySQL from JSP Page

  #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  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,458
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 11
Solved Threads: 296
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

  #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)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Jan 2008
Posts: 35
Reputation: electron33 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
electron33 electron33 is offline Offline
Light Poster

Re: How to Connect MySQL from JSP Page

  #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  
Join Date: Apr 2008
Posts: 1
Reputation: praveen56 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
praveen56 praveen56 is offline Offline
Newbie Poster

Re: How to Connect MySQL from JSP Page

  #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  
Join Date: Aug 2007
Location: Somewhere between heaven and hell
Posts: 100
Reputation: Kusno is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
Kusno's Avatar
Kusno Kusno is offline Offline
Junior Poster

Re: How to Connect MySQL from JSP Page

  #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
public class SomeClassName extends HttpServlet
{
Connection conn;
Statement stmt;
	
public void init() throws ServletException
{
	try
	{
		Class.forName("com.mysql.jdbc.Driver").newInstance();
	}
	catch (Exception ex)
	{
		System.out.println("Exception is : " + ex.toString() );
	}
}
	
public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
	try
	{
		conn = DriverManager.getConnection("jdbc:mysql://localhost/" 
													+ "database_name?user=my_username&password=my_password");
		stmt = conn.createStatement();
	}
	catch(SQLException ex)
	{
		System.out.println("SQLException : " + ex.getMessage() );
	}

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  
Join Date: Jan 2008
Posts: 35
Reputation: electron33 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
electron33 electron33 is offline Offline
Light Poster

Re: How to Connect MySQL from JSP Page

  #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  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,458
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 11
Solved Threads: 296
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

  #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)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JSP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 5:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC