这是我第一次问如此我很抱歉,如果我搞砸了。哦,所以在这里它
我有这个mysql表,我想在jsp页面中显示,我实际上让它工作,但它不是一个很好的方法,因为所有代码(连接到数据库等)都在JSP.So我做一个java bean,它控制连接到数据库并执行sql查询部分,以及一个单独的JSP页面,我可以在其中显示该表。
main.jsp中
<%@ page import="java.sql.*" %>
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<head>
<title>Welcome to Car Rental System</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css"></link>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<script src="jquery.backstretch.min.js"></script>
<link rel="stylesheet" href="style.css"></link>
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro" rel="stylesheet">
</head>
<body>
<p><div><center><h1><b>Car Rental System</b></h1></center></div></p>
<%
<jsp:useBean id="displayCarList" class="displayCarList.displayCarList" >
<jsp:setProperty name="displayCarList" property="*"/>
</jsp:useBean>
<%
String carbookId = request.getParameter("carbookId");
String manufacturername = request.getParameter("manufacturername");
String modelname = request.getParameter("modelname");
String price_day = request.getParameter("price_day");
%>
<table border='3' align='center'>
<tr>
<td><b>Car ID</td>
<td><b>Manufacturer</td>
<td><b>Model name</td>
<td><b>Price per day</td>
</tr>
<tr>
<td><%=displayCarList.getCarbookid()%></td>
<td><%=displayCarList.getManufacturername()%></td>
<td><%=displayCarList.getModelname()%></td>
<td><%=displayCarList.getPriceday()%></td>
</tr>
</table>
<br></br>
<form action=secondpage.jsp method="post">
<fieldset><legend>Fill in the form below :</legend>
<p><b>Name:</b> <input type="text" name="name" size="20" maxlength="40" /></p>
<p><b>Telephone number</b> <input type="text" name="phonenumber" th:field="*{phonenumber}" size="20" maxlength="40" /></p>
<p><b>Rental date:</b> <input type="date" th:field="*{rentaldate}" name="rentaldate"></p>
<p><b>Return date:</b> <input type="date" th:field="*{returndate}" name="returndate"></p>
<p><b>Car ID:</b> <input type="text" name="carID" th:field="*{carID}" size="20" maxlength="40" /></p>
<div align="left"><input type="submit" name="submit" value="Submit " /></div>
</form>
<script>
$.backstretch("background2.jpg");
</script>
</body>
</html>
以下是java bean,displayCarList.java
package displayCarList;
import java.io.*;
import static java.lang.System.out;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class displayCarList {
private String carbookId, manufacturername, modelname, price_day;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
public displayCarList(){
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(displayCarList.class.getName()).log(Level.SEVERE, null, ex);
}
con =DriverManager.getConnection ("jdbc:mysql://localhost:3306/rentalcar","root","");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM carlist");
while(rs.next()){
carbookId = rs.getString(1);
manufacturername= rs.getString(2);
modelname = rs.getString(3);
price_day = rs.getString(4);
}
stmt.close();
con.close();
// displaying record
} catch (SQLException e) {
System.out.println("couldnotconnecYO");
}
}
public String getCarbookid(){
return carbookId;
}
public void setCarbookid(){
this.carbookId = carbookId;
}
public String getManufacturername(){
return manufacturername;
}
public void setManufacturername(){
this.manufacturername = manufacturername;
}
public String getModelname(){
return modelname;
}
public void setModelname(){
this.modelname = modelname;
}
public String getPriceday(){
return price_day;
}
public void setPriceday(){
this.price_day = price_day;
}
}
但由于某种原因,只显示一条记录,这是最后一条,它应该返回表格中的所有内容(据说)
所以我很感激,如果你们能帮助我,并指出我做错了什么,并帮助我获得表格中显示的所有记录,如此
再次感谢
更新
根据Shuddh的指示,我对代码进行了一些更改,但它仍然无法解决某些血腥的原因
public ArrayList displayCarList(){
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(displayCarList.class.getName()).log(Level.SEVERE, null, ex);
}
con =DriverManager.getConnection ("jdbc:mysql://localhost:3306/rentalcar","root","");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM carlist");
while(rs.next()){
int i = 1;
while(i <= 12){
carlist.add(rs.getString(i++));
}
}
stmt.close();
con.close();
// displaying record
} catch (SQLException e) {
System.out.println("couldnotconnecYO");
}
return carlist;
}
它现在在每个列中返回null
答案 0 :(得分:0)
您获得最后一行的原因是因为您没有将其他行存储在任何位置。因此它会覆盖变量并捕获最后一行。将局部变量存储为列表,这样就可以了。
创建一个List并将每个结果集添加到
中try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(displayCarList.class.getName()).log(Level.SEVERE, null, ex);
}
con =DriverManager.getConnection ("jdbc:mysql://localhost:3306/rentalcar","root","");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM carlist");
while(rs.next()){
carbookId = rs.getString(1);
manufacturername= rs.getString(2);
modelname = rs.getString(3);
price_day = rs.getString(4);
// add to list here.
}
stmt.close();
con.close();
// displaying record
} catch (SQLException e) {
System.out.println("couldnotconnecYO");
}
return List. // If you have no idea about list in java google it and learn how to use it
将上述代码写在方法中而不是构造函数中。