我有两张名为Earnings and Passengers的表,现在我有这些代码
从这个
开始查看<%@ 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表,并且如果我将guadalupe放入dest中,收入必须将其条目基于dest示例,则收益表必须具有1和20的一个条目作为停止号码,20是到达那里的价格
这是我的豆子
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();
}
}
}
现在我被我的控制器困住了,任何人都可以指出我的代码已经完成了任何错误我会非常感激,如果它帮助我使用MySQL Server Management Studio创建数据库和他们的表我已经完成了映射
答案 0 :(得分:0)
不完全确定问题是什么,但在问题中我没有看到您试图回复的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");
}
}