日期:2014-05-20 浏览次数:21058 次
package com.em.junit; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Sqlconnention { public static void main(String[] args) { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); try { Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=ExaManager", "sa", "hekunlun"); String sql = "select ?, ? from tb_studentLogin"; PreparedStatement pstmt = null; pstmt = con.prepareStatement(sql); pstmt.setString(1, "studentNumber"); pstmt.setString(2, "studentPassword"); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getString(1)); System.out.println(rs.getString(2)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:185) at com.em.junit.Sqlconnention.main(Sqlconnention.java:16)