我有一个执行查询和存储过程的类。我面临的问题是执行结果没有返回值,并且SP输出参数没有返回值。当我在SQL编辑器中执行这些SP和查询时,它工作正常。
package com.airtel.mail;
import java.io.File;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import javax.mail.internet.NewsAddress;
import org.apache.poi.hssf.record.formula.functions.Ipmt;
import com.auxilii.msgparser.Message;
import com.auxilii.msgparser.MsgParser;
public class Utility {
public static Connection getConnection() throws SQLException{
Connection conn= null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:jtds:sqlserver://(address:port)","(user)", "(password)");
if(conn == null){
System.out.println("Connection Failed");
}else{
System.out.println("Connection Success");
}
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static String getLeaEmailId(String emailID)
{
String btStatus = "";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String SQLQUERY = "select Lea_EmailId from dbo.Lea_add where LEA_Number=(select varLeaNumber from dbo.TR_AddTarget where varEmailId='"+emailID+"' and dtAuthorizationDate=(select MAX(dtAuthorizationDate) from TR_AddTarget where varEmailId='"+emailID+"'))";
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(SQLQUERY);
System.out.println("SQLQUERY <><> " + SQLQUERY);
if(rs.next()) {
btStatus = rs.getString("Lea_EmailId");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return btStatus;
}
public static String[] getFwdMailId(String emailId) throws SQLException {
System.out.println("EMAIL ID INSIDE getFwdEmailID <><> " + emailId);
CallableStatement cstsmt = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Connection conn = null;
String fwdId = "";
String nodalId = "";
String fwdNodal[] = new String[2];
try {
conn = getConnection();
cstsmt = conn.prepareCall("{call dbo.Usp_GetLeaIDMain(?,?,?)}");
cstsmt.setString(1, emailId);
cstsmt.registerOutParameter(2, Types.VARCHAR);
cstsmt.registerOutParameter(3, Types.VARCHAR);
cstsmt.execute();
fwdId = cstsmt.getString(2);
nodalId = cstsmt.getString(3);
System.out.println("LEA ID INSIDE PROCEDURE<><><> "+ fwdId);
System.out.println("NODAL ID INSIDE PROCEDURE<><><> "+ nodalId);
fwdNodal[0] = fwdId;
fwdNodal[1] = nodalId;
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
cstsmt.close();
conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
return fwdNodal;
}
public static String[] getEmailPasswd(){
String emailPasswd[] = new String[2];
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String email = "";
String passwd = "";
String SQLQUERY = "SELECT Email_Id, Email_Passwd FROM Java_Config";
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(SQLQUERY);
if(rs.next()) {
email = rs.getString("Email_Id");
passwd = rs.getString("Email_Passwd");
}
emailPasswd[0] = email;
emailPasswd[1] = passwd;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return emailPasswd;
}
public static void main(String[] args) {
String str = "deepak.deemca@gmail.com";
try{
String s = getLeaEmailId(str);
System.out.println("ID via QUERY <><> " +s );
String str1[] = getFwdMailId(str);
System.out.println("fl <><> " +str1[0]);
String str2[] = getEmailPasswd();
System.out.println("str2 [0] and str2[1] <><> " + str2[0] +"<><><><" +str2[1]);
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}