高手请看看这些access数据库连接代码有什么问题?

yeyuanzhi2000 2008-12-22 10:06:55
高手请看看这些access数据库连接代码有什么问题?
1、先建立一个JDBC类:

Java codepackage com;
import java.sql.*;
public class JDBC{
private final String spath="/DataBase\\db1.mdb";
private final String dbpath = application.getRealPath(spath);
private final String dbname = "";
private final String pwd = "";
private final String url ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dbpath;
private final Connection conn= DriverManager.getConnection(url);

public JDBC() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); //加载数据库驱动
} catch (Exception ex) {
System.out.println("数据库加载失败");
}
}

//创建数据库连接
public Connection getConnection() {
try {
conn= DriverManager.getConnection(url, dbname,pwd);
conn.setAutoCommit(true);

} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("creatConnectionError!");
}
return conn;
}
}


2、在index.jsp页面调用如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> 
<%@ page import="java.util.*"%>
<jsp:useBean id="conn" scope="request" class="com.JDBC"/>
<%
Connection cnn = conn.getConnection();
  Statement stmt=cnn.createStatement();
  String query="insert into about(name) values('"+sContent+"')";
  stmt.executeUpdate(query);

String sql="select * from tb_userInfo";
  ResultSet rs1=stmt.executeQuery(sql);
  while(rs1.next())
 {
 String s=rs1.getString(1);
 out.print(s+" <br>");
 }

%>
谢谢!!
...全文
65 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
jinhuiyu 2008-12-22
你的问题呢?异常?
回复
yeyuanzhi2000 2008-12-22
没人能解决这个问题吗?
回复
yeyuanzhi2000 2008-12-22
不知道是不是路径问题?现把各个文件路径说明如下:
1、JDBC类路径:src/com/JDBC.java
2、数据库路径:Database/db1.mdb
3、index.jsp路径:admin/index.jsp
回复
yeyuanzhi2000 2008-12-22
异常:org.apache.jasper.JasperException: /admin/index.jsp(3,0) The value for the useBean class attribute com.JDBC is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1178)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1117)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2166)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2216)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2222)
org.apache.jasper.compiler.Node$Root.accept(Node.java:457)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2166)
org.apache.jasper.compiler.Generator.generate(Generator.java:3324)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:296)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:302)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
回复
相关推荐
发帖
Java SE
创建于2007-09-28

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2008-12-22 10:06
社区公告
暂无公告