用java连接sqlServer2008,显示成功连接,但是读不出数据,请帮忙看一下代码,谢谢
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;impor...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlSeverConnect {
public static void main(String[] args) {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //驱动的名字
String url = "jdbc:sqlserver://127.0.0.1:1433; databaseName=TestDB"; //sqlServer所占用端口和所要连接的数据库名字
String user = "sa"; //sqlServer用户名
String password = "123456"; //密码啊
try {
Class.forName(driver); //加载驱动
Connection conn = DriverManager.getConnection(url, user, password);//建立到TestDB数据库的连接
if(!conn.isClosed()) //如果开着
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement(); //建立sql语句的执行容器
String sql = "select * from user";
ResultSet rs = statement.executeQuery(sql); //执行该sql语句,返回resultSet对象
String name = null;
while(rs.next()) { //分析resultSet对象,确定用户是否存在
name = rs.getString("uID");
System.out.println(name);
}
rs.close();
conn.close();
}catch(ClassNotFoundException e) {
System.out.println("Sorry, can't find the Driver");
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
执行结果为:
Succeeded connecting to the Database!
com.microsoft.sqlserver.jdbc.SQLServerException: 关键字 'user' 附近有语法错误。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:775)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:676)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:611)
at SqlSeverConnect.main(SqlSeverConnect.java:22) 展开
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlSeverConnect {
public static void main(String[] args) {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //驱动的名字
String url = "jdbc:sqlserver://127.0.0.1:1433; databaseName=TestDB"; //sqlServer所占用端口和所要连接的数据库名字
String user = "sa"; //sqlServer用户名
String password = "123456"; //密码啊
try {
Class.forName(driver); //加载驱动
Connection conn = DriverManager.getConnection(url, user, password);//建立到TestDB数据库的连接
if(!conn.isClosed()) //如果开着
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement(); //建立sql语句的执行容器
String sql = "select * from user";
ResultSet rs = statement.executeQuery(sql); //执行该sql语句,返回resultSet对象
String name = null;
while(rs.next()) { //分析resultSet对象,确定用户是否存在
name = rs.getString("uID");
System.out.println(name);
}
rs.close();
conn.close();
}catch(ClassNotFoundException e) {
System.out.println("Sorry, can't find the Driver");
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
执行结果为:
Succeeded connecting to the Database!
com.microsoft.sqlserver.jdbc.SQLServerException: 关键字 'user' 附近有语法错误。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:775)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:676)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:611)
at SqlSeverConnect.main(SqlSeverConnect.java:22) 展开
展开全部
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement(); //建立sql语句的执行容器
String sql = "select * from app_htjsk ";
我用你的代码试了下,我就是换了个表而已。其他没问题 肯定是你那user表有问题,你可以先把你那句SQL语句拿去数据库执行下。看可以不,你那句SQL语句肯定有问题
Statement statement = conn.createStatement(); //建立sql语句的执行容器
String sql = "select * from app_htjsk ";
我用你的代码试了下,我就是换了个表而已。其他没问题 肯定是你那user表有问题,你可以先把你那句SQL语句拿去数据库执行下。看可以不,你那句SQL语句肯定有问题
追问
确实在在DBMS里,执行“select * from user"都显示 user附近有异常,表换个名字,其他数据一样执行都没问题吗。 看来可能是sqlserver2008不许用user作表名,很奇怪。
追答
换个表名就没问题 你代码我都执行过了
展开全部
String sql = "select * from user;";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你select其他表试试还报错吗?根据异常信息,好像user是关键字吧
追问
我选择其他表是可以的,难道是因为user表中, uID列 的数据类型是 char(20) ,而不是nvarchar吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询