Results 1 to 3 of 3
Thread: JSP database connection problem
- 03-02-2012, 09:06 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
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:
Here is the core for the jsp: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
Can you please critic my code has wellJava 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>
- 03-05-2012, 11:08 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 03-10-2012, 05:47 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
Similar Threads
-
getting database connection
By ravidasineni in forum AWT / SwingReplies: 0Last Post: 11-27-2009, 04:33 AM -
getting database connection
By ravidasineni in forum AWT / SwingReplies: 1Last Post: 11-22-2009, 02:01 AM -
Problem with database connection
By mainy in forum New To JavaReplies: 3Last Post: 08-07-2009, 11:43 PM -
database connection in jsf
By Srikala in forum JavaServer Faces (JSF)Replies: 0Last Post: 10-06-2008, 06:53 AM -
Database Connection
By vipinkumarsolanki in forum Advanced JavaReplies: 2Last Post: 11-26-2007, 06:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks