jsp从数据库读取数据显示到jsp页面

jsp从数据库读取数据显示到jsp页面_第1张图片

整个项目结构,jar包可网上下载导入

package com.Podcast.dbutil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBconn {
	//三属性,四方法
	
	//三大核心接口
	private Connection conn=null;
	private PreparedStatement pstmt=null;
	private ResultSet rs=null;
	
	String driver="com.mysql.jdbc.Driver";
	String url="jdbc:mysql://localhost:3306/podcast";
	String user="root";
	String password="123456";
	//四个方法
	//method1:创建数据库的连接
	private void getConntion(){
		try{
			//加载连接驱动
			Class.forName(driver);
			//连接mysql数据库
			conn=DriverManager.getConnection(url,user,password);
		} catch(ClassNotFoundException e){
			e.printStackTrace();
		} catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	//method2:关闭数据库连接
	public void closeConn(){
		if(rs!=null){
			try{
				rs.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(pstmt!=null){
			try{
				pstmt.close();
				
			} catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try{
				conn.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
		}
		
	}
	
	//method3:专门用于发送增删改语句的方法
	public int execOther(final String strSQL, final Object[] params){
		//连接
		getConntion();
		System.out.println("SQL:>"+strSQL);
		try{
			//创建statement接口对象
			pstmt=conn.prepareStatement(strSQL);
			//动态为pstmt对象赋值
			for(int i=0;i"+strSQL);
		try{
			pstmt=conn.prepareStatement(strSQL);
			for(int i=0;i
package com.Podcast.entity;

public class PostBar {
	private int Pid;//帖子id
	private String Topic;//帖子主题
	private String Message;//帖子内容
	private String PDate;//帖子发布日期
	private String ChangeDate;//帖子修改日期
	private int Uid;//发布帖子的用户ID
	private String Pkind;//帖子类型
	
	public int getPid() {
		return Pid;
	}
	public void setPid(int pid) {
		Pid = pid;
	}
	public String getTopic() {
		return Topic;
	}
	public void setTopic(String topic) {
		this.Topic = topic;
	}
	public String getMessage() {
		return Message;
	}
	public void setMessage(String message) {
		this.Message = message;
	}
	public String getDdate() {
		return PDate;
	}
	public void setPDate(String pdate) {
		PDate = pdate;
	}
	public String getChangeDate() {
		return ChangeDate;
	}
	public void setChangeDate(String changeDate) {
		ChangeDate = changeDate;
	}
	public int getUid() {
		return Uid;
	}
	public void setUid(int uid) {
		Uid = uid;
	}
	public String getPkinnd() {
		return Pkind;
	}
	public void setPkind(String pkind) {
		Pkind = pkind;
	}
	
	
	
	

}
实体类

package com.Podcast.Dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import com.Podcast.entity.*;
import com.Podcast.dbutil.*;

public class GetPostDao {

	public List GetPost(int Pid){
		
		List lstpost=new ArrayList();
		DBconn dbconn=new DBconn();
		String strSQL="select * from posttopic where Pid=?";
		ResultSet rs=dbconn.execQuery(strSQL,new Object[]{Pid});
		try{
			while(rs.next()){
				PostBar postbar=new PostBar();
				postbar.setPid(rs.getInt("Pid"));
				postbar.setTopic(rs.getString("Topic"));
				postbar.setMessage(rs.getString("Message"));
				postbar.setPDate(rs.getString("PDate"));
				postbar.setChangeDate(rs.getString("ChangeDate"));
				postbar.setUid(rs.getInt("Uid"));
				postbar.setPkind(rs.getString("PKind"));
				lstpost.add(postbar);
			}
			return lstpost;
		} catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{
			dbconn.closeConn();
		}
		
	}
	
}
读取数据库数据具体方法

package com.Podcast.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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.Podcast.Dao.GetPostDao;

/**
 * Servlet implementation class PostBar
 */
@WebServlet("/PostBar")
public class PostBarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PostBarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		GetPostDao gpd=new GetPostDao();
		
		List list=gpd.GetPost(4);
		request.setAttribute("list",list);
		
		request.getRequestDispatcher("/PostBar.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
servlet方法

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>





贴吧



id ${post.pid }
主题 ${post.topic }
内容 ${post.message }
jsp页面



	PostBarServlet
	com.Podcast.servlet.PostBarServlet



	PostBarServlet
	/servlet/PostBarServlet

servlet注册xml文件


你可能感兴趣的:(java)