jdbc+jsp+javaweb

本文介绍了一个使用JSP与JavaWeb技术实现的商品列表展示应用案例。该案例中,通过JDBC连接MySQL数据库,并创建了包含商品信息的数据表。接着定义了User类映射数据库表结构,并通过JDBCUtils工具类进行数据库操作。最后,通过ListUserServlet实现了商品数据的获取及展示。
摘要由CSDN通过智能技术生成

jdbc+jsp+javaweb
下面展示数据库代码。

create database jdbc;
use jdbc;
create table users(
goods_id int primary key auto_increment,
name varchar( 50),
price decimal(20,2),
mktprice decimal( 20,2),
mkt_enable tinyint(4),
creatime timestamp);

insert into users(goods_id,name, price,mktprice,mkt_enable,creatime)
values('2', '汇源果汁100%苹果汁' , '24.99' , '30.00' , '0 ','2016-09-08');
insert into users(goods_id,name, price,mktprice,mkt_enable,creatime)
values( '4', '汇源果汁100%橙汁' , '23.88' , ' 30.00','1' ,'2017-09-08');
insert into users(goods_id,name,price,mktprice,mkt_enable,creatime)
values('5', '汇源果汁100%桃汁', '18.88' , ' 30.00' , ' 0' , ' 2019-09-08');

select * from users;

下面展示一些 User.java

package tzzy1907451057;

public class User {
	private int goods_id;
	private String name;
	private double price;
	private double mktprice;
	private int mkt_enable;
	private String creatime;
	public int getGoods_id() {
		return goods_id;
	}
	public void setGoods_id(int goods_id) {
		this.goods_id = goods_id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public double getMktprice() {
		return mktprice;
	}
	public void setMktprice(double mktprice) {
		this.mktprice = mktprice;
	}
	public int getMkt_enable() {
		return mkt_enable;
	}
	public void setMkt_enable(int mkt_enable) {
		this.mkt_enable = mkt_enable;
	}
	public String getCreatime() {
		return creatime;
	}
	public void setCreatime(String creatime) {
		this.creatime = creatime;
	}
}

下面展示一些 JDBCUtils.java`。

package tzzy1907451057;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class JDBCUtils {
	public static Connection getConnection() throws SQLException,ClassNotFoundException{
		Class.forName("com.mysql.jdbc.Driver");
		String url="jdbc:mysql://localhost:3306/jdbc";
		String username = "root";
		String password="123456";
		Connection conn= DriverManager.getConnection(url,username,password);
		return conn;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		getAll();
	}

	public static List<User> getAll(){
		Connection conn =null;
		Statement stmt = null;
		ResultSet rs = null;
		
		String sql = "select * from users";
		List<User> data = new ArrayList<User>();
		try {
			conn = getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			
			while(rs.next()) {
				int id = rs.getInt("goods_id");
				String name =rs.getString("name");
				double price = rs.getDouble("price");
				double mktprice = rs.getDouble("mktprice");
				int mkt_enable = rs.getInt("mkt_enable");
				String creatime=rs.getString("creatime");
			    User user = new User();
			    user.setCreatime(creatime);
			    user.setGoods_id(id);
			    user.setMkt_enable(mkt_enable);
			    user.setMktprice(mktprice);
			    user.setName(name);
			    user.setPrice(price);
			    data.add(user);
			}
			return data;
		}catch(Exception e) {
			e.printStackTrace();
		}return null;
	}
}

下面展示一些 ListUserServlet.java。

package tzzy1907451057;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ListUserServlet
 */
public class ListUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListUserServlet() {
        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
		List<User> data=JDBCUtils.getAll();
		request.setAttribute("data", data);
		request.getRequestDispatcher("list.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);
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值