Java 2表1网络应用我该怎么做

Java 2表1网络应用我该怎么做,java,mysql,model-view-controller,crud,Java,Mysql,Model View Controller,Crud,我有两张名为“收入”和“乘客”的表格,现在我有了这些代码 从这个开始的视图 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

我有两张名为“收入”和“乘客”的表格,现在我有了这些代码

从这个开始的视图

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="mrtservlet" method="post">
Enter first name: <input type="text" name="fname">
Enter last name: <input type="text" name="lname">
Destination: <input type="text" name="dest">
<input type="submit" value="submit">
</form>
</body>
</html>

以及我的sql命令和连接实用程序

package utilities;

 import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class earnings_connection {

private static Connection connection = null;

public static Connection getConnection() {
     if (connection != null)
     return connection;
     else {
     try {
     Properties prop = new Properties();
     InputStream=inputStreampassengers_connection.class.getClassLoader().getResourceAsStream("/db.properties0");
     prop.load(inputStream);
     String driver = prop.getProperty("driver");
     String url = prop.getProperty("url");
     String user = prop.getProperty("user");
     String password = prop.getProperty("password");
     Class.forName(driver);
     connection = DriverManager.getConnection(url, user, password);
     } catch (ClassNotFoundException e) {
     e.printStackTrace();
     } catch (SQLException e) {
     e.printStackTrace();
     } catch (FileNotFoundException e) {
     e.printStackTrace();
     } catch (IOException e) {
     e.printStackTrace();
     }
     return connection;
     }
    }   
}
收入连接(上图)和乘客连接(下图) 包装实用程序

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class passengers_connection {

private static Connection connection = null;

public static Connection getConnection() {
     if (connection != null)
     return connection;
     else {
     try {
     Properties prop = new Properties();
     InputStream inputStream = passengers_connection.class.getClassLoader().getResourceAsStream("/db.properties");
     prop.load(inputStream);
     String driver = prop.getProperty("driver");
     String url = prop.getProperty("url");
     String user = prop.getProperty("user");
     String password = prop.getProperty("password");
     Class.forName(driver);
     connection = DriverManager.getConnection(url, user, password);
     } catch (ClassNotFoundException e) {
     e.printStackTrace();
     } catch (SQLException e) {
     e.printStackTrace();
     } catch (FileNotFoundException e) {
     e.printStackTrace();
     } catch (IOException e) {
     e.printStackTrace();
     }
     return connection;
     }

    }
    }
表将首先用于收益的sql操作

package utilities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.earnings;

public class earnings_dao {

private Connection connection;

public earnings_dao(){
    connection = utilities.passengers_connection.getConnection(); 
    }

    public void insertrip(earnings user) { 
        try {
        PreparedStatement preparedStatement = connection
        .prepareStatement("insert into Earnings(Stop No,Fare)"
                + "values (?, ?)");
        preparedStatement.setInt(1, user.getsno());
        preparedStatement.setInt(2, user.getfare());
        preparedStatement.executeUpdate();
        } catch (SQLException e) {
        e.printStackTrace();
        }
    }

    public List<earnings> getAllUsers() {
        List<earnings> users = new ArrayList<earnings>();
        try {
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("select Stop No from Earnings");
        while (rs.next()) {
        earnings user = new earnings();
        user.setsno(rs.getInt("Stop No"));
        users.add(user);}
        } catch (SQLException e) {
        e.printStackTrace();
        }
        return users;
    }   
}

现在我被我的控制器卡住了,有人能指出我的代码有什么错误吗?如果它能帮助我使用MySQL Server Management Studio创建数据库和它们的表,我将不胜感激。我还没有做映射,不太确定问题出在哪里,但是在这个问题中,我没有看到您试图发回的Servlet的任何声明,所以我假设这就是问题所在:

您可以定义Servlet类,该类将处理对定义的url执行的get和post请求

在答案中,我们将使用JAVA约定命名类,并尝试编写一些代码,希望能对您有所帮助

@WebServlet(name = "mrtservlet", urlPatterns = {"/mrtservlet"}})
public class Mrtservlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //do the get stuff if necessary
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //read the post parameters to temporary variables
        String fname= req.getParameter("fname");
        String lname= req.getParameter("lname");
        String dest= req.getParameter("dest");
         // do some null checks on the fields you get from the request

        //fill the earning with what you want and do all the dao related stuf
        Earning earning = new Earning();

        EarningsDao earningsDao= new EarningsDao();
        earningsDao.insert(earning);

        //You can wrap the above code in a switch or a if's if you want to do something different for each dest 
        if(dest!=null && dest.equals("guadalupe")){
         //do guadalupe related stuff
        }

        //redirect to any servlet/page you want you want

        resp.sendRedirect("/servlet");
    }
}

您缺少servlet本身,是否在某个地方定义了它?您需要告诉应用程序服务器处理/mrtservlet路径的方法我已经创建了servlet,只是我无法理解servlet部分中我需要做什么的概念是的,在serlvets中被迫这样做,背景很差只是一个想法会更好我会尝试在回答中添加更多细节类名应该是驼峰大小写,比如EarningsDao而不是earnings\u dao
package utilities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.earnings;

public class earnings_dao {

private Connection connection;

public earnings_dao(){
    connection = utilities.passengers_connection.getConnection(); 
    }

    public void insertrip(earnings user) { 
        try {
        PreparedStatement preparedStatement = connection
        .prepareStatement("insert into Earnings(Stop No,Fare)"
                + "values (?, ?)");
        preparedStatement.setInt(1, user.getsno());
        preparedStatement.setInt(2, user.getfare());
        preparedStatement.executeUpdate();
        } catch (SQLException e) {
        e.printStackTrace();
        }
    }

    public List<earnings> getAllUsers() {
        List<earnings> users = new ArrayList<earnings>();
        try {
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("select Stop No from Earnings");
        while (rs.next()) {
        earnings user = new earnings();
        user.setsno(rs.getInt("Stop No"));
        users.add(user);}
        } catch (SQLException e) {
        e.printStackTrace();
        }
        return users;
    }   
}
package utilities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import model.passengers;


public class passengers_dao {

private Connection connection;

public passengers_dao(){
connection = utilities.passengers_connection.getConnection(); 
}

public void insertrip(passengers user) { 
    try {
    PreparedStatement preparedStatement = connection
    .prepareStatement("insert into Passengers(firstname,lastname,destination)"
            + "values (?, ?, ?)");
    preparedStatement.setString(1, user.getfname());
    preparedStatement.setString(2, user.getlname());
    preparedStatement.setString(3, user.getdest());
    preparedStatement.executeUpdate();
    } catch (SQLException e) {
    e.printStackTrace();
    }

   }
}
@WebServlet(name = "mrtservlet", urlPatterns = {"/mrtservlet"}})
public class Mrtservlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //do the get stuff if necessary
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //read the post parameters to temporary variables
        String fname= req.getParameter("fname");
        String lname= req.getParameter("lname");
        String dest= req.getParameter("dest");
         // do some null checks on the fields you get from the request

        //fill the earning with what you want and do all the dao related stuf
        Earning earning = new Earning();

        EarningsDao earningsDao= new EarningsDao();
        earningsDao.insert(earning);

        //You can wrap the above code in a switch or a if's if you want to do something different for each dest 
        if(dest!=null && dest.equals("guadalupe")){
         //do guadalupe related stuff
        }

        //redirect to any servlet/page you want you want

        resp.sendRedirect("/servlet");
    }
}