package ssd;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class sdd {public static void main(String[] srg) {String driverName = com.microsoft.sqlserver.jdbc.SQLServerDri
package ssd; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class sdd { public static void main(String[] srg) { String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动 String dbURL = "jdbc:microsoft:sqlserver://127.0.0.1:1433; DatabaseName=lianxi"; // 连接服务器和数据库 String userName = "sa"; // 密码 String password = "sa"; Connection dbConn = null; Statement sta = null; ResultSet rs = null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, password); System.out.println("数据库连接成功!"); sta = dbConn.createStatement(); rs = sta.executeQuery("select * from users"); while(rs.next()){ System.out.println(rs.getString(1)); System.out.println(rs.getString(2)); } } catch (Exception e) { System.err.println("exception:" + e.getMessage()); } } }
出现
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'users' 无效。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSExecuteRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQuery(Unknown Source)
......
代码中:
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=XXX
注意到了localhost:1433;后面有一空格,这样就找不到Database了,肯定也就找不到其中的数据表了。