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 363,457 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 3,328 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

How to Connect MySQL from JSP Page

Join Date: Aug 2007
Posts: 68
Reputation: Kusno is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
Kusno's Avatar
Kusno Kusno is offline Offline
Junior Poster in Training

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  
All times are GMT -4. The time now is 2:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC