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

Recommended Answers

All 3 Replies

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.

thanks both of U.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.