我装的sql sever2000,系统为W2K,用的是sever 2005 for JDBC驱动,运行程序为下面的代码,
import java.sql.*;
public class testjdbc{
public static void main(String[] args){
try{
//load the Driver
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://222.27.110.17:1433;DatabaseName=mydb";
String user = "administrator";
String password = "woaiwojia";
//connecte to the server
Connection conn = DriverManager.getConnection(url,user,password);
// Connection conn = DriverManager.getConnection(url,user,password);
//send the SQL command
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM Student";
ResultSet rs = stmt.executeQuery(sql);
//output the result
System.out.println("=====SELECT * FROM Student=====");
System.out.println("Sno\t\tSname");
while(rs.next())
System.out.println(rs.getString("Sno")+ "\t\t" +rs.getString("Sname"));
//close the connecte
conn.close();
}
catch(SQLException e){
System.out.println("SQL State :" +e.getSQLState());
System.out.println("SQL Error Code :" +e.getErrorCode());
}
catch(Exception e){
e.printStackTrace();
}
}
}出错信息为:
SQL State:08001
SQL Error Code 0
import java.sql.*;
public class testjdbc{
public static void main(String[] args){
try{
//load the Driver
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://222.27.110.17:1433;DatabaseName=mydb";
String user = "administrator";
String password = "woaiwojia";
//connecte to the server
Connection conn = DriverManager.getConnection(url,user,password);
// Connection conn = DriverManager.getConnection(url,user,password);
//send the SQL command
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM Student";
ResultSet rs = stmt.executeQuery(sql);
//output the result
System.out.println("=====SELECT * FROM Student=====");
System.out.println("Sno\t\tSname");
while(rs.next())
System.out.println(rs.getString("Sno")+ "\t\t" +rs.getString("Sname"));
//close the connecte
conn.close();
}
catch(SQLException e){
System.out.println("SQL State :" +e.getSQLState());
System.out.println("SQL Error Code :" +e.getErrorCode());
}
catch(Exception e){
e.printStackTrace();
}
}
}出错信息为:
SQL State:08001
SQL Error Code 0
System.out.println("SQL Error Code :" +e.getErrorCode());
~~~~~~~~~~~~~~~~~~~~~~~~
改成e.printStackTrace(); 看看异常。
要不然就去查SQL文档,看看08001是什么意思。