首先创建News,然后实现其中方法package cn.hpu.model;public class News { private int id; private String title; private String context;public int getId() {return id;}
public void setId(int id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}
public String getContext() {return context;}public void setContext(String context) {this.context = context;} }然后创建操作接口package cn.hpu.service;import cn.hpu.model.News;
import java.util.List;public interface NewsMa { public List
import cn.hpu.model.News;import cn.hpu.util.DBO;public class NewsMaImpl implements NewsMa{public List
链接数据库package cn.hpu.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;static{ try {Class.forName(DBDRIVER);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
创建登陆页面并实现验证。此处省略 实现添加的时候别忘了接口add里面的public boolean add(News news) {// TODO Auto-generated method stubboolean flag=false;Connection conn=null;PreparedStatement pst =null;try {
conn=DBO.getConnection();String sql=('insert into news (title,context) value(?,?)');pst=conn.prepareStatement(sql);pst.setString(1,news.getTitle());pst.setString(2, news.getContext());int rows=pst.executeUpdate();if(rows>0){flag=true;}} catch (Exception e) {// TODO: handle exception}finally{DBO.close(pst, conn);}return flag;} }