0

我正在尝试使用 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)"

德比存储过程代码如下:-

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

1 回答 1

0

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

将 jar 添加到 Derby:

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')

希望这可以帮助某人

于 2016-03-18T18:09:33.590 回答