JavaWeb项目查询数据库获得数据显示在jsp页面上

先来简单的例子吧 。大概如下:index.jsp页面中只有一个按钮,用来跳转到操作数据,中对数据库查询操作后将信息保存在对象中通过转发传递到.jsp页面(如果你对数据的操作也是在jsp中,java代码逻辑部分其实是一样的,注意跳转路径即可;用了@同理,跳转的逻辑也是一样的)
把数据库建好
CREATE TABLE `User` (`id` int(11) NOT NULL,`username` varchar(255) DEFAULT NULL,`password` varchar(255) DEFAULT NULL,`address` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`))
插入一个数据
insert into User values(1,"Martin","12345","zjut")
建立一个 Web,目录如下:
User.java
package com.entity;public class User {private int id;private String username;private String password;private String address;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", password=" + password + ", address=" + address + "]";}public User(int id, String username, String password, String address) {super();this.id = id;this.username = username;this.password = password;this.address = address;}public User() {super();}}
.java
package com.utils;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBHelper {private static final String driver = "com.mysql.cj.jdbc.Driver";private static final String url = "jdbc:mysql://localhost:3306/webtest?useUnicode=true&characterEncoding=UTF-8";private static final String username = "root";private static final String password = "123456";private static Connection conn=null;static {try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}public static Connection getConnection() {if(conn==null) {try {conn = DriverManager.getConnection(url,username,password);System.out.println("数据库初始化成功");} catch (SQLException e) {e.printStackTrace();System.out.println("数据库初始化失败");}return conn;}else {return conn;}}}
.java
package com.servlet;import java.io.IOException;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.entity.User;import com.utils.DBHelper;@WebServlet("/servlet/ServletDemo")public class ServletDemo extends HttpServlet {private static final long serialVersionUID = 1L;public ServletDemo() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Connection conn = DBHelper.getConnection();String sql="select * from User";try {Statement st = conn.createStatement();ResultSet rs = st.executeQuery(sql);User user = new User();while(rs.next()) {user.setId(rs.getInt("id"));user.setUsername(rs.getString("username"));user.setPassword(rs.getString("password"));user.setAddress(rs.getString("address"));}//将数据库中查询到的信息封装在user对象中,use对象保存在request中,之后将通过request对象将数据传递到页面//需要的话也可以只传递一个参数request.setAttribute("id", user.getId());request.setAttribute("user", user);//这里是转发,从Servlet跳转到showinfo.jsp页面,并且带上request和response对象中原有的参数request.getRequestDispatcher("/showinfo.jsp").forward(request, response);} catch (SQLException e) {e.printStackTrace();}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}