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);
}
}