环境:win7 x64 en, jdk1.6.0_29, sql server 2008 r2 x64, sqljdbc_4.0.2206.100_enu
代码:
package com.jekon.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcTest { /** * @param args * @throws SQLException */ public static void main(String[] args) throws SQLException { // TODO Auto-generated method stub String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url = "jdbc:sqlserver://localhost:1433;databaseName=Northwind;"; String username = "sa"; String password = ""; String sql = "SELECT * FROM dbo.Customers"; Connection conn = null; Statement stmt = null; ResultSet data = null; try { Class.forName(driver); System.out.println("Loaded Driver"); conn = DriverManager.getConnection(url, username, password); System.out.println("Connected"); stmt = conn.createStatement(); data = stmt.executeQuery(sql); System.out.println("sql excute"); while (data.next()) { System.out.println(data.getObject(1) + " " + data.getObject(2) + " " + data.getObject(3) + " " + data.getObject(4) + " " + data.getObject(5) + " " + data.getObject(6) + " " + data.getObject(7) + " " + data.getObject(8) + " " + data.getObject(9) + " " + data.getObject(10) + " " + data.getObject(11)); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { try { if (null != data) data.close(); } catch (Exception e2) { } finally { try { if (null != stmt) stmt.close(); } catch (Exception e3) { // TODO: handle exception } finally { try { if (null != null) conn.close(); } catch (Exception e4) { // TODO: handle exception } } } System.out.println("Source Unloaded"); } } }
再下一步就没响应了,程序也没有停止!
sql profile 监听如下:
敢问哪位仁兄能帮兄弟我分析一下这是什么原因造成的,如何解决???
感激!!!!
看下你的sql端口是否一样和sa用户名密码,既然楼上说他运行都没问题,那代码肯定没什么问题
是啊,我另外一台电脑也是可以运行的。端口、防火墙、帐号密码都是对的。
@疯狂的Duck: 那应该就是你这台电脑的问题吧
@┢┦偉: 把jdk更新了,OK了
@疯狂的Duck: 哦 呵呵
把jdk更新到1.6u37了,好了,估计是jdk的缘故。