0

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

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by anand01
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.