0

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());
}
%>
Kacy
  • 3,330
  • 4
  • 29
  • 57
  • Please, make yourself a favor and [don't use scriptlets](http://stackoverflow.com/q/3177733/1065197). – Luiggi Mendoza Apr 10 '15 at 15:00
  • hmmm, i'm actually not really sure you was able to call line 20, it looks like compiler error, not a runtime exception. I'm not really familiar with jsp... – JohnnyAW Apr 10 '15 at 15:03
  • @JohnnyAW It is a compiler error. Poor wording on my part. Edited. – Kacy Apr 10 '15 at 15:06
  • @LuiggiMendoza The DB class is not contained inside scriptlet tags. Unless your'e talking about the import? – Kacy Apr 10 '15 at 15:07
  • 1
    You're using scriptlets, noted by usage of `<% %>` directly in your JSP. Probably you have two or more classes called `DB` and you're not noticing this. Again, do yourself a favor and don't use them. Instead, move that code into a Servlet and forward the request to the desired jsp.. – Luiggi Mendoza Apr 10 '15 at 15:22
  • @LuiggiMendoza I don't have 2 DB classes. I've checked the "mypackage" folder which contains my DB class. "mypackage" is also the only directory in WEB-INF/classes. Unless you think moving the code to a Servlet will remove this error, I'd rather focus on my immediate problem. – Kacy Apr 10 '15 at 15:27
  • @KacyRaye I agree with Luiggi Mendoza, move that code to the servlet, get the data from your DB there and pass the data to jsp. In the jsp try only to "display" the data, that was collected by the servlet – JohnnyAW Apr 10 '15 at 15:31
  • I think there's a `DB` class declared in default package, thus you're getting this exception. And yes, I think using Scriplets helps you avoid all these headaches. – Luiggi Mendoza Apr 10 '15 at 15:31
  • @KacyRaye btw. there is a tag library called JSTL with some common tags, maybe you want to look at it – JohnnyAW Apr 10 '15 at 15:37
  • @LuiggiMendoza I changed the name of the class to MyDB and still receive the same error. I will try converting it to a servlet now. – Kacy Apr 10 '15 at 15:41

0 Answers0