jsp:无法从类转换为同类

时间:2015-04-10 14:55:14

标签: java jsp

我有以下错误:

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();

请注意,它没有抱怨静态函数“setConfig”或使用构造函数。 但是调用返回数据库对象的静态函数会失败。

我该如何解决这个问题?

getInstance()定义:

public static DB getInstance()
{
    if ( db == null )
        db = new DB();

    return db;
}

以下是我将类导入jsp文件的方法:

<%@ page import="mypackage.DB" %>

DB.class位于WEB-INF / classes / mypackage / DB.class

我使用的jar命令是:

jar cvf mypackage.jar *.class

我将jar复制到WEB-INF / lib

以下是我的jsp文件的内容:

<%@ 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());
}
%>

0 个答案:

没有答案