使用bean通过JSP访问jdbc查询时出错

时间:2016-03-02 16:58:49

标签: java oracle jsp jdbc

我在尝试使用bean将行插入数据库时​​遇到错误。编译和运行java代码时,create tables方法工作正常。任何意见,将不胜感激。

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@ page import="java.util.*" %>
<%@ page import="assignment2.*"%>

<jsp:useBean id="homework2" class="assignment2.homework2" scope="session" />


<html>
<h2>Registration Page</h2>
<body>

    <form method="post" action="register.jsp">
        <fieldset>
            <legend>Create your account</legend>
            <div>
                <label>Username: *</label>
                <input type="text" name="username">
            </div>
            <div>
                <label>First Name: *</label> 
                <input type="text" name="firstname">
            </div>
            <div>
                <label>Last Name: *</label> 
                <input type="text" name="lastname">
            </div>
            <div>
                <label>E-mail: *</label> 
                <input type="text" name="email">
            </div>
        </fieldset>
        <br />
        <fieldset>
            <legend>Your Password</legend>
                <div>
                    <label>Password: *</label>
                    <input type="password" name="password">
                </div>
                <div>
                    <input type="submit" name="submit" value="Submit">
                </div>
        </fieldset>
        <br/>
        <input type="button" value="Back to login" onclick="location.href='login.jsp'">

    </form>
</body>
</html>

<%



    String username = request.getParameter("username");
    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    if(username == null && firstname == null && lastname == null && email == null && password == null){

    } else if(username.equals("")){
        out.print("please fill in username");
    } else if(firstname.equals("")){
        out.print("please fill in firstname");
    } else if(lastname.equals("")){
        out.print("please fill in lastname");
    } else if(email.equals("")){
        out.print("please fill in email");
    } else if(password.equals("")){
        out.print("please fill in password");
    } else{
        homework2.insertRecordIntoDbUserTable(username,password,firstname,lastname,email);      
    }

%>

这是java代码

