package com.brookfieldres.operations;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
public class SQLConnection {
private static Connection acon = null;
private static CallableStatement _cs = null;
private static String _dbServer = null;
private static String _dbUsername = null;
private static String _dbPassword = null;
private static String _dbName = null;
private static String _dbInstance = null;
private static String _dbWindowsAuthentication = null;
private static final Logger aLogger = Logger.getLogger(SQLConnection.class.getName());
// static ResourceBundle resource = ResourceBundle.getBundle("Resource");
public SQLConnection() {
_dbServer = "localhost";
_dbUsername = "NewLocationTestUser";
_dbPassword = "TestPass123";
_dbName = "NewLocationDB";
_dbInstance = "APPSQL";
_dbWindowsAuthentication = "FALSE";
}
public Connection getConnection() {
SQLServerDataSource ds = new SQLServerDataSource();
Properties properties = new Properties();
try {
String dbURL = "jdbc:sqlserver://" + _dbServer;
if(!_dbInstance.equalsIgnoreCase(""))
{
dbURL += "\\" + _dbInstance;
}
if(_dbWindowsAuthentication.equalsIgnoreCase("TRUE"))
{
dbURL += ";integratedSecurity=true";
}
else
{
properties.put("user", _dbUsername);
properties.put("password", _dbPassword);
}
dbURL += ";";
properties.put("database", _dbName);
acon = DriverManager.getConnection(dbURL, properties);
System.out.println("1");
} catch (Exception e) {
aLogger.error(e.getMessage());
}
finally {
ds = null;
}
aLogger.info("The sql connection has been established.");
return acon;
}
public int insertLocations(Timestamp RunDate, String rlpCompanyid, String rlpLocationid, String rlpOpenDate){
int returnVal = 0;
try{
_cs = getConnection().prepareCall("{call iCurrentLocations01(?, ?, ?, ?)}");
_cs.setTimestamp("RunDate", RunDate);
_cs.setString("CompanyId", rlpCompanyid);
_cs.setString("LocationId", rlpLocationid);
_cs.setString("rlpOpenDate", rlpOpenDate);
returnVal = _cs.executeUpdate();
System.out.println("2");
}catch (SQLException e){
aLogger.error(e.getMessage());
}finally {
if (_cs != null){
try{
_cs.close();
}catch(SQLException e) {
aLogger.error(e.getMessage());
}
}
}
return returnVal;
}
因此,當我嘗試運行我的線束時,連接就會發生。但是,當程序嘗試調用我的存儲過程「iCurrentLocations01」時。它在我的日誌返回錯誤信息使用命名JDBC參數調用存儲過程會引發異常
「2016年1月27日13點11分17秒錯誤的SQLConnection:97 - 參數RunDate是 不是存儲過程iCurrentLocations01定義」
有人可以請解釋我在這裏做錯了嗎?該程序btw從LDAP目錄獲取信息並將其插入本地數據庫。
編輯 - 這是我的存儲過程:
USE [NewLocationDB]
GO
/****** Object: StoredProcedure [dbo].[iCurrentLocations01] Script Date: 01/27/2016 1:27:10 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[iCurrentLocations01]
@RunDate datetime,
@CompanyId varchar,
@LocationId varchar,
@rlpOpenDate varchar
AS
DECLARE @RetVal int
SET @RetVal = 0
INSERT CurrentLocations (RunDate, CompanyId, LocationId, rlpOpenDate)
VALUES (@RunDate, @CompanyId, @LocationId, @rlpOpenDate)
SET @RetVal = @@ERROR
Return @RetVal
GO
認爲信息很清楚 – Jens