用JDBC连接数据库,并显示其中内容。

1,主要框架

在这里插入图片描述

2 StuBean

package com.qst.bean;

public class StuBean {
	private int id;
	private String name;
	private String game;
	private String countries;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public String getCountries() {
		return countries;
	}
	public void setCountries(String countries) {
		this.countries = countries;
	}
	public String getGame() {
		return game;
	}
	public void setGame(String game) {
		this.game = game;
	}

	
	
	
	
}

3 StuDao

package com.qst.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import com.qst.bean.StuBean;
import com.qst.util.DBUtil;

public class StuDao {


	public ArrayList<StuBean> getList() {
		// TODO Auto-generated method stub
		ArrayList<StuBean> tag_Array = new ArrayList<StuBean>();
		Connection conn = DBUtil.getConnection();
		String sql = "select * from neirong";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()){
				StuBean tag = new StuBean();
				tag.setId(rs.getInt("id"));
				tag.setName(rs.getString("name"));
				tag.setGame(rs.getString("game"));
				tag.setCountries(rs.getString("countries"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeJDBC(rs, stm, conn);
		}
		return tag_Array;
	}
	
	
}

4 IndexView

package com.qst.servlet;

import java.io.IOException;
import java.util.ArrayList;

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.qst.bean.StuBean;
import com.qst.dao.StuDao;

/**
 * Servlet implementation class IndexView
 */
@WebServlet("/IndexView")
public class IndexView extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexView() {
        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
		ArrayList<StuBean> bookdata = new ArrayList<StuBean>();
		

  	   StuDao bookdao = new StuDao();
  	   bookdata = (ArrayList<StuBean>)bookdao.getList();
  	   
  	 request.setAttribute("usrid", bookdata);
	request.getRequestDispatcher("index.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
		doGet(request, response);
	}

}

5 DBUtil

package com.qst.util;

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

public class DBUtil {
	//数据库mysql
	static String name = "root";//用户名
	static String password = "123456";//密码
	static String url = "jdbc:mysql://localhost:3306/zuoye";
	//MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称(zuoye)
	static {
		try{
			//加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}
	}
	//进行数据库连接
	public static Connection getConnection() {
		Connection conn = null;
		try{
			conn = DriverManager.getConnection(url, name, password);
		}catch(SQLException e){
			e.printStackTrace();
		}
		return conn;
	}
	//关闭 rs,pstmt,conn
	public static void closeJDBC(ResultSet rs, PreparedStatement pstmt, Connection conn) {
		if(rs !=null){
			try{
				rs.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(pstmt !=null){
			try{
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(conn != null)
		{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}

6index.jsp

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>

</head>

<body >

<form action="IndexView">
<input id="View"  type="submit"  value="显示">
</form>
<table cellpadding="30">
				<thead>
					<tr>
						<th>ID</th>
						<th>姓名</th>
						<th>班级</th>
						<th>比赛项目</th>

					</tr>
				</thead>
				<tbody>
					<c:forEach items="${usrid}" var="w">
					<tr>
					<td>${w.id}</td>
					<td>${w.name}</td>
					<td>${w.game}</td>
					<td>${w.countries}</td>
					</tr>
					</c:forEach>
				</tbody>
			</table>

</body>
</html>

7数据库

在这里插入图片描述
在这里插入图片描述

8运行

在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值