954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

jsp with mysql connectivity

hi friends,
This is Anand, am beginner to jsp, now am trying to connect jsp with mysql.
Is this possible to code connection code separately and include each and every page.

am trying the following code.I code these lines in separated.and I want to use it other pages.at the same time I want access my connection variable in other page to access query. Please help me.

<%!
        Connection con;
        String url="jdbc:mysql://localhost:3306/";
        String Driver="com.mysql.jdbc.Driver";
        %>
        
        
        <%
        try{
            // connection codings
        Class.forName(Driver).newInstance();
        con=DriverManager.getConnection(url,"school","");
        
        
        }
        catch(Exception e)
        {
        System.out.println(e.getMessage());
        }
        %>


Thanks,
Anand

anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Delete everything. Never put that code in a jsp.

Have a class with a method that connects to the database, does whatever you want and returns the result. Call that method from the jsp.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

thanks both of U.

anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You