javaweb后台的增删改查

第一步先建立数据库的连接

1 建立包  cn.edu.hpu.util 之后建立数据库连接类DBO


package cn.edu.hpu.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBO { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection() { Connection conn=null; try { conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sas", "root", "123456"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void close(Statement st,ResultSet rs,Connection conn) { try { if(st!=null) { st.close(); } if(rs!=null) { rs.close(); } if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(PreparedStatement pst,Connection conn) { try { if(pst!=null) { pst.close(); } if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

2.建立测试包cn.edu.hpu.test 然后建立测试类test

import cn.edu.hpu.service.Manager; import cn.edu.hpu.service.ManagerImpl; import java.sql.Connection; import cn.edu.hpu.util.DBO; public class Test { public static void main(String[] args) { Connection conn=DBO.getConnection(); if(conn!=null) { System.out.println("ok!"); }else{ System.out.println("error!"); } } }

第二步 1. 建立与数据库中表相关的表的类user


package cn.edu.hpu.model; public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

建立增删改查的接口及实现类接口


package cn.edu.hpu.service; import java.util.List; import cn.edu.hpu.model.User; public interface Manager { //添加数据 public boolean addUser(User user); //更新数据 public boolean updateUser(int id,User user); //查询数据 public List getAllUser(); //删除数据 public boolean deletUser(int id); }

建立接口实现类

package cn.edu.hpu.service; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import cn.edu.hpu.model.User; import cn.edu.hpu.util.DBO; public class ManagerImpl implements Manager { @Override public boolean addUser(User user) { // TODO Auto-generated method stub boolean flag=false; Connection conn=null; PreparedStatement pst=null; String sql="insert into user(username,password) value(?,?)"; conn=DBO.getConnection(); try { pst=conn.prepareStatement(sql); pst.setString(1, user.getUsername()); pst.setString(2, user.getPassword()); int row=pst.executeUpdate(); if(row>0) { flag=true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBO.close(pst, conn); } return flag; } @Override public boolean updateUser(int id, User user) { // TODO Auto-generated method stub boolean flag=false; String sql="update user set username=?,password=? where id="+id; Connection conn=DBO.getConnection(); PreparedStatement pst=null; try { pst=conn.prepareStatement(sql); pst.setString(1, user.getUsername()); pst.setString(2, user.getPassword()); int row=pst.executeUpdate(); if(row>0) { flag=true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBO.close(pst, conn); } return flag; } @Override public List getAllUser() { // TODO Auto-generated method stub List list=new ArrayList (); String sql="select * from user"; Connection conn=null; Statement st=null; ResultSet rs=null; conn=DBO.getConnection(); try { st=conn.createStatement(); rs=st.executeQuery(sql); while(rs.next()) { User user = new User(); user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); list.add(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBO.close(st, rs, conn); } return list; } @Override public boolean deletUser(int id) { // TODO Auto-generated method stub boolean flag=false; String sql="delete from user where id="+id; Connection conn=DBO.getConnection(); PreparedStatement pst=null; try { pst=conn.prepareStatement(sql); pst.executeUpdate(); flag=true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBO.close(pst, conn); } return flag; } }

最后一步在测试类里测试

package cn.edu.hpu.test; import java.util.ArrayList; import java.util.List; import cn.edu.hpu.model.User; import cn.edu.hpu.service.Manager; import cn.edu.hpu.service.ManagerImpl; public class Test { public static void main(String[] args) { // Connection conn=DBO.getConnection(); // if(conn!=null) { // System.out.println("ok!"); // }else{ // System.out.println("error!"); // } // //添加测试 // Manager mng=new ManagerImpl(); // User user = new User(); // user.setUsername("han"); // user.setPassword("123"); // for(int i=1;i<100;i++) // mng.addUser(user); // if(flag) { // System.out.println("ok"); // }else{ // System.out.println("error"); // } //更新测试 // Manager mng=new ManagerImpl(); // User user=new User(); // user.setUsername("fang"); // user.setPassword("222"); // boolean flag=mng.updateUser(1, user); // if(flag=true) { // System.out.println("ok"); // }else{ // System.out.println("error"); // } // //删除数据 // Manager mng =new ManagerImpl(); // boolean flag=mng.deletUser(1); // if(flag) { // System.out.println("ok"); // }else{ // System.out.println("error"); // } //查询所有信息 Manager mng=new ManagerImpl(); List list=mng.getAllUser(); for(int i=1;i

你可能感兴趣的:(javaweb)