使用JavaBean访问SQL Server2000数据库

//连接数据库的JavaBeanpackagebuyBean;importjava.sql.*;publicclassDBConnectionManager{privat... //连接数据库的JavaBean
package buyBean;
import java.sql.*;
public class DBConnectionManager{
private String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
private String url="jdbc:odbc:person";
private String user="admin";
private String password="123456";

public void setDriverName(String newDriverName){
this.driverName=newDriverName;
}
public String getDriverName(){
return driverName;
}

public void setUrl(String newUrl){
this.url=newUrl;
}
public String getUrl(){
return url;
}

public void setUser(String newUser){
this.user=newUser;
}
public String getUser(){
return user;
}

public void setPassword(String newPassword){
this.password=newPassword;
}
public String getPassword(){
return password;
}

public Connection getConnection(){
try{
Class.forName(driverName);
return DriverManager.getConnection(url,user,password);
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
}

//执行SQL语句的JavaBean

package buyBean;
import java.sql.*;
import buyBean.*;

public class DBSQLManager{
protected Connection conn=null;
protected Statement stmt=null;
protected ResultSet rs=null;
protected String sqlStr;

public DBSQLManager(){
try{
sqlStr="";
DBConnectionManager dcm=new DBConnectionManager();
conn=dcm.getConnection();
stmt=conn.createStatement();
}
catch(Exception e){
System.out.println(e);

}
}
public Statement getStmt(){
return stmt;
}

public Connection getConn(){
return conn;
}

public ResultSet getRs(){
return rs;
}

public void setSqlStr(String newSqlStr){
this.sqlStr=newSqlStr;
}

public String getSqlStr(){
return sqlStr;
}

public void executeQuery()throws Exception{
rs=stmt.executeQuery(sqlStr);
}

public void executeUpdate()throws Exception{
stmt.executeUpdate(sqlStr);
}

public void close()throws SQLException{
if(stmt!=null){
stmt.close();
stmt=null;
}
conn.close();
conn=null;
}
};
//JSP页面;
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="buyBean.DBSQLManager"%>
<html>
<body>
<%
DBSQLManager dbsm=new DBSQLManager();
String sql="select * from grade";
dbsm.setSqlStr(sql);
dbsm.executeQuery();
ResultSet rs =dbsm.getRs();
while(rs.next()){
%>

out.print(rs.getString("number"));
out.print(rs.getString("姓名"));
<%}%>
</body>
</html>

//报错:
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 6 in the jsp file: /person.jsp

Generated servlet error:
[javac] Compiling 1 source file

E:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\person_jsp.java:53: ?????
??? ? ResultSet
??? ? org.apache.jsp.person_jsp
ResultSet rs =dbsm.getRs();
^
1 ??

org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
展开
 我来答
百度网友3aa3b52ee
2007-04-10 · 超过16用户采纳过TA的回答
知道答主
回答量:74
采纳率:0%
帮助的人:0
展开全部
import java.sql.*;

public class DBConn {
static String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
static String dbURL = "jdbc:Microsoft:sqlserver://localhost;DatabaseName=pubs";
static String userName = "sa";
static String userPwd = "sa";
static Connection dbConn=null;

public DBConn(){
}

public static Connection getConn(){

try {

Class.forName(driverName);
dbConn=DriverManager.getConnection(dbURL+";User="+userName+";Password="+userPwd);
}
catch (Exception e) {
e.printStackTrace();
}
return dbConn;
}
}

调用:DBConn.getConn();

lib 下面要有这三个驱动:
msbase.jar mssqlserver.jar msutil.jar
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式