How do I solve the problem:Syntax error on tokens, delete these tokens?
Nina Savannah
Ranch Hand
Joined: Oct 15, 2008
Posts: 35
posted
0
I need help to solve the errors that I am getting for my connection pooling code:-
Generated servlet error:
Syntax error on token "import", delete this token
Generated servlet error:
Syntax error on tokens, delete these tokens
Please do help, thanks!
THE CODE:
<%@ page import="javax.naming.*,java.sql.*,java.io.*,java.lang.*,java.text.*,java.util.*"%>
<%
public class ConnectionPool implements Runnable
{
private int m_InitialConnectionCount = 5; // Number of initial connections to make.
private Vector m_AvailableConnections = new Vector();// A list of available connections for use.
private Vector m_UsedConnections = new Vector();// A list of connections being used currently.
private String m_URLString = null;// The URL string used to connect to the database
private String m_UserName = null; // The username used to connect to the database
private String m_Password = null;// The password used to connect to the database
private Thread m_CleanupThread = null;// The cleanup thread
for(int cnt=0; cnt<m_InitialConnectionCount; cnt++)
{
m_AvailableConnections.addElement(getConnection());// Add a new connection to the available list.
}
// Create the cleanup thread
m_CleanupThread = new Thread(this);
m_CleanupThread.start();
}
newConnxn = getConnection();// Im out of connections. Create one more.
m_UsedConnections.addElement(newConnxn);// Add this connection to the "Used" list.
// We dont have to do anything else since this is
// a new connection.
}
else
{
// Connections exist !
newConnxn = (Connection)m_AvailableConnections.lastElement();// Get a connection object
m_AvailableConnections.removeElement(newConnxn);// Remove it from the available list.
m_UsedConnections.addElement(newConnxn); // Add it to the used list.
}
return newConnxn;// Either way, we should have a connection object now.
}
public synchronized void checkin(Connection c)
{
if(c != null)
{
m_UsedConnections.removeElement(c);// Remove from used list.
m_AvailableConnections.addElement(c); // Add to the available list
}
}
public int availableCount()
{
return m_AvailableConnections.size();
}
public void run()
{
try
{
while(true)
{
synchronized(this)
{
while(m_AvailableConnections.size() > m_InitialConnectionCount)
{
// Clean up extra available connections.
Connection c = (Connection)m_AvailableConnections.lastElement();
m_AvailableConnections.removeElement(c);
c.close(); // Close the connection to the database.
}
// Clean up is done
}
out.print("CLEANUP : Available Connections : " + availableCount());
public class Main
{
public static void main (String[] args)
{
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception E)
{
out.print("Unable to load driver.");
E.printStackTrace();
}
try
{
ConnectionPool cp = new ConnectionPool("jdbc:mysql://localhost/publications", "books", "books");
Nothing wrong with your import statement, in jsp page you can always have multiple imports. But something looks wrong with you class declarations, in a jsp page we can only declare abstract or final classes. Another mistake is here.
// Example code using ConnectionPool.
import java.sql.*;
In a jsp page you can not use the import statement any where, it has to come under page directive typically the first statement.
Moreover I am not being able to make out what exactly you want to do, why so many class declarations in side a jsp page?
I was bit wrong in my post i.e. "in a jsp page we can only declare abstract or final classes". We can declare classes with abstract , final or no access modifier, but public is not allowed.
Nina Savannah
Ranch Hand
Joined: Oct 15, 2008
Posts: 35
posted
0
Thanks guys...I have just configured settings in server.xml and web.xml for connection pooling. Now in this code I am attempting to apply
the connection pooling to my web system. I am not sure how right or wrong I am here.
I have removed
import java.sql.*; // REMOVE THIS IMPORT
and below are the errors I am now getting.
An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Illegal modifier for the local class ConnectionPool; only one of abstract or final is permitted
An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method
An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Illegal modifier for the local class Main; only one of abstract or final is permitted
An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
The method main cannot be declared static; static methods can only be declared in a static or top level type
An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method
An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method
An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method
An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method
An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method
Hi Nina,
Use DAO class to communicate with DB . and writing the java code into jsp is not a good parctise .jsp is used for presentation logic not for bussiness logic
As said above its better to use jsp just for the presentation logic, but in case you declare classes in jsp page it can't have public/private or protected access modifiers.
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
You should be writing Java code in Java classes.
It will also make this kind of problems disappear.