您的帮助是我学习最大的动力:我用WSAD写数据库连接的JAVABean出错!
我用WSAD建立的工程,运行结果显示
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]没有执行可选特性。可是用JB 单独运行作为Bean的类时没有出现异常!
所以就郁闷了!
作为Bean的类的代码:
package TestConnection;
import java.sql.*;
/**
* @author Administrator
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class TestConnection {
private Connection conn=null;
private ResultSet rs=null;
public TestConnection(){
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
}
catch(Exception ex){
ex.printStackTrace();
}
}
public ResultSet SQLFuction(String str_SQL){
try{
conn=DriverManager.getConnection( "jdbc:odbc:SQ ");
Statement sta=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
rs=sta.executeQuery(str_SQL);
}
catch(Exception ex){
ex.printStackTrace();
return null;
}
return rs;
}
}
JSP页面代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<HTML>
<HEAD>
<%@ page
language= "java "
contentType= "text/html; charset=GB18030 "
pageEncoding= "GB18030 "
import= "java.sql.* "
%>
<META http-equiv= "Content-Type " content= "text/html; charset=GB18030 ">
<META name= "GENERATOR " content= "IBM WebSphere Studio ">
<META http-equiv= "Content-Style-Type " content= "text/css ">
<LINK href= "theme/Master.css " rel= "stylesheet "
type= "text/css ">
<TITLE> TestJDBC.jsp </TITLE>
</HEAD>
<BODY>
<jsp:useBean id= "TestSQLConnection " scope= "page " class= "TestConnection.TestConnection "/>
<%
String str_SQL= "select * from Table_Users ";
ResultSet rs=TestSQLConnection.SQLFuction(str_SQL);
String str_name;
String str_Region;
while(rs.next()){
str_name=rs.getString( "UsersName ");
str_Region=rs.getString( "UsersRegion ");
out.println(str_name+ "%&$@%@ "+str_Region);
}
%>
</BODY>
</HTML>
------解决方案--------------------Statement sta=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
Statement对象里面的属性有问题。