日期:2014-05-17  浏览次数:20693 次

jsp 调用sql server 语句错在哪儿?高手指点!
显示java语法错误和结果集无结果。但数据库和记录是没问题的。


<%@ page contentType="text/html;charset=gb2312"%>   
 
<%@ page import="java.sql.*"%> 
 

<html>

 
<body>   


 
<%   


int numberOfXing=0;
int numberOfMing=0;

  
String textXing="张"
;
String textMing="秋";
out.print("您输入的姓名是: "+textXing+textMing);



 
 
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");   
 
Connection conn= DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=name;user=sa;password=123456");  
 
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   
 
String sql="select sum(姓数字) as 姓数字,sum(名数字) as 名数字 from (select '姓数字'=数字, '名数字 '=0  from name.dbo.table1 

WHERE 汉字 = '"+textXing+"'  UNION  select '姓数字'=0, '名数字'= 数字 from name.dbo.table1 WHERE 汉字='"+textMing+"' )aa"; 

  String sql="select * from name.dbo.table1 where 汉字='"+textXing+"'";
  ResultSet rs=stmt.executeQuery(sql);  
        numberOfXing=Integer.parseInt(rs.getInt("姓数字"));
numberOfMing=Integer.parseInt(rs.getInt("名数字"));




out.print(numberOfXing);

out.print(numberOfMing);

 
/** while(rs.next()){%> 第一个字段:<%=rs.getString(1)%> 第二个字段:<%=rs.getString(2)%> 
 
<br> 
 
<%  
 
}  

**/


%>   
 
<%


out.print("数据库操作成功");%> 
 
 <%rs.close(); stmt.close(); conn.close();   
 
%> 





</body>   
 
</html> 
------解决方案--------------------
哪地方有语法错误?啥错误?
------解决方案--------------------
错误呢。什么具体,为什么用中文看起来别扭。
------解决方案--------------------
换成试试:
Connection conn= DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=name","sa","123456");  

------解决方案--------------------
把异常信息贴出来看看。还有你的getConnection方法最好分成3个参数。比较清楚一些。
------解决方案--------------------
36: where 汉字 = '"+textXing+"'  UNION  select '姓数字'=0, '名数字'= 数字 from name.dbo.table1 WHERE 汉字='"+textMing+"' )aa"; 

where 汉字='   "+textXing+" '两个地方的字符串拼接都要加上单引号

------解决方案--------------------
不是给出错误提示了吗 字符串没有双引号结尾。