2024/02/17
package DAO; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBUtil //用于连接数据库 { String name="root"; String password="123456"; public Connection getConnection() { try { Class.forName("com.mysql.cj.jdbc.Driver"); return DriverManager.getConnection("jdbc:mysql://localhost:3306/test",name,password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public void closeConnection(Connection conn) { if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package DAO; import bean.LiuShui; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class liushuiDAO { public void insert (LiuShui liushui) { DBUtil db=new DBUtil(); Connection conn=db.getConnection(); String sql="insert into liushui(schoolid,stuid,is1,is2,name,zhuanye,zongfen,math,zhengzhi,english,zy) values(?,?,2,2,?,?,?,?,?,?,?)"; try { PreparedStatement pstm=conn.prepareStatement(sql); pstm.setString(1, liushui.getSchoolid()); pstm.setString(2,liushui.getStuid()); pstm.setString(3,liushui.getName()); pstm.setString(4,liushui.getZhuanye()); pstm.setString(5,liushui.getZongfen()); pstm.setString(6,liushui.getMath()); pstm.setString(7,liushui.getZhengzhi()); pstm.setString(8, liushui.getZhengzhi()); pstm.setString(9, liushui.getZy()); pstm.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } } }
package DAO; import bean.School; import bean.Stu; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class SchoolDAO { public boolean Login(School school) //登录 { DBUtil db=new DBUtil(); Connection conn=db.getConnection(); String sql="select * from school where id=? and password=?"; try { PreparedStatement pstm=conn.prepareStatement(sql); pstm.setString(1,school.getId()); pstm.setString(2,school.getPassword()); ResultSet rs=pstm.executeQuery(); if(rs.next()) { return true; } } catch (SQLException e) { throw new RuntimeException(e); } return false; } public List<School> tiaojiyuanxiao() { List<School> SchoolList =new ArrayList<>(); DBUtil db=new DBUtil(); Connection conn=db.getConnection(); String sql="select *from school"; try { PreparedStatement pstm=conn.prepareStatement(sql); ResultSet rs=pstm.executeQuery(); while(rs.next()) { School school=new School(); school.setId(rs.getString("id")); school.setName(rs.getString("name")); school.setPlan(rs.getString("plan")); school.setRt(rs.getString("rt")); SchoolList.add(school); } } catch (SQLException e) { throw new RuntimeException(e); } return SchoolList; } }