dbutils操作sqlserver数据库



dbutils操作sqlserver数据库

1、引入两个包

dbutils包:commons-dbutils-1.7.jar  http://commons.apache.org/proper/commons-dbutils/download_dbutils.cgi

sqlserver驱动包:sqljdbc42.jar

2、写dbutils的工具类:

 

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. public class DbUtils {
  7. // 使用静态块加载驱动程序
  8. static {
  9. try {
  10. Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
  11. } catch (ClassNotFoundException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. // 定义一个获取数据库连接的方法
  16. public static Connection getConnection() {
  17. Connection conn = null;
  18. try {
  19. conn = DriverManager.getConnection(“jdbc:sqlserver://58.214.246.30:11433;DatabaseName=IntelGrain_DATA_2016_cx”,
  20. “sa”, “zhiyuan@111″);
  21. } catch (SQLException e) {
  22. e.printStackTrace();
  23. System.out.println(“获取连接失败”);
  24. }
  25. return conn;
  26. }
  27. // 关闭数据库连接
  28. public static void close(ResultSet rs, Statement stat, Connection conn) {
  29. try {
  30. if (rs != null)
  31. rs.close();
  32. if (stat != null)
  33. stat.close();
  34. if (conn != null)
  35. conn.close();
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }

3、进行增删改操作

 

  1. package com.vae.dao;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4. import org.apache.commons.dbutils.QueryRunner;
  5. import org.apache.commons.dbutils.handlers.BeanHandler;
  6. import org.apache.commons.dbutils.handlers.BeanListHandler;
  7. import org.apache.commons.dbutils.handlers.ScalarHandler;
  8. import com.util.db.DBUtils;
  9. import com.vae.domain.Person;
  10. public class PersonDaoImpl implements PersonDao {
  11. private QueryRunner runner = null;//查询运行器
  12. public PersonDaoImpl(){
  13. runner = new QueryRunner();
  14. }
  15. //方法:向数据库中添加一条记录
  16. @Override
  17. public void add(Person p) throws SQLException {
  18. String sql = “insert into person(name,age,description)values(?,?,?)”;
  19. runner.update(DBUtils.getConnection(), sql, p.getName(), p.getAge(),p.getDescription());
  20. }
  21. //方法:根据id向数据库中修改某条记录
  22. @Override
  23. public void update(Person p) throws SQLException {
  24. String sql = “update person set name=?,age=?,description=? where id=?”;
  25. runner.update(DBUtils.getConnection(), sql, p.getName(),p.getAge(),p.getDescription(),p.getId());
  26. }
  27. //方法:根据id删除数据库中的某条记录
  28. @Override
  29. public void delete(int id) throws SQLException {
  30. String sql = “delete from person where id=?”;
  31. runner.update(DBUtils.getConnection(), sql, id);
  32. }
  33. //方法:使用BeanHandler查询一个对象
  34. @Override
  35. public Person findById(int id) throws SQLException {
  36. String sql = “select name,age,description from person where id=?”;
  37. Person p = runner.query(DBUtils.getConnection(), sql, new BeanHandler<Person>(Person.class),id);
  38. return p;
  39. }
  40. //方法:使用BeanListHandler查询所有对象
  41. @Override
  42. public List<Person> findAll() throws SQLException {
  43. String sql = “select name,age,description from person”;
  44. List<Person> persons = runner.query(DBUtils.getConnection(), sql, new BeanListHandler<Person>(Person.class));
  45. return persons;
  46. }
  47. //方法:使用ScalarHandler查询一共有几条记录
  48. @Override
  49. public long personCount()throws SQLException{
  50. String sql = “select count(id) from person”;
  51. return runner.query(DBUtils.getConnection(),sql, new ScalarHandler<Long>());
  52. }
  53. }
  54.