當連接到SQL Server 2008(本地快速生成,完整的服務器在生產中)時,在我的本地機器上開發時,對我來說工作正常,但這件事只是在生產中掛起。JDBC連接掛起,沒有來自SQL Server 2008 r2的響應
下面是代碼:
package oata;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import sun.applet.Main;
public class Sql {
public static final String SQLDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
protected Connection conn = null;
private String ip = "";
private int port = 0;
private String databaseName = "";
private String db_userid = "";
private String db_password = "";
public void callDb(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
System.out.println("Initialising variables");
ip = args[0];
port = Integer.parseInt(args[1]);
databaseName = args[2];
db_userid = args[3];
db_password = args[4];
try{
Log logger = LogFactory.getLog(Main.class);
System.out.println("Opening logger...");
logger.debug("opening driver " + SQLDRIVER);
System.out.println("Creating connection instance...");
Class.forName(SQLDRIVER).newInstance();
System.out.println("Driver Manager.getConnection...");
conn = DriverManager.getConnection(getDBURL(), db_userid, db_password);
System.out.println("Connection prepare statement...");
PreparedStatement ps = conn.prepareStatement("select * from nstupersonal");
System.out.println("Executing query...");
ResultSet rs = ps.executeQuery();
while(rs.next()){
System.out.println(rs.getString("StudentId"));
}
}catch(Exception e){
System.out.println(e.getMessage());
}
conn.close();
}
private String getDBURL(){
String url = "";
try {
url = "jdbc:sqlserver://" + ip
+":" + port +";databaseName="+
databaseName + ";user=" + db_userid + ";password="+db_password;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return url;
}
}
就是這樣被運行的代碼。在控制檯,它實際上只是在生產中掛起。它似乎總是掛在SQL Server 2008上,但對於我所有其他客戶的生產環境都能正常工作。運行...
SQL Server中的1433上啓用了TCP/IP,並允許遠程連接在管理控制檯中設置爲true。
java -jar ipaddress port dbname用戶密碼
任何想法? SQL驅動程序是否錯誤?我使用sqljdbc4.jar
得到的命令提示符...
正在初始化變量 打開記錄器... 驅動Manager.getConnection ...
也不例外得到投擲
謝謝,
d
它掛在哪裏?你能插入更多的日誌語句來追蹤導致它掛起的行嗎? – beny23
已更新的代碼有系統輸出和拋出消息,但從不拋出異常。 – david99world