【发布时间】:2016-06-25 17:05:09
【问题描述】:
我现在有两个名为 Earnings 和 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 输入,则 Earnings 必须基于 dest 示例的条目,因此 Earnings 表必须有 1 和 20 的条目,两列有 1是停止号,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 创建我尚未完成的数据库及其表,我将不胜感激映射尚未
【问题讨论】:
-
您缺少 servlet 本身,是否在某处定义了它?您需要告诉应用程序服务器什么是处理 /mrtservlet 路径
标签: java mysql model-view-controller crud