在eclipse创建jsp页面,使用servlet和javabean,连接mysql数据库bus,选择下拉框中的出发地和目的地,点击查询即可在当前页面显示数据库中的车次信息代码
时间: 2024-03-12 14:44:50 浏览: 19
好的,下面是一个基本的jsp页面、servlet和javabean,用于连接mysql数据库bus,选择下拉框中的出发地和目的地,点击查询即可在当前页面显示数据库中的车次信息。
首先,我们需要创建一个名为“Bus.java”的javabean,用于获取和设置数据库中的车次信息。下面是示例代码:
```java
package com.example;
public class Bus {
private String id;
private String from;
private String to;
private String time;
private String price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
```
接下来,我们需要创建一个名为“BusDAO.java”的servlet,用于连接数据库bus并获取车次信息。下面是示例代码:
```java
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BusDAO {
private Connection connection;
public BusDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bus", "root", "password");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public List<Bus> getBuses(String from, String to) {
List<Bus> buses = new ArrayList<>();
try {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM buses WHERE `from`=? AND `to`=?");
statement.setString(1, from);
statement.setString(2, to);
ResultSet rs = statement.executeQuery();
while (rs.next()) {
Bus bus = new Bus();
bus.setId(rs.getString("id"));
bus.setFrom(rs.getString("from"));
bus.setTo(rs.getString("to"));
bus.setTime(rs.getString("time"));
bus.setPrice(rs.getString("price"));
buses.add(bus);
}
} catch (SQLException e) {
e.printStackTrace();
}
return buses;
}
}
```
最后,我们需要创建一个名为“bus.jsp”的jsp页面,用于显示出发地和目的地下拉框,以及查询结果。下面是示例代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="com.example.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Bus Search</title>
</head>
<body>
<h1>Bus Search</h1>
<form method="post" action="bus.do">
<label for="from">From:</label>
<select name="from" id="from">
<option value="Beijing">Beijing</option>
<option value="Shanghai">Shanghai</option>
<option value="Guangzhou">Guangzhou</option>
</select>
<label for="to">To:</label>
<select name="to" id="to">
<option value="Beijing">Beijing</option>
<option value="Shanghai">Shanghai</option>
<option value="Guangzhou">Guangzhou</option>
</select>
<input type="submit" value="Search">
</form>
<hr>
<% String from = request.getParameter("from");
String to = request.getParameter("to");
if (from != null && to != null) {
BusDAO dao = new BusDAO();
List<Bus> buses = dao.getBuses(from, to);
if (buses.size() > 0) {
out.println("<h2>Results:</h2>");
out.println("<table>");
out.println("<tr><th>ID</th><th>From</th><th>To</th><th>Time</th><th>Price</th></tr>");
for (Bus bus : buses) {
out.println("<tr><td>" + bus.getId() + "</td><td>" + bus.getFrom() + "</td><td>" + bus.getTo() + "</td><td>" + bus.getTime() + "</td><td>" + bus.getPrice() + "</td></tr>");
}
out.println("</table>");
} else {
out.println("<h2>No results found.</h2>");
}
}
%>
</body>
</html>
```
在这个示例中,我们使用了两个下拉框来选择出发地和目的地,然后通过servlet调用数据库获取车次信息,并在jsp页面中显示查询结果。当用户点击“Search”按钮时,jsp页面会提交表单到servlet中进行查询。注意,我们在servlet中使用了预编译语句来避免SQL注入攻击。
请注意,这只是一个简单的示例,您需要根据您的具体需求进行修改和调整。此外,您需要将代码中的数据库连接信息和密码替换为您自己的信息。