第七周JSP增删改查
package ak74; public class Haoshu {
package ak74; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.mysql.jdbc.Driver; public class JDBCUtils { //创建工具类 方便使用 //获取连接 public static Connection getCon() throws Exception{ Class.forName( "com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useSSL=false", "root", "guoshaofeng"); return con; } //关闭数据流 public static void realse(ResultSet rst,Statement st,Connection con){ if(rst!=null){ try { rst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(st!=null){ try { st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(con!=null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package ak74; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class HaoshuDao { private List<Haoshu> list; /** * @param args */ //增加 public boolean insert(Haoshu haoshu){ Statement st=null; Connection con=null; try{ con=JDBCUtils.getCon(); st=con.createStatement(); String sql="insert into haoshu "+"values('" +haoshu.getId()+"','" +haoshu.getName()+"','" +haoshu.getMvp()+"','" +haoshu.getChenghao()+"'" +")"; int row=st.executeUpdate(sql); if(row==1){ return true; } }catch(Exception e){ throw new RuntimeException(e); } finally{ JDBCUtils.realse(null, st, con); } return false; } //查询所有 public List<Haoshu> FindAllHaoshu(){ Connection con=null; Statement st=null; ResultSet rst=null; try{ con= JDBCUtils.getCon(); st=con.createStatement(); String sql="select*from haoshu;"; rst=st.executeQuery(sql); List<Haoshu> list=new ArrayList<Haoshu>(); while(rst.next()){ Haoshu haoshu=new Haoshu(); haoshu.setId(rst.getInt("id")); haoshu.setName(rst.getString("name")); haoshu.setMvp(rst.getString("mvp")); haoshu.setChenghao(rst.getString("chenghao")); list.add(haoshu); } return list; }catch(Exception e){ throw new RuntimeException(e); } finally{ JDBCUtils.realse(rst, st, con); } } //根据ID来查询记录 public Haoshu FindHaoshuByid(int id){ Connection con=null; PreparedStatement st=null; ResultSet rst=null; try{ con= JDBCUtils.getCon(); String sql="select*from haoshu where id=?;"; st=con.prepareStatement(sql); st.setInt(1, id); rst=st.executeQuery(); if(rst.next()){ Haoshu haoshu=new Haoshu(); haoshu.setId(rst.getInt("id")); haoshu.setName(rst.getString("name")); haoshu.setMvp(rst.getString("mvp")); haoshu.setChenghao(rst.getString("chenghao")); return haoshu; } }catch(Exception e){ throw new RuntimeException(e); } finally{ JDBCUtils.realse(rst, st, con); } return null; } //根据id修改name和mvp public boolean Update(Haoshu haoshu){ PreparedStatement st=null; Connection con=null; try{ con=JDBCUtils.getCon(); String sql="update haoshu set name=?,mvp=? where id=?"; st=con.prepareStatement(sql); st.setString(1, haoshu.getName()); st.setString(2, haoshu.getMvp()); st.setInt(3, haoshu.getId()); int row=st.executeUpdate(); if(row==1){ return true; } }catch(Exception e){ throw new RuntimeException(e); } finally{ JDBCUtils.realse(null, st, con); } return false; } //根据id删除 public boolean Delete(int id){ PreparedStatement st=null; Connection con=null; try{ con=JDBCUtils.getCon(); String sql="delete from haoshu where id=?"; st=con.prepareStatement(sql); st.setInt(1, id); int row=st.executeUpdate(); if(row==1){ return true; } }catch(Exception e){ throw new RuntimeException(e); } finally{ JDBCUtils.realse(null, st, con); } return false; } }
package ak74; public class JdbcInsertText { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub HaoshuDao dao=new HaoshuDao(); Haoshu haoshu=new Haoshu(); haoshu.setId(6); haoshu.setName("兰亭集序"); haoshu.setMvp("王小姐"); haoshu.setChenghao("GXJ"); dao.insert(haoshu) ; } }
package ak74; public class FindHaoshuByid { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub HaoshuDao dao=new HaoshuDao(); Haoshu h= dao.FindHaoshuByid(6); System.out.println(h.getName()); } }
package ak74; public class Update { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub HaoshuDao dao=new HaoshuDao(); Haoshu hh=new Haoshu(); hh.setId(6); hh.setName("徐凤年"); hh.setMvp("bl"); boolean b=dao.Update(hh); System.out.println(b); } }
package ak74; public class Delete { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub HaoshuDao dao=new HaoshuDao(); boolean bb=dao.Delete(2); System.out.println(bb); } }
/** * @param args */ //一,创建JavaBean用来封装用户数据 private int id; private String name; private String mvp; private String chenghao; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMvp() { return mvp; } public void setMvp(String mvp) { this.mvp = mvp; } public String getChenghao() { return chenghao; } public void setChenghao(String chenghao) { this.chenghao = chenghao; } public static void main(String[] args) { // TODO Auto-generated method stub } }