标签:close 静态 创建 mysql [] test 数据类型 integer man
Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能。来源百度百科
简而言之,Dbutils就是封装了jdbc的代码,简化了jdbc的dao层的操作。
QueryRunner queryRunner = new QueryRunner();
public void insert(Student student) { sql = "insert into student(name,clazz,grade) values(?,?,?)"; try { //JDBCTools.getConnection():数据库连接 //sql:数据库查询sql语句 //student.getName():需要的参数,参数与sql中的参数的数量保持一致 queryRunner.update(JDBCTools.getConnection(),sql,student.getName(),student.getClazz(),student.getGrade()); } catch (SQLException e) { e.printStackTrace(); } }
public Student findById(int id) { sql = " select * from student where id = ?"; Student student = null; try { student = queryRunner.query(JDBCTools.getConnection(),sql,new BeanHandler<Student>(Student.class),id); } catch (SQLException e) { e.printStackTrace(); } return student; }
ResultSetHandler<javabean类型> rsh = new BeanHandler<javabean类型>(javabean.class);
List<javaBean类型> list = <List<javaBean类型>> new BeanListHandler<javaBean类型>(javaBean.class);
Map <String,Object> map = new MapHandler();
List<Map<String,Object>> listmap = new MapListHandler();
List<Object> nameList = new ColumnHandler();
ResultSetHandler resultSetHandler = new ScalarHandler();
package com.dreambamboo.entity; public class Student { private int id; private String name; private String grade; private String clazz; public Student(int id, String name, String grade, String clazz) { this.id = id; this.name = name; this.grade = grade; this.clazz = clazz; } public Student() { } 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 getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public String getClazz() { return clazz; } public void setClazz(String clazz) { this.clazz = clazz; } public Student(String name, String grade, String clazz) { this.name = name; this.grade = grade; this.clazz = clazz; } @Override public String toString() { return "Student{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", grade=‘" + grade + ‘\‘‘ + ", clazz=‘" + clazz + ‘\‘‘ + ‘}‘; } }
package com.dreambamboo.dao; import com.dreambamboo.entity.Student; import java.util.List; public interface StudentDao { /** * 添加学生信息 * @param student */ public void insert(Student student); /** * 更新学生信息 * @param student */ public void update(Student student); /** * 删除学生信息 * @param id */ public void delete(int id); /** * 根据学生编号查询学生信息 * @param id * @return */ public Student findById(int id); /** * 查询所有学生信息 * @return */ public List<Student> findAll(); /** * 查询学生总数 * @return */ public int studentCount(); }
package com.dreambamboo.dao.impl; import com.dreambamboo.dao.StudentDao; import com.dreambamboo.entity.Student; import com.dreambamboo.util.JDBCTools; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import java.sql.SQLException; import java.util.List; public class StudentDaoImpl implements StudentDao { private QueryRunner queryRunner = null;//查询运行器 public StudentDaoImpl(){ queryRunner = new QueryRunner(); } String sql = null; @Override public void insert(Student student) { sql = "insert into student(name,clazz,grade) values(?,?,?)"; try { //JDBCTools.getConnection():数据库连接 //sql:数据库查询sql语句 //student.getName():需要的参数,参数与sql中的参数的数量保持一致 queryRunner.update(JDBCTools.getConnection(),sql,student.getName(),student.getClazz(),student.getGrade()); } catch (SQLException e) { e.printStackTrace(); } } @Override public void update(Student student) { sql = " update student set name = ? ,clazz = ?,grade = ? where id = ?"; try { queryRunner.update(JDBCTools.getConnection(),sql,student.getName(),student.getClazz(),student.getGrade(),student.getId()); } catch (SQLException e) { e.printStackTrace(); } } @Override public void delete(int id) { sql = "delete from student where id = ?"; try { queryRunner.update(JDBCTools.getConnection(),sql,id); } catch (SQLException e) { e.printStackTrace(); } } @Override public Student findById(int id) { sql = " select * from student where id = ?"; Student student = null; try { student = queryRunner.query(JDBCTools.getConnection(),sql,new BeanHandler<Student>(Student.class),id); } catch (SQLException e) { e.printStackTrace(); } return student; } @Override public List<Student> findAll() { sql = "select * from student"; List<Student> list = null; try { list = queryRunner.query(JDBCTools.getConnection(),sql,new BeanListHandler<>(Student.class)); } catch (SQLException e) { e.printStackTrace(); } return list; } @Override public int studentCount() { sql = "select count(id) from student"; int count = 0; try { count = queryRunner.query(JDBCTools.getConnection(),sql,new ScalarHandler<Integer>()); } catch (SQLException e) { e.printStackTrace(); } return count; } }
package com.dreambamboo.util; import java.sql.*; import java.util.ResourceBundle; /** * 数据库操作工具类 */ public class JDBCTools { private static String URL; private static String USERNAME; private static String PASSWORD; private static String DRIVER; private static ResourceBundle resourceBundle = ResourceBundle.getBundle("com.dreambamboo.util.db-config"); private JDBCTools(){ } /** * 使用静态代码块加载驱动程序 * 防止重复代码,使用静态代码块在类加载的时候只会执行一次 */ static { URL = resourceBundle.getString("jdbc.url"); USERNAME = resourceBundle.getString("jdbc.username"); PASSWORD = resourceBundle.getString("jdbc.password"); DRIVER = resourceBundle.getString("jdbc.driver"); } /** * 获取数据库连接 * @return */ public static Connection getConnection(){ Connection connection = null; try { connection = DriverManager.getConnection(URL,USERNAME,PASSWORD); } catch (SQLException e) { e.printStackTrace(); System.out.println("获取连接失败"); } return connection; } /** * 关闭数据库连接资源 * @param connection 数据库连接 * @param statement 数据库会话语句 * @param resultSet 数据库查询结果集 */ 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(); } } }
jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=mysql jdbc.driver=com.mysql.jdbc.Driver
package com.dreambamboo.test; import com.dreambamboo.dao.StudentDao; import com.dreambamboo.dao.impl.StudentDaoImpl; import com.dreambamboo.entity.Student; import java.util.List; public class Test { public static void main(String[] args) { StudentDao studentDao = new StudentDaoImpl(); //studentDao.insert(new Student("孙悟空","高三年级","五班")); //studentDao.update(new Student(14,"孙悟空111","高三年级","十一班")); //studentDao.delete(14); List<Student> list = studentDao.findAll(); for (Student st : list) { System.out.println(st.getId() + "===>>>" + st.getName() + "===>>>>" + st.getClazz() + "===>>> "+ st.getGrade()); } } }
标签:close 静态 创建 mysql [] test 数据类型 integer man
原文地址:https://www.cnblogs.com/jackieliutao/p/10180455.html