日期:2014-05-20  浏览次数:20808 次

求助?sql列名无效
rs=stat.executeQuery( "SELECT   *   FROM   users     where   users.username= "+uname);
出现列名无效。

整个程序为:
import   java.sql.*;
public   class   DB
{
public   static   void   main(String   []args){
String   url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=blog ";
Connection   con=null;
Statement   stat=null;
ResultSet   rs=null;
String   uname= "uname ";
try{
                Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
                System.out.println( "驱动程序已装载 ");
                System.out.println( "即将连接数据库 ");
}catch(Exception   e){
System.out.println( "无法加载驱动程序: "+e.getMessage());
return;
}
try{
                con=DriverManager.getConnection(url, "sa ", "sa ");
                stat=con.createStatement();
                rs=stat.executeQuery( "SELECT   *   FROM   users     where   users.username= "+uname);
                System.out.println( "查询结果为: ");  
                while(rs.next()){
                System.out.println(rs.getString(1)+rs.getString(2)
                                  +rs.getString(3)+rs.getString(4)+rs.getString(5)+rs.getString(6));
                }
                rs.close();
                stat.close();
                con.close();
}catch(SQLException   ex){
while(ex!=null){
ex.printStackTrace();
ex=ex.getNextException();
}
}
}
}


在Eclipse中出现如下结果:

驱动程序已装载
即将连接数据库
java.sql.SQLException:   [Microsoft][SQLServer   2000   Driver   for   JDBC][SQLServer]列名   'uname '   无效。
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)
at   DB.main(DB.java:21)