I have the following error:
An error occurred at line: 21 in the jsp file: /init.jsp
Type mismatch: cannot convert from DB to DB
18: DB.setConfig( driver, url, username, password );
19:
20: DB db1 = new DB();
21: DB db2 = DB.getInstance();
Notice that it did not complain about the static function "setConfig" or the use of the constructor. But calling a static function which returns a DB object fails.
How do I fix this?
getInstance() definition:
public static DB getInstance()
{
if ( db == null )
db = new DB();
return db;
}
Here is how I'm importing the class into my jsp file:
<%@ page import="mypackage.DB" %>
The DB.class is located at WEB-INF/classes/mypackage/DB.class
The jar command I used is:
jar cvf mypackage.jar *.class
And I copied the jar into WEB-INF/lib
Here is the content of my jsp file:
<%@ page language="java" import="java.sql.*, java.util.Properties,java.io.*,java.lang.*" errorPage="" %>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.* " %>
<%@ page import="java.io.*" %>
<%@ page import="mypackage.DB" %>
<%
try
{
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost:5432/cse135_db";
String username = "postgres";
String password = "password";
DB.setConfig( driver, url, username, password );
DB db1 = new DB();
DB db2 = DB.getInstance();
String myDataField = null;
String myQuery = "SELECT * FROM users LIMIT 1";
Connection myConnection = null;
PreparedStatement myPreparedStatement = null;
ResultSet myResultSet = null;
Class.forName(driver).newInstance();
myConnection = DriverManager.getConnection(url,username,password);
/* myPreparedStatement = myConnection.prepareStatement(myQuery);
myResultSet = myPreparedStatement.executeQuery();
if(myResultSet.next())
myDataField = myResultSet.getString("username");
out.print(myDataField); */
}
catch( ClassNotFoundException e )
{
e.printStackTrace(); out.print("doesn't work");
}
catch ( SQLException ex )
{
out.print("SQLException: " + ex.getMessage());
out.print("SQLState: " + ex.getSQLState());
out.print("VendorError: " + ex.getErrorCode());
}
%>