数据库帮助类DBConnection
CRUD(增删改查)每个方法中都存在加载驱动程序,获得连接对象、关闭资源对象等重复代码,DBConnection帮助类避免了代码重复,实现代码重用和面向对象中的类设计的高内聚。
public class DBConnection { // 数据库驱动 // MySQL版本8.0后为com.mysql.cj.jdbc.Driver // MySQL版本8.0前为com.mysql.jdbc.Driver private static final String driverName = "com.mysql.cj.jdbc.Driver"; private static final String url = "jdbc:mysql://localhost:3306/testdb";//jdbc:mysql://localhost:端口号/数据库名 private static final String user = "root";//数据库用户 private static final String password = "yxj123";//数据库密码 private DBConnection(){} //加载驱动程序 static{ try{ Class.forName(driverName); }catch (ClassNotFoundException e){ e.printStackTrace(); } } //创建连接对象 public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,user,password); } //依次关闭资源对象 public static void close(ResultSet rs, Statement st,Connection conn){ try{ if(rs!=null){ rs.close(); } }catch (SQLException e){ e.printStackTrace(); }finally { try{ if(st!=null){ st.close(); } }catch (SQLException e){ e.printStackTrace(); }finally { if(conn!=null){ try{ conn.close(); }catch (SQLException e){ e.printStackTrace(); } } } } } }
CURD *** 作类StudentCURD
对Student表实现插入记录,查询记录、更新记录、删除记录的 *** 作
public class StudentCRUD { public static void main(String[] args) throws ClassNotFoundException, SQLException{ listAll();//...在该处调用方法 } //Student(id,name,sex,age,class) //查询student表中的所有记录 public static void listAll() throws SQLException{ Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try{ conn = DBConnection.getConnection();//创建连接对象 String query="select * from student";//sql语句 ps=conn.prepareStatement(query);//创建prepareStatement对象执行SQl语句 rs=ps.executeQuery();//查询后的结果集放在rs里 //遍历结果集对象并打印 while (rs.next()){ long id=rs.getInt("id"); String name=rs.getString("name"); String sex=rs.getString("sex"); String age=rs.getString("age"); String classInfo=rs.getString("class"); System.out.println("学号:"+id+";姓名:"+name+";性别:"+sex+";年龄:"+age+";班级:"+classInfo); } }finally { DBConnection.close(rs,ps,conn); } } //插入一条记录 public static void add() throws SQLException{ Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try{ conn = DBConnection.getConnection(); String insert="insert into student(id,name,sex,age,class) values(?,?,?,?,?)"; ps=conn.prepareStatement(insert); ps.setString(1,"1"); ps.setString(2,"李四"); ps.setString(3,"男"); ps.setString(4,"20"); ps.setString(5,"软件192"); ps.executeUpdate(); }finally { DBConnection.close(rs,ps,conn); } } //删除一条记录 public static void delete() throws SQLException{ Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try{ conn = DBConnection.getConnection(); String del="delete from student where id=?"; ps=conn.prepareStatement(del); ps.setString(1,"1"); ps.executeUpdate(); }finally { DBConnection.close(rs,ps,conn); } } //更新一条记录 public static void update() throws SQLException{ Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try{ conn = DBConnection.getConnection(); String update="update student set name=? where id=?"; ps=conn.prepareStatement(update); ps.setString(2,"1"); ps.setString(1,"王五"); ps.executeUpdate(); }finally { DBConnection.close(rs,ps,conn); } } }
MySQL驱动jar包
官网地址:https://dev.mysql.com/downloads/connector/j/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)