注册 登录
编程论坛 J2EE论坛

JSP与数据库连接时出现的问题

ldw8477 发布于 2007-06-05 16:11, 533 次点击

本人用的是My Eclipse做的

想实现在数据库实现修改
类的建立
package l2;

import java.sql.*;

public class DataBean {
private Connection conn = null;

private ResultSet rs = null;

private String dbs;

public DataBean() {

}

public Connection setConnection(String db) {
dbs = "jdbc:odbc:" + db;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(dbs);
} catch (Exception e) {
System.out.println(e.toString());
}
return conn;
}

public ResultSet getQuery(String sqls) {
try {
Statement statement = conn.createStatement();
rs = statement.executeQuery(sqls);
} catch (Exception e) {
System.out.println(e.toString());
}
return rs;
}

public void setModify(String sqls) {
try {
Statement statement = conn.createStatement();
statement.executeUpdate(sqls);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}

修改部分
<%@page contentType="text/html;charset=GB2312" import="java.sql.*"%>

<%
Connection conn=DataBean.setConnection("db1");
DataBean.setModify("DELETE FROM table1 WHERE dep='美工部';");
DataBean.setModify("UPDATE table1 SET dep='管理部' WHERE name='陈小明';");
DataBean.setModify("INSERT INTO table1 VALUES('小林子','网管部',357);");
DataBean.setModify("INSERT INTO table1 VALUES('咩咩杨','图书部',812);");
ResultSet rs=DataBean.getQuery("SELECT * FROM table1");
%>

姓名 部门 分机号码

<%
while(rs.next())
{
错误为
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /Jsp8.jsp(6,0) The value for the useBean class attribute cc is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:150)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1227)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Generator.generate(Generator.java:3272)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:244)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:470)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.

本人做毕业设计很急 忘各位大哥帮帮小第

3 回复
#2
支离破碎2007-06-05 16:51
DataBean类要在JSP面页上引入
#3
ldw84772007-06-06 08:51

谢谢 我明白了

#4
天使坠落的眼泪2007-06-06 14:17
是,简单点,相当于Java的没有初始化Bean
1