jsp mysql模板_Jsp+servlet+mysql搭建套路

1、建立数据库

根据需求建立相应的数据库

确立数据库的字段、属性、主键等

2、建立javaweb项目,搭建开发环境

在开发环境的/WebRoot/WEB-INF

下建立lib文件夹,存放需要使用的jar包

常用的包:

mysqldriver.jar

beanutil.jar

commons-logging.jar

jstl.jar

standard.jar

3、java内部书写,建立domain包

在其下书写bean类,类的字段与数据库对应

4、定义dao和其接口

(1)接口dao包:对daoimlents包进行抽象(为了增加dao的课扩展性)

//增加客户

//这里对数据库中的数据进行操作增删,加入sql语句

模板:

Connection conn =null;

PreStatement stmt = null;

ResultSet rs = null;

if(c==null)

//参数不对异常见还有NUllException,就是参数为空的时候

throw new IllegaException();

try{

conn=Jutil.getConnection();

stmt=conn.prepareStatement("sql语句");

stmt.setString(1,c.getName());

.

.

.

.

stmt.getexcuteUpdate();

}catch(Exception e){

throw new DaoException(e);

}finally{

Jtuil.release(*,*,*);*参数名字

}

void addCustomer(Customer c);

//根据id删除客户信息。名字体现要实现的功能

void delCustomerById( String customerId);

//更改客户的信息 IdIsNullException如果参数id为null,则抛出异常

//建立异常包,命名IdIsNullException类继承Exception

void updateCustomer(Customer c) throw IdIsNullException;

//查询所有用户信息

List findAll();

//根据客户id查询客户信息(单个某个客户)

Customer findCustomerById(String customerId);

注意;

查询方法还可拓展,比如根据用户的其他爱好,地址,等信息进行查找用户

(2)daoimlents包对dao具体实现,就是功能的实现在此书写,

四大方法增删改查

daoimlents implements dao{}

5、建立与数据库连接的Jutil包

其中书写Jutil类

两个方法:

//书写数据链接

//为增加扩展性一般情况,代码中的String字符串都写在properties配置文件中,Class.forname("com.mysql.jdbc.Driver");

//建立properties文本,然后写入

className=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/databaseName

user=root

password=123456

建立读取此文本的方法

InputSteam in= Jutil.class.getClassLoader.getReSourseAsStream("propertiesname.properties");

Properties pro = new Properties();

pro.load(in);

url=pro.getProperties("url");

password=pro.getProperties("password");

user=pro.getProperties("user");

className=pro.getProperties("className");

public static Connection getConnection(){}

//书写资源释放

public static void relase(ResultSet st,Statement st,Connection conn){}

6、书写模板位置

7、测试

java.lang.NoClassDefFoundError: Could not initialize class com.Jutil.Jutil

at com.daoImpl.CustomerDaoImpl.addCustomer(CustomerDaoImpl.java:44)

at com.test.CustomerDaoImplTest.testAddCustomer(CustomerDaoImplTest.java:30)

数据库名字与javabean的名字不同,配置文件名字不同,没有把jar包变成buildpath

8、业务层搭建

建立service接口

书写service实现

因为业务层调用dao层

private CustomerDao dao= new CustomerDaoImpl();

9、servlet和jsp书写

打开web.xml将servlet设为主页

servlet调用service所以需要

private Service s = new ServiceImpl();

servlet 建立显示所有信息的界面的连接

List cs = s.findAll();

request.setAttribute("cs",cs);

request.getRequestDispathcher("/listshow.jsp").forword(request,response);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值