我有两个名为“收入”和“乘客”的表,现在我有这些代码
从此开始查看
<%@ 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>
现在我必须将 fname lname 和 dest 发送到 Passengers 表,并且收入必须将其条目基于 dest 示例,如果我将 guadalupe 作为 dest,收入表必须有一个条目 1 和 20 两列,其中 1是停靠站号码,20 是到达那里的价格
这是我的 bean
package model;
public class earnings {
private int fare;
private int sno;
public int getfare(){
return fare;
}
public void setfare(int fare){
this.fare = fare;
}
public int getsno(){
return sno;
}
public void setsno(int sno){
this.sno = sno;
}
}
和
package model;
public class passengers {
private String fname;
private String lname;
private String dest;
public String getfname(){
return fname;
}
public void setfname(String fname){
this.fname = fname;
}
public String getlname(){
return lname;
}
public void setlname(String lname){
this.lname = lname;
}
public String getdest(){
return dest;
}
public void setdest(String dest){
this.dest = dest;
}
}
以及我的 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;
}
}
现在为乘客
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();
}
}
}
现在我被我的 Controller 困住了,任何人都可以指出我对代码所做的任何错误,如果它能帮助我使用 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");
}
}
关于java - 2 个表格 1 个网络应用程序 我该怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35923510/