专业实训第四天 java+jsp+myeclipes 项目实训

导入mysql驱动包

不再像java项目时需要3 3 2,直接复制驱动包到项目目录下WebRoot >>Web-INF>>lib,就可以了,会自动将jar包加载。

html+jsp简单知识点
<%@   %>  指令
<%    %>  语句
<%=   %> 表达式
pageEncoding="utf-8" utf-8编码
request.setCharacterEncoding("utf-8"); 设置请求的编码方式,传递中文时使用
项目实训简单车辆租赁系统
数据库
CREATE DATABASE rentcar;
USE rentcar;
CREATE TABLE cars (
  id int PRIMARY KEY AUTO_INCREMENT,
  brant varchar(20) NOT NULL,
  miles int(11) NOT NULL,
  status int(11) NOT NULL,
  detail varchar(100) NOT NULL,
  )
  insert  into `cars`(`id`,`brant`,`miles`,`status`,`detail`) values (1,'bmw',1000,0,'i am bmw'),(2,'bj',200,1,'i am bj'),(3,'das',152,1,'dasd'),(4,'das',152,1,'dasd');
java

实体类

public class Cars {
    private int id;
    private String brant;
    private int miles;
    private int status;
    private String detail;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getBrant() {
        return brant;
    }
    public void setBrant(String brant) {
        this.brant = brant;
    }
    public int getMiles() {
        return miles;
    }
    public void setMiles(int miles) {
        this.miles = miles;
    }
    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
    public String getDetail() {
        return detail;
    }
    public void setDetail(String detail) {
        this.detail = detail;
    }
    public Cars(int id, String brant, int miles, int status, String detail) {
        super();
        this.id = id;
        this.brant = brant;
        this.miles = miles;
        this.status = status;
        this.detail = detail;
    }
    public Cars() {
        super();
    }

}

基本工具类

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

public class BaseDao {
    private String driver = "com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/RentCar";
    private String name = "root";
    private String pwd = "123456";

    public Connection getCon() {
        Connection con = null;
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,name,pwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }       
        return con;
    }

    public void closeAll(Connection con,PreparedStatement ps,ResultSet rs){
        try {
            if(rs != null) rs.close();
            if(ps != null) ps.close();
            if(con != null) con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int excute(String sql,Object ... param){
        int res = 0;
        Connection con = this.getCon();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            int i = 1;
            for (Object o : param) {
                ps.setObject(i, o);
                i++;
            }
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(con, ps, null);
        }
        return res;
    }

}

数据访问类

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

public class RentCarDao extends BaseDao{
    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;
    public ArrayList<Cars> getAll(){
        ArrayList<Cars> list = new ArrayList<Cars>();
        String sql = "select * from Cars";
        con = this.getCon();
        try {
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                Cars c = new Cars(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getString(5));
                list.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(con, ps, rs);
        }
        return list;
    }

    public int add (Cars c) {
        String sql = "insert into cars values(null,?,?,?,?)";
        return this.excute(sql,c.getBrant(),c.getMiles(),c.getStatus(),c.getDetail());
    }

    public int update (Cars c) {
        String sql = "update cars set brant = ?,miles = ?,status = ?,detail = ? where id = ?";
        return this.excute(sql, c.getBrant(),c.getMiles(),c.getStatus(),c.getDetail(),c.getId()); 
    }

    public int delete (int id) {
        String sql = "delete from cars where id = ?";
        return this.excute(sql,id);
    }

    public Cars getById(int id){
        Cars c = null;
        String sql = "select * from where id = ?";
        con = this.getCon();
        try {
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs.next()){
                c = new Cars(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getString(5));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(con, ps, rs);
        }
        return c;
    }
}
jsp

主页

<%@ page language="java" import="java.util.*,dao.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

ArrayList<Cars> list = new ArrayList(); 
list = new RentCarDao().getAll();

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    <h1 align="center">简单车辆租赁系统</h1>
    <table border="1" width="500px" align="center">
        <tr>
            <th>id</th>
            <th>brant</th>
            <th>miles</th>
            <th>status</th>
            <th>detail</th>
            <th>operation</th>
        </tr>
        <% for(Cars c : list) { %>
        <tr>
            <td><%=c.getId() %></td>
            <td><%=c.getBrant() %></td>
            <td><%=c.getMiles() %></td>
            <td><%=c.getStatus() %></td>
            <td><%=c.getDetail() %></td>
            <td>
                <a href="delete.jsp?id=<%=c.getId() %>">delete</a>
                <a href="update.jsp?id=<%=c.getId() %>">update</a>
            </td>
        </tr>
        <% } %>
    </table>
    <p align="center"><a href="add.jsp">add new Car</a></p>
  </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值