当前位置:我的异常网» 热门搜索»sql 列名无效
sql 列名无效
网友分享于:-07-06搜索量:1383次
场景:
求助?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.monTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.monExecute(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)
------解决方案--------------------
rs=stat.executeQuery( "SELECT * FROM users where users.username= ' "+uname + " ' ");
不加引号的话dbms会把uname的值当作一个列名的
文章评论
如果觉得《eclipse列名无效_sql 列名无效》对你有帮助,请点赞、收藏,并留下你的观点哦!