java.sql.SQLSyntaxErrorException:类' TempSP'不存在或无法访问

时间:2014-11-20 05:54:21

标签: derby java-stored-procedures

我正在尝试使用java在derby数据库中创建存储过程。

public class TestDrive {

public static void main (String[] args) throws SQLException, ClassNotFoundException{
    Connection conn = null;
    CallableStatement stmt = null;
    try {
        try{
        Class.forName("org.apache.derby.jdbc.ClientDriver").;
    } catch(ClassNotFoundException e){
        System.out.println("Can't load the database driver");
        return;
    }
        conn = DriverManager.getConnection("jdbc:derby://localhost:1527/Connections_DB", "Connections_DB", "KaayRey");
        stmt = conn.prepareCall("CALL NameOFstoreProcedure4( ?, ? )");
        stmt.setString(1, "Kitu");
        stmt.setString(2, "KaayChalay");
        stmt.execu`enter code here`te();
    } finally {
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}
}   //END OF CLASS TestDrive.java

TempSP.java代码: -

public class TempSP {
public static void NameOFSP(String uname, String upass)throws SQLException {
   PreparedStatement stmt = null;
   ResultSet res = null;
   String message = null ;
    try{
        try{`enter code here`
        Class.forName("org.apache.derby.jdbc.ClientDriver");
    } catch(ClassNotFoundException e){
        System.out.println("Can't load the database driver");
        return;
    }
    String dbURL = "jdbc:derby://localhost:1527/Connections_DB";
    Connection conn = DriverManager.getConnection(dbURL, "Connections_DB", "KaayRey");      
     Stri`enter code here`ng sql = "INSERT INTO USERS2(USER_NAME, USER_PASSWORD) VALUES(?, ?)";
     stmt = conn.prepareStatement(sql);
     stmt.setString(1, uname);
     stmt.setString(2, upass);
         int row = stmt.executeUpdate(sql);
    }catch (SQLException ex) {
        message = "ERROR 1 : " + ex.getMessage();
        ex.printStackTrace();
    } 
}
} // END OD TempSP.java

但是当我运行我的TestDrive.java文件时,它会给我以下错误: -

"Exception in thread "main" java.sql.SQLSyntaxErrorException: The class 'TempSP' does not exist or is inaccessible. This can happen if the class is not public.
    at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.client.am.Connection.prepareCall(Unknown Source)
    at FriendsConnectionPack.TestDrive.main(TestDrive.java:34)
Caused by: org.apache.derby.client.am.SqlException: The class 'TempSP' does not exist or is inaccessible. This can happen if the class is not public.
    at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.readPrepare(Unknown Source)
    at org.apache.derby.client.net.StatementReply.readPrepare(Unknown Source)
    at org.apache.derby.client.net.NetStatement.readPrepare_(Unknown Source)
    at org.apache.derby.client.am.Statement.readPrepare(Unknown Source)
    at org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInput(Unknown Source)
    at org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
    at org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
    at org.apache.derby.client.am.Connection.prepareCallX(Unknown Source)
    ... 2 more
Caused by: org.apache.derby.client.am.SqlException: Java exception: 'TempSP: java.lang.ClassNotFoundException'.
    ... 13 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)"

Derby存储过程代码如下: -

CREATE PROCEDURE NameOFstoreProcedure5(User_Name VARCHAR(50), User_Password VARCHAR(20))
PARAMETER STYLE JAVA READS SQL DATA LANGUAGE JAVA EXTERNAL NAME 
'FriendsConnectionPack.TempSP.NameOFSP'

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,解决方案是在jar文件中设置存储过程的代码,然后使用derby的系统过程注册jar并设置derby的类路径,如下所示

向德比添加一个jar:

CALL sqlj.install_jar('PATH TO JAR FILE', 'DATABASE.IDENTIFIER FOR THE JAR', 0)

将jar添加到derby的内部类路径

CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(
'derby.database.classpath',
'colonSeparatedJarFiles')

希望这有助于某人