package assignment2;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class homework2 {

    private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:XE";
    private static final String DB_USER = "scott";
    private static final String DB_PASSWORD = "tiger";

        public static void main(String[] args){
                try{
                    createDbUserTable();
                    createDbCartTable();
                    createDbAddressTable();
                    createDbproductTable();
                    createDbOrderTable();
                    createDbOrder_detTable();
                    createDbpay_mthd();
                    insertRecordIntoDbUserTable("","","","","");
                } catch (SQLException e){
                    System.out.println(e.getMessage());
                }


    }

    private static void createDbUserTable() throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String sequence = "CREATE SEQUENCE customers_seq START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE";

        String createTableSQL = "CREATE TABLE dbuser("
                + "USER_ID NUMBER(5) NOT NULL, "
                + "USERNAME VARCHAR(20) NOT NULL, "
                + "PASSWORD VARCHAR(20) NOT NULL, "
                + "FIRSTNAME VARCHAR(20) NOT NULL, "
                + "LASTNAME VARCHAR(20) NOT NULL, "
                + "EMAIL VARCHAR(20) NOT NULL, "
                + "PRIMARY KEY(USER_ID) "
                + ")";

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();
            System.out.println(createTableSQL);
            statement.execute(createTableSQL);
            System.out.println(sequence);
            statement.execute(sequence);

            System.out.println("Table \"dbuser\" is created!");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (dbConnection != null) {
                dbConnection.close();
            }
        }
    }

    public static void insertRecordIntoDbUserTable(String username, String password, String firstname,
                                                   String lastname,String email) throws SQLException {

    Connection dbConnection = null;
            Statement statement = null;

            String insertTableSQL = "INSERT INTO DBUSER"
                    + "(USER_ID, USERNAME, PASSWORD, FIRSTNAME, LASTNAME, EMAIL) "
                    + "VALUES"
                    + "(CUSTOMERS_SEQ.NEXTVAL,?,?,?,?,?)";

            try {
                dbConnection = getDBConnection();
                statement = dbConnection.createStatement();

                System.out.println(insertTableSQL);
                statement.executeUpdate(insertTableSQL);
                System.out.println("Record is inserted into DBUSER table");
            } catch (SQLException e){
                System.out.println(e.getMessage());
            } finally {
                if(statement != null) {
                    statement.close();
                }
                if(dbConnection != null){
                    dbConnection.close();
                }
            }
        }


    private static void createDbCartTable() throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String sequence = "CREATE SEQUENCE cart_seq START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE";

        String createTableSQL = "CREATE TABLE DBCART("
                + "cart_id NUMBER(5) NOT NULL, "
                + "user_id NUMBER(5) NOT NULL, "
                + "anon_id NUMBER(5) NOT NULL, "
                + "item VARCHAR2(20) NOT NULL, "
                + "amount NUMBER(5) NOT NULL, "
                + "price DECIMAL(10,2) NOT NULL, "
                + "PRIMARY KEY(cart_id) "
                + ")";

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();
            System.out.println(createTableSQL);
            statement.execute(createTableSQL);
            System.out.println(sequence);
            statement.execute(sequence);

            System.out.println("Table \"dbcart\" is created!");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (dbConnection != null) {
                dbConnection.close();
            }
        }
    }

    public static void insertRecordIntoDbCartTable(int cart_id, int user_id, int anon_id, String item,int amount, double price) throws SQLException {

    Connection dbConnection = null;
            Statement statement = null;

            String insertTableSQL = "INSERT INTO DBCART"
                    + "(cart_id, user_id, anon_id, item, amount, price) "
                    + "VALUES"
                    + "(cart_seq.NEXTVAL,?,?,?,?,?)";

            try {
                dbConnection = getDBConnection();
                statement = dbConnection.createStatement();

                System.out.println(insertTableSQL);
                statement.executeUpdate(insertTableSQL);
                System.out.println("Record is inserted into DBCART table");
            } catch (SQLException e){
                System.out.println(e.getMessage());
            } finally {
                if(statement != null) {
                    statement.close();
                }
                if(dbConnection != null){
                    dbConnection.close();
                }
            }
        }

    private static void deleteRecordFromDbCartTable(int cart_id) throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String deleteTableSQL = "DELETE DBUSER WHERE cart_id = " + cart_id;

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();

            System.out.println(deleteTableSQL);

            // execute delete SQL stetement
            statement.execute(deleteTableSQL);

            System.out.println("Record is deleted from DBcart table!");

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } finally {

            if (statement != null) {
                statement.close();
            }

            if (dbConnection != null) {
                dbConnection.close();
            }

        }

    }


    private static void createDbAddressTable() throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String sequence = "CREATE SEQUENCE address_seq START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE";

        String createTableSQL = "CREATE TABLE dbAddress("
                + "address_id NUMBER(5) NOT NULL, "
                + "user_id NUMBER(5) NOT NULL, "
                + "street VARCHAR2(20) NOT NULL, "
                + "city VARCHAR2(20) NOT NULL, "
                + "region VARCHAR2(20) NOT NULL, "
                + "country VARCHAR2(20) NOT NULL, "
                + "postal_code VARCHAR2(20) NOT NULL, "
                + "PRIMARY KEY(address_id) "
                + ")";

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();
            System.out.println(createTableSQL);
            statement.execute(createTableSQL);
            System.out.println(sequence);
            statement.execute(sequence);

            System.out.println("Table \"dbaddress\" is created!");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (dbConnection != null) {
                dbConnection.close();
            }
        }
    }

    public static void insertRecordIntoDbCartTable(int address, int user_id, String street, String city, String region, String country, String postal_code) throws SQLException {

    Connection dbConnection = null;
            Statement statement = null;

            String insertTableSQL = "INSERT INTO dbCart"
                    + "(address_id, user_id, street, city, region, country, postal_code) "
                    + "VALUES"
                    + "(address_seq.NEXTVAL,?,?,?,?,?,?)";

            try {
                dbConnection = getDBConnection();
                statement = dbConnection.createStatement();

                System.out.println(insertTableSQL);
                statement.executeUpdate(insertTableSQL);
                System.out.println("Record is inserted into DBaddress table");
            } catch (SQLException e){
                System.out.println(e.getMessage());
            } finally {
                if(statement != null) {
                    statement.close();
                }
                if(dbConnection != null){
                    dbConnection.close();
                }
            }
        }

    private static void createDbproductTable() throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String sequence = "CREATE SEQUENCE product_seq START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE";

        String createTableSQL = "CREATE TABLE dbProduct("
                + "prod_id NUMBER(5) NOT NULL, "
                + "order_id NUMBER(5) NOT NULL, "
                + "item_name VARCHAR2(20) NOT NULL, "
                + "description VARCHAR2(20) NOT NULL, "
                + "price DECIMAL(8,2) NOT NULL, "
                + "qoh NUMBER(5) NOT NULL, "
                + "PRIMARY KEY(prod_id) "
                + ")";

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();
            System.out.println(createTableSQL);
            statement.execute(createTableSQL);
            System.out.println(sequence);
            statement.execute(sequence);

            System.out.println("Table \"dbproduct\" is created!");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (dbConnection != null) {
                dbConnection.close();
            }
        }
    }

    public static void insertRecordIntoDbProductTable(int prod_id, String item_name, String description, double price, int qoh) throws SQLException {

    Connection dbConnection = null;
            Statement statement = null;

            String insertTableSQL = "INSERT INTO dbProduct"
                    + "(prod_id, item_name, description, price, qoh) "
                    + "VALUES"
                    + "(prod_seq.NEXTVAL,?,?,?,?)";

            try {
                dbConnection = getDBConnection();
                statement = dbConnection.createStatement();

                System.out.println(insertTableSQL);
                statement.executeUpdate(insertTableSQL);
                System.out.println("Record is inserted into DBproduct table");
            } catch (SQLException e){
                System.out.println(e.getMessage());
            } finally {
                if(statement != null) {
                    statement.close();
                }
                if(dbConnection != null){
                    dbConnection.close();
                }
            }
        }


    private static void deleteRecordFromDbProductTable(int product_id) throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String deleteTableSQL = "DELETE dbProduct WHERE product_id = " + product_id;

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();

            System.out.println(deleteTableSQL);

            // execute delete SQL stetement
            statement.execute(deleteTableSQL);

            System.out.println("Record is deleted from DBproduct table!");

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } finally {

            if (statement != null) {
                statement.close();
            }

            if (dbConnection != null) {
                dbConnection.close();
            }

        }

    }



