使用JSP提交表单

时间:2013-04-03 03:08:35

标签: jsp

我正在尝试创建一个JSP,它将插入通过表单提交的记录。但是,我正在尝试确保它连接,并通过使用mySQL select语句来检索数据。我在下面发布了错误。我不知道为什么我会收到这个错误。到目前为止我有这个

<%@ page import ="java.sql.*" %>
<%
String url = "jdbc:mysql//sql.njit.edu:3306/user";
String user = "u";
String pass = "p";
String classPath = "com.mysql.jdbc.Driver";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try{
    Class.forName(classPath);
    conn = DriverManager.getConnection(url, user, pass);
} catch(Exception e){
    e.printStackTrace();
}

stmt = conn.createStatement();

String query = "SELECT * FROM Music";

try{
    rs = stmt.executeQuery(query);
    while(rs.next()){
        out.println(rs.getString("song"));
    }
}catch(SQLException e){
    e.printStackTrace();
}
finally {
    try {
        if (stmt != null)
            stmt.close();
        if (rs != null)
            rs.close();
        if (conn != null)
            conn.close();
    }catch (SQLException e) {
        e.printStackTrace();
    }
}
%>

我收到以下错误

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:321)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:257)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.NullPointerException
org.apache.jsp.insert_jsp._jspService(insert_jsp.java:64)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:321)    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:257)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

1 个答案:

答案 0 :(得分:0)

您的JDBC URL格式错误。它应该像

jdbc:mysql//sql.school.edu:3306/user