开学考试——数据库增删改查实现
数据库连接:
package util; import java.sql.*; public class JDBCTools { private static Connection connection; private static String url="jdbc:mysql://localhost:3306/runoob?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";//runoob为数据库名称 private static String user="root";//用户名 private static String pass="123456";//密码 static { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection(){ try { connection= DriverManager.getConnection(url,user,pass); } catch (SQLException e) { e.printStackTrace(); } return connection; } public static void release(Connection connection, Statement statement, ResultSet resultSet){ try { if (connection!=null) { connection.close(); } if (statement!=null){ statement.close(); } if (resultSet!=null){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
bean层
package bean; public class Bean { String name;//姓名 String yijian;//意见 String xinwen;//新闻正文 String time;//时间 String lanmu;//栏目 String xinwenname;//新闻名 String guanjian;//关键词 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getYijian() { return yijian; } public void setYijian(String yijian) { this.yijian = yijian; } public String getXinwen() { return xinwen; } public void setXinwen(String xinwen) { this.xinwen = xinwen; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getLanmu() { return lanmu; } public void setLanmu(String lanmu) { this.lanmu = lanmu; } public String getXinwenname() { return xinwenname; } public void setXinwenname(String xinwenname) { this.xinwenname = xinwenname; } public String getGuanjain() { return guanjian; } public void setGuanjain(String guanjian) { this.guanjian = guanjian; } @Override public String toString() { return "Bean [name=" + name + ", yijian=" + yijian + ", xinwen=" + xinwen + ", time=" + time + ", lanmu=" + lanmu + ", xinwenname=" + xinwenname + ", guanjian=" + guanjian + "]"; } public Bean(String xinwenname,String xinwen, String lanmu,String name,String time,String guanjian) { this.xinwenname=xinwenname; this.xinwen=xinwen; this.lanmu=lanmu; this.name=name; this.time=time; this.guanjian=guanjian; } }
依赖层
package respository; import bean.Bean; import util.JDBCTools; import java.sql.*; import java.util.ArrayList; import java.util.List; public class BeanRespository { public List<Bean> findAll(){//查询 List<Bean> list = new ArrayList<>(); Connection connection=null; PreparedStatement preparedStatement=null; ResultSet resultSet=null; try { connection=JDBCTools.getConnection(); String sql ="select * from xinwenq";//查询语句 preparedStatement=connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ //从resultSet拿出每个属性数据 String xinwenname=resultSet.getString(1); String xinwen=resultSet.getString(2); String lanmu=resultSet.getString(3); String name=resultSet.getString(4); String yijian=resultSet.getString(5); String huifu=resultSet.getString(6); //这里可以理解为,resultSet拿出每个属性数据赋予student对象,形成一个有数据的student对象 Bean we = new Bean(xinwenname, xinwen, lanmu, name, yijian,huifu); list.add(we);//可能多条数据,放到集合中 } } catch (SQLException e) { e.printStackTrace(); } finally { //调用JDBCTools,关闭connection,preparedStatement,resultSet JDBCTools.release(connection,preparedStatement,resultSet); } return list; } public void add(String xinwenname,String xinwen,String lanmu,//作者添加 String name) { Connection connection=null; PreparedStatement preparedStatement=null; try { connection= JDBCTools.getConnection(); String sql="insert into xinwenq(xinwenname,xinwen,lanmu,name) values (?,?,?,?)"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,xinwenname);//就是把id替代sql的第一个问号,id由前端传过来 preparedStatement.setString(2,xinwen); preparedStatement.setString(3,lanmu); preparedStatement.setString(4,name); // preparedStatement.setString(5,yijian); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCTools.release(connection,preparedStatement,null); } }public void deleteByxinwenname(String xinwenname) {//根据名字删除 Connection connection=null; PreparedStatement preparedStatement=null; try { connection= JDBCTools.getConnection(); String sql="delete from xinwenq where xinwenname=?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,xinwenname); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { JDBCTools.release(connection,preparedStatement,null); } } public Bean findByxinwenname(String xinwenname){//根据查询 Connection connection=null; PreparedStatement preparedStatement=null; ResultSet resultSet=null; Bean we=null; try { connection= JDBCTools.getConnection(); String sql="select * from xinwenq where xinwenname=?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,xinwenname); resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ String id2=resultSet.getString(1); String xinwen=resultSet.getString(2); String lanmu=resultSet.getString(3); String name=resultSet.getString(4); String yijian=resultSet.getString(5); String huifu=resultSet.getString(6); we = new Bean(id2, xinwen, lanmu, name, yijian,huifu); } } catch (SQLException e) { e.printStackTrace(); }finally { JDBCTools.release(connection,preparedStatement,resultSet); } return we; } public void update(String xinwenname,String xinwen,String lanmu,//修改新闻 String name){ Connection connection=null; PreparedStatement preparedStatement=null; try { connection= JDBCTools.getConnection(); String sql="update xinwenq set xinwen=?,lanmu=?,name=? where xinwenname=?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,xinwen); preparedStatement.setString(2,lanmu); preparedStatement.setString(3,name); preparedStatement.setString(4,xinwenname); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCTools.release(connection,preparedStatement,null); } } public void haoadd(String shenfen,String hao,String password//作者添加 ) { Connection connection=null; PreparedStatement preparedStatement=null; try { connection= JDBCTools.getConnection(); String sql="insert into denglu(shenfen,hao,password) values (?,?,?)"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,shenfen);//就是把id替代sql的第一个问号,id由前端传过来 preparedStatement.setString(2,hao); preparedStatement.setString(3,password); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCTools.release(connection,preparedStatement,null); } } }
实现层:
package respository; import bean.Bean; import util.JDBCTools; import java.sql.*; import java.util.ArrayList; import java.util.List; public class BeanRespository { public List<Bean> findAll(){//查询 List<Bean> list = new ArrayList<>(); Connection connection=null; PreparedStatement preparedStatement=null; ResultSet resultSet=null; try { connection=JDBCTools.getConnection(); String sql ="select * from xinwenq";//查询语句 preparedStatement=connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ //从resultSet拿出每个属性数据 String xinwenname=resultSet.getString(1); String xinwen=resultSet.getString(2); String lanmu=resultSet.getString(3); String name=resultSet.getString(4); String yijian=resultSet.getString(5); String huifu=resultSet.getString(6); //这里可以理解为,resultSet拿出每个属性数据赋予student对象,形成一个有数据的student对象 Bean we = new Bean(xinwenname, xinwen, lanmu, name, yijian,huifu); list.add(we);//可能多条数据,放到集合中 } } catch (SQLException e) { e.printStackTrace(); } finally { //调用JDBCTools,关闭connection,preparedStatement,resultSet JDBCTools.release(connection,preparedStatement,resultSet); } return list; } public void add(String xinwenname,String xinwen,String lanmu,//作者添加 String name) { Connection connection=null; PreparedStatement preparedStatement=null; try { connection= JDBCTools.getConnection(); String sql="insert into xinwenq(xinwenname,xinwen,lanmu,name) values (?,?,?,?)"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,xinwenname);//就是把id替代sql的第一个问号,id由前端传过来 preparedStatement.setString(2,xinwen); preparedStatement.setString(3,lanmu); preparedStatement.setString(4,name); // preparedStatement.setString(5,yijian); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCTools.release(connection,preparedStatement,null); } } public void madd(String huifu) {//评论 Connection connection=null; PreparedStatement preparedStatement=null; try { connection= JDBCTools.getConnection(); String sql="insert into xinwenq(huifu) values (?)"; preparedStatement=connection.prepareStatement(sql); //这里注意第一个参数对应sql语句问号的序号, preparedStatement.setString(1,huifu);//就是把id替代sql的第一个问号,id由前端传过来 preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCTools.release(connection,preparedStatement,null); } } public void deleteByxinwenname(String xinwenname) {//根据名字删除 Connection connection=null; PreparedStatement preparedStatement=null; try { connection= JDBCTools.getConnection(); String sql="delete from xinwenq where xinwenname=?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,xinwenname); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { JDBCTools.release(connection,preparedStatement,null); } } public Bean findByxinwenname(String xinwenname){//根据查询 Connection connection=null; PreparedStatement preparedStatement=null; ResultSet resultSet=null; Bean we=null; try { connection= JDBCTools.getConnection(); String sql="select * from xinwenq where xinwenname=?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,xinwenname); resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ String id2=resultSet.getString(1); String xinwen=resultSet.getString(2); String lanmu=resultSet.getString(3); String name=resultSet.getString(4); String yijian=resultSet.getString(5); String huifu=resultSet.getString(6); we = new Bean(id2, xinwen, lanmu, name, yijian,huifu); } } catch (SQLException e) { e.printStackTrace(); }finally { JDBCTools.release(connection,preparedStatement,resultSet); } return we; } public void update(String xinwenname,String xinwen,String lanmu,//修改新闻 String name){ Connection connection=null; PreparedStatement preparedStatement=null; try { connection= JDBCTools.getConnection(); String sql="update xinwenq set xinwen=?,lanmu=?,name=? where xinwenname=?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,xinwen); preparedStatement.setString(2,lanmu); preparedStatement.setString(3,name); preparedStatement.setString(4,xinwenname); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCTools.release(connection,preparedStatement,null); } } public void haoadd(String shenfen,String hao,String password//作者添加 ) { Connection connection=null; PreparedStatement preparedStatement=null; try { connection= JDBCTools.getConnection(); String sql="insert into denglu(shenfen,hao,password) values (?,?,?)"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,shenfen);//就是把id替代sql的第一个问号,id由前端传过来 preparedStatement.setString(2,hao); preparedStatement.setString(3,password); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCTools.release(connection,preparedStatement,null); } } }
对于本次考试,很多功能没有实现,普通用户界面没有,评论功能未实现,需要以后弥补。