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

有关convert转换函数
程序如下:
import   java.sql.*;  
import   java.io.*;  
import   java.util.*;  

public   class   Test   {  
public   static   void   main(String   args[])   {  
String   url   =   "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant ";  
String   user   = "sa ";  
String   password   =   " ";  
String   sqlStr   =   "select   convert(varchar(10),bookDate,11)   from   roomBook   where   bookDate   between   '2007-4-10 '   and   '2007-4-25 ' ";  

try   {  
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");  
System.out.println(   "fdgdsfg "   );  

Connection   con   =   DriverManager.getConnection(   url,   user,   password   );  
Statement   st   =   con.createStatement();  
ResultSet   rs   =   st.executeQuery(   sqlStr   );  

while(rs.next())   {  
System.out.println( "运行这里还没有出错 ");  
System.out.println(rs.getString( "bookDate "));   //这里该怎么写?

}  
rs.close();  
st.close();  
con.close();  
}   catch(Exception   err)   {  
System.out.println(err.toString());  
}  
}  
}  

编译可以通过,但是运行的时候出错了:
----------   运行   ----------
fdgdsfg
运行这里还没有出错
java.sql.SQLException:   [Microsoft][SQLServer   2000   Driver   for   JDBC]Invalid   column   name:   bookDate

因为想把查找的的时间按年/月/日的方式输出,结果出错了。该怎么做呢?


------解决方案--------------------
try:
select convert(varchar(10),bookDate,11) as convertBookDate from roomBook where bookDate between '2007-4-10 ' and '2007-4-25 ' ";

rs.getString( "convertBookDate ");