如何将JSP连接到MS访问数据库?

时间:2012-04-17 00:51:04

标签: sql jsp ms-access

我在使用以下JSP代码创建表时遇到问题。该错误与我的代码的第17行有关。我已经包含了我得到的错误以及我的代码。非常感谢任何帮助!

我的代码

<%@ page import="java.sql.*" %>
<%
 String url = "jdbc:odbc:productDSN";
 String username="";
 String password="";
 Connection  conn=null;
 String classpath = "sun.jdbc.odbc.JdbcOdbcDriver";
try{
    Class.forName(classpath);
    conn = DriverManager.getConnection(url,username,password);
   }catch(Exception exc){
     out.println(exc.toString());
}

%>
<%
    Statement stm= conn.createStatement();
    String query1, query2;
    query1="CREATE TABLE product(id char(3) PRIMARY KEY,"+
           "name varchar(15),model varchar(5), price float,"+
           "manufacturerID integer)";
    query2="CREATE TABLE manufacturer("+
  "manufacturerID integer PRIMARY KEY,"+
      "name varchar(15),address varchar(20), city varchar(20),"+
      "state varchar(15), zipCode char(5), phone varchar(15))";
try{
stm.executeUpdate(query1);
stm.executeUpdate(query2);

  out.println("Two tables were successfully created.");
}catch(Exception exc){
  out.println("These tables exist already.");
}
stm.close();
conn.close();
%>

错误

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: An exception occurred processing JSP page /example1.jsp at line 17

14: 
15: %>
16: <%
17:     Statement stm= conn.createStatement();
18:     String query1, query2;
19:     query1="CREATE TABLE product(id char(3) PRIMARY KEY,"+
20:            "name varchar(15),model varchar(5), price float,"+


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:471)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

java.lang.NullPointerException
    org.apache.jsp.example1_jsp._jspService(example1_jsp.java:82)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.21 logs.

Apache Tomcat/7.0.21

1 个答案:

答案 0 :(得分:0)

您的ConnectionNULL

您是否创建了DSN?如果没有,请创建System DSN,然后重试。