private static void updateRecordIntoDbProductTable(int prod_id, String item_name, String description, double price, int qoh) throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String updateTableSQL = "UPDATE dbProduct"
                + " SET item_name = " + item_name+", "
                + "description= " + description + ", "
                + "price= " + price + ", "
                + "qoh= " + qoh + ", "
                + " WHERE prod_id = " + prod_id;

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();

            System.out.println(updateTableSQL);

            // execute update SQL stetement
            statement.execute(updateTableSQL);

            System.out.println("Record is updated to DBUSER table!");

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } finally {

            if (statement != null) {
                statement.close();
            }

            if (dbConnection != null) {
                dbConnection.close();
            }

        }

    }

    private static void createDbOrderTable() throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String sequence = "CREATE SEQUENCE order_seq START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE";

        String createTableSQL = "CREATE TABLE dbOrder("
                + "order_id NUMBER(5) NOT NULL, "
                + "user_id NUMBER(5) NOT NULL, "
                + "prod_id NUMBER(5) NOT NULL, "
                + "amount VARCHAR2(20) NOT NULL, "
                + "price DECIMAL(8,2) NOT NULL, "
                + "total_price NUMBER(5) NOT NULL, "
                + "pay_mthd NUMBER(5) NOT NULL, "
                + "PRIMARY KEY(prod_id) "
                + ")";

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();
            System.out.println(createTableSQL);
            statement.execute(createTableSQL);
            System.out.println(sequence);
            statement.execute(sequence);

            System.out.println("Table \"dborder\" is created!");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (dbConnection != null) {
                dbConnection.close();
            }
        }
    }

    public static void insertRecordIntoDbOrderTable(int order_id, int user_id, double total_price, String pay_mthd) throws SQLException {

    Connection dbConnection = null;
            Statement statement = null;

            String insertTableSQL = "INSERT INTO dbOrder"
                    + "(order_id, user_id, total_price) "
                    + "VALUES"
                    + "(order_seq.NEXTVAL,?,?)";

            try {
                dbConnection = getDBConnection();
                statement = dbConnection.createStatement();

                System.out.println(insertTableSQL);
                statement.executeUpdate(insertTableSQL);
                System.out.println("Record is inserted into DBorder table");
            } catch (SQLException e){
                System.out.println(e.getMessage());
            } finally {
                if(statement != null) {
                    statement.close();
                }
                if(dbConnection != null){
                    dbConnection.close();
                }
            }
        }


    private static void createDbOrder_detTable() throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String sequence = "CREATE SEQUENCE order_det_seq START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE";

        String createTableSQL = "CREATE TABLE dbOrder_det("
                + "order_det_id NUMBER(5) NOT NULL, "
                + "prod_id NUMBER(5) NOT NULL, "
                + "amount NUMBER(5) NOT NULL, "
                + "price DECIMAL(8,2) NOT NULL, "
                + "PRIMARY KEY(prod_id) "
                + ")";

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();
            System.out.println(createTableSQL);
            statement.execute(createTableSQL);
            System.out.println(sequence);
            statement.execute(sequence);

            System.out.println("Table \"dborder_det\" is created!");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (dbConnection != null) {
                dbConnection.close();
            }
        }
    }

    public static void insertRecordIntoDbOrder_detTable(int order_det_id, int prod_id, double amount, double price) throws SQLException {

    Connection dbConnection = null;
            Statement statement = null;

            String insertTableSQL = "INSERT INTO dbOrder_det"
                    + "(order_det_id, prod_id, amount, price) "
                    + "VALUES"
                    + "(order_det_seq.NEXTVAL,?,?,?)";

            try {
                dbConnection = getDBConnection();
                statement = dbConnection.createStatement();

                System.out.println(insertTableSQL);
                statement.executeUpdate(insertTableSQL);
                System.out.println("Record is inserted into DBorder_det table");
            } catch (SQLException e){
                System.out.println(e.getMessage());
            } finally {
                if(statement != null) {
                    statement.close();
                }
                if(dbConnection != null){
                    dbConnection.close();
                }
            }
        }

    private static void createDbpay_mthd() throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String sequence = "CREATE SEQUENCE pay_mthd_id_seq START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE";

        String createTableSQL = "CREATE TABLE dbPay_mthd("
                + "pay_mthd_id NUMBER(5) NOT NULL, "
                + "user_id NUMBER(5) NOT NULL, "
                + "order_id NUMBER(5) NOT NULL, "
                + "cc_type VARHCAR2(20) NOT NULL, "
                + "cc_num NUMBER(5)(8,2) NOT NULL, "
                + "PRIMARY KEY(prod_id) "
                + ")";

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();
            System.out.println(createTableSQL);
            statement.execute(createTableSQL);
            System.out.println(sequence);
            statement.execute(sequence);

            System.out.println("Table \"Dbpay_mthd\" is created!");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (dbConnection != null) {
                dbConnection.close();
            }
        }
    }

    public static void insertRecordIntoDbpay_mthdTable(int pay_mthd_id, int user_id, int order_id, String cc_type, int cc_num) throws SQLException {

    Connection dbConnection = null;
            Statement statement = null;

            String insertTableSQL = "INSERT INTO dbPay_mthd"
                    + "(pay_mthd_id, user_id, order_id, cc_type, cc_num) "
                    + "VALUES"
                    + "(pay_mthd_id_seq.NEXTVAL,?,?,?,?)";

            try {
                dbConnection = getDBConnection();
                statement = dbConnection.createStatement();

                System.out.println(insertTableSQL);
                statement.executeUpdate(insertTableSQL);
                System.out.println("Record is inserted into DBpay_mthd table");
            } catch (SQLException e){
                System.out.println(e.getMessage());
            } finally {
                if(statement != null) {
                    statement.close();
                }
                if(dbConnection != null){
                    dbConnection.close();
                }
            }
        }



    private static Connection getDBConnection() {
            Connection dbConnection = null;
            try {
                Class.forName(DB_DRIVER);
            } catch (ClassNotFoundException e) {
                System.out.println(e.getMessage());
            }

            try {
                dbConnection = DriverManager.getConnection(
                        DB_CONNECTION, DB_USER,DB_PASSWORD);
                return dbConnection;
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
            return dbConnection;
    }
}

这是我得到的错误:

HTTP Status 500 - java.lang.NoClassDefFoundError: assignment2/homework2 (wrong name: homework2)

type Exception report

message java.lang.NoClassDefFoundError: assignment2/homework2 (wrong name: homework2)

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

exception

javax.servlet.ServletException: java.lang.NoClassDefFoundError: assignment2/homework2 (wrong name: homework2)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:349)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NoClassDefFoundError: assignment2/homework2 (wrong name: homework2)
    java.lang.ClassLoader.defineClass1(Native Method)
    java.lang.ClassLoader.defineClass(Unknown Source)
    java.security.SecureClassLoader.defineClass(Unknown Source)
    org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2472)
    org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:854)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1274)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1292)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1178)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:464)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3594)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

再次感谢

0 个答案:

没有答案