Zugriff auf MSSQL Funktion aus Java

magdafr

Grünschnabel
Hallo Zusammen,

ich habe schon ein bissle nachgeschaut was es hier schon gibt.
Ich verstehe nicht wo das Problem liegt....

HIer mein Java Code
Code:
private static void MSSQLServerStoredProcedureCallExample () throws ClassNotFoundException, SQLException{	
	 String sDbDrv = "net.sourceforge.jtds.jdbc.Driver";
      
			Class.forName(sDbDrv);
			JtdsDataSource ds = new JtdsDataSource();
			ds.setServerName("sql2005");
			
			ds.setPortNumber(1433);
			ds.setUser("user");
			ds.setPassword("psw");
			ds.setDatabaseName("dbname");

			Connection con = ds.getConnection();

			try {
				CallableStatement callableStatement = con
						.prepareCall("dbo.magalieFunction((@valueOne=?, @valueTwo=?)");
				callableStatement.setInt("@valueOne", 10);
				callableStatement.setInt("@valueTwo", 30);
				//callableStatement.registerOutParameter("@valueOne", Types.INTEGER);

				callableStatement.execute();

				callableStatement.getMoreResults();

				int result = callableStatement.getInt(1);

				System.out.println("RESULT: " + result);

			} finally {
				if (con != null)
					con.close();
			}
		}

Hier meine Function:
Code:
CREATE FUNCTION magalieFunction (@valueOne  int, @valueTwo  int)
RETURNS int
AS
begin
RETURN @valueOne + @valueTwo;
end;

Meine FehlerMeldung
Code:
Exception in thread "main" java.sql.SQLException: Parameter '@valueOne' not found in the parameter list.
	at net.sourceforge.jtds.jdbc.JtdsCallableStatement.findParameter(JtdsCallableStatement.java:95)
	at net.sourceforge.jtds.jdbc.JtdsCallableStatement.setInt(JtdsCallableStatement.java:288)
	at de.pickert.db.TestDb.MSSQLServerStoredProcedureCallExample(TestDb.java:142)
	at de.pickert.db.TestDb.main(TestDb.java:24)
 
Hi,
Dein obiges Listing hat offenbar die Regestrierung des Parameters auskommentiert

//callableStatement.registerOutParameter("@valueOne", Types.INTEGER)

Also insofern wundert mich di eFehlermeldung zumindest in dieser Konstalation nicht.
 
Also ich habe es so geändert und funktionniert...

Danke für die HIlfe

Code:
CallableStatement callableStatement = con.prepareCall("?= dbo.magalieFunction(?,?)");
				callableStatement.registerOutParameter(1, Types.INTEGER);
				callableStatement.setInt(2, 10);
				callableStatement.setInt(3, 30);
				callableStatement.execute();
				int result = callableStatement.getInt(1);
 

Neue Beiträge

Zurück