Results 1 to 3 of 3
  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default JSP database connection problem

    I am retrieving the name of countries from my database. I wrote a class that uses the singleton pattern to handle the database connection. the problem is that when I run the web app i am getting this error:
    org.apache.jasper.JasperException: Unable to compile class for JSP:

    An error occurred at line: 7 in the generated java file
    Syntax error on token ";", delete this token

    An error occurred at line: 89 in the jsp file: /index.jsp
    The method getDatabaseInstance() is undefined for the type Database
    86: <select style="width:100px;" class="option">
    87: <%
    88: //Establishes connection to database
    89: Database database = Database.getDatabaseInstance ();
    90: database.initialize("jdbc:mysql://localhost/invent", "root","");
    91: database.connect();
    92: Statement statement = database.getStatement();

    Here is the code for the Database class:
    Java Code:
    package Classes;
    import java.sql.*;
    /**
     *
     * @author Mario
     */
    public class Database {
        private static Database database;
        private Connection con;
        private Statement statement;
        private String databaseName,username,password;
        
        //constructor
        private Database () {}
        
        
        /**
         * A static method that returns a 
         * instance of Database class.
         * 
         * @author Mario Dennis
         * @return Database     */
        public static Database getDatabaseInstance ()
        {
            //creates database instance if 
            //one doesn't exist
            if (database == null)
            {
                database = new Database ();
                return database;
            }//end if  
            
            else
                return database;
        }//end getDatabaseInstance method
        
        
        /**
         * initializes connection variables to connect to database.
         * 
         * @param databaseName
         * @param username
         * @param password   */
        public void initialize (String databaseName,String username,String password)
        {
            //initialize connection variable if they are empty
           if (databaseName == null || username == null || password == null)
           {
               this.username = username;
               this.password = password;
               this.databaseName = databaseName;
           }//end if
        }//end initiate method
        
        
        /**
         * 
         * Creates connection to database
         * 
         * @author Mario Dennis
         * @return void     */
        public void connect ()
        {
            //connects to database if connection 
            //doesn't already exists
            if (con == null)
            {
                try
                {
                    Class.forName("com.mysql.jdbc.Driver");
                    con = DriverManager.getConnection (databaseName,username,password);
                    statement = con.createStatement ();
                }
                catch (Exception e)
                {
                    System.err.println (e.getMessage ());
                }
            }//end if
        }//end connect method
        
        
        /**
         * Gets statement to query database.
         * Return null is statement not created.
         * 
         * @author Mario Dennis
         * @return  Statement     */
        public Statement getStatement ()
        {
            return statement;
        }//end getStatement method
        
    }//end Database class
    Here is the core for the jsp:
    Java Code:
     
    <%@page import="java.sql.*,classes.*;" %>
    <%@page import="Classes.Database"%>
     <select style="width:100px;" class="option">
                                            <% 
                                                //Establishes connection to database
                                                Database database = Database.getDatabaseInstance ();
                                                database.initialize("jdbc:mysql://localhost/invent", "root","");
                                                database.connect();
                                                Statement statement = database.getStatement();
                                                
                                                //validate statement
                                                if (statement != null)
                                                {
                                                    try
                                                    {
                                                        ResultSet rs = statement.executeQuery("SELECT name FROM countries");
                                                        //get name of countries from database
                                                        while (rs.next ())
                                                        {
                                                            out.print ("<option>" + rs.getString("name") + "</option>");
                                                        }//end while
                                                    }
                                                    catch (Exception e)
                                                    {
                                                        out.println (e.getMessage ());
                                                    }
                                                }//end if
                                            %>
                                            
                                        </select>
    Can you please critic my code has well

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: JSP database connection problem

    Can't tell you why it can't find Database, unless you have another Database class somewhere.
    But that code is really quite wrong for a threaded environment like a web app.
    First off, you shouldn't be doing code stuff in a JSP like that.
    JSPs are for displaying data...they're also a sod to debug,

    Thread-wise, though, that Database class is not thread safe in the slightest.
    You'll be sharing connections and statements between different clients, which is a recipe for disaster as the db won't know one transaction from another.
    I suggest either creating a new Connection each time you need one or , if you know about connection pools, then using one of them (which is the standard).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: JSP database connection problem

    Found out what the problem was i had a logic error in my Database class that was causing null to be returned instead of a instance of the class and THANKS alot for criticizing really appreciate it.

Similar Threads

  1. getting database connection
    By ravidasineni in forum AWT / Swing
    Replies: 0
    Last Post: 11-27-2009, 04:33 AM
  2. getting database connection
    By ravidasineni in forum AWT / Swing
    Replies: 1
    Last Post: 11-22-2009, 02:01 AM
  3. Problem with database connection
    By mainy in forum New To Java
    Replies: 3
    Last Post: 08-07-2009, 11:43 PM
  4. database connection in jsf
    By Srikala in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 10-06-2008, 06:53 AM
  5. Database Connection
    By vipinkumarsolanki in forum Advanced Java
    Replies: 2
    Last Post: 11-26-2007, 06:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •