java的MVC-模式的数据库增删改查.doc
《java的MVC-模式的数据库增删改查.doc》由会员分享,可在线阅读,更多相关《java的MVC-模式的数据库增删改查.doc(43页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-datejava的MVC-模式的数据库增删改查攀枝花学院实验报告攀枝花学院实验报告实验课程:JAVA应用开发 实验项目:JSP操作数据库实验日期:2013.5 系:计算机 班级:10计本1班 姓名:蒋志勇 学号:201010801035指导老师:范胜波成绩:一 实验目的:1. 掌握JSP,servlet的基本应用。2. 掌握用JSP对数据库进行增,删,查,改操作。二 实验设备
2、Eclipse,Mysql数据库,Tomact三 实验内容1. 创建数据库javaee和表Users,Users的sql语句为: CREATE TABLE users ( id int(11) NOT NULL AUTO_INCREMENT, UserName varchar(30) NOT NULL, Password varchar(30) NOT NULL, PRIMARY KEY (id) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;2. 编写数据库连接、增、删、查、改类UserDao,核心代码如下:public class
3、UserDao final static String url = jdbc:mysql:/localhost:3306/javaee?useUnicode=true&characterEncoding=UTF-8;final static String user = root;final static String password = 123456;static try Class.forName(com.mysql.jdbc.Driver); catch (ClassNotFoundException e) e.printStackTrace();/数据库连接private Connec
4、tion getConnection() throws SQLException Connection conn = DriverManager.getConnection(url, user, password);return conn;/根据ID删除用户public boolean delUser(int id)boolean flag=false;String addUserSQL=DELETE FROM Users WHERE ID=?;Connection conn = null;PreparedStatement pst = null;try conn=getConnection(
5、);pst=conn.prepareStatement(addUserSQL);pst.setInt(1, id);flag=(pst.executeUpdate()=1); catch (SQLException e) e.printStackTrace();finallytry pst.close();conn.close(); catch (SQLException e) e.printStackTrace();return flag;/增加用户public boolean addUser(User newUser)boolean flag=false;String addUserSQL
6、=INSERT INTO Users(UserName,Password) VALUES(?,?);Connection conn = null;PreparedStatement pst = null;try conn=getConnection();pst=conn.prepareStatement(addUserSQL);pst.setString(1, newUser.getUserName();pst.setString(2, newUser.getPassword();flag=(pst.executeUpdate()=1); catch (SQLException e) e.pr
7、intStackTrace();finallytry pst.close();conn.close(); catch (SQLException e) e.printStackTrace();return flag;/分页查询用户public List getUser(int startIndex,int count) List userList = new ArrayList();String getUserSQL = SELECT ID,UserName,Password FROM Users ORDER BY ID Desc LIMIT ?,?;Connection conn = nul
8、l;PreparedStatement pst = null;ResultSet rs = null;try conn = getConnection();pst = conn.prepareStatement(getUserSQL);pst.setInt(1, startIndex);pst.setInt(2, count);rs = pst.executeQuery();while (rs.next() User u = new User(rs.getInt(ID), rs.getString(UserName),rs.getString(Password);userList.add(u)
9、; catch (SQLException e) e.printStackTrace();finallytry rs.close();pst.close();conn.close(); catch (SQLException e) e.printStackTrace();return userList;/更新用户数据public boolean updateUser(String userName,String password,int id)boolean flag=false;String SQL=update Users set UserName=?, Password=? where
10、id=?;Connection conn = null;PreparedStatement pst = null;try conn=getConnection();pst=conn.prepareStatement(SQL);System.out.println(userName);System.out.println(password);pst.setString(1, userName);pst.setString(2, password);pst.setInt(3, id);flag=(pst.executeUpdate()=1);System.out.println(flag); ca
11、tch (SQLException e) e.printStackTrace();finallytry pst.close();conn.close(); catch (SQLException e) e.printStackTrace();return flag; 3. 编写控制类SaveUserServlet.java和查询雷ListUserServlet.java,代码如下。SaveUserServlet代码:public class SaveUserServlet extends HttpServlet private static final long serialVersionUI
12、D = -5350720307591869348L;Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doAction(request, response);Overrideprotected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOExceptio
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java MVC 模式 数据库 增删
限制150内