java数据库编程之查询案例

        JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。 JDBC并不能直接访问数据库,需要借助于数据库厂商提供的JDBC驱动程序。JDBC中常用的类和接口可用于我们编程开发,利用这些类和接口可以方便的经行数据访问和处理。这些类和接口都位于java.sql包中。

        具体案例如下:

1、定义数据库连接属性:DBConf.java

public class DBConf {
    public static  String DRIVER = "com.mysql.jdbc.Driver"; 
    public static  String URL = "jdbc:mysql://IP:3306/数据库名?useUnicode=true&characterEncoding=UTF-8";
    public static  String DB_NAME = "root";  
    public static  String DB_PASSWORD = "root";
}

2、定义数据操作工具类:DBUtil.java

public class DBUtil {    
    public static final String name = DBConf.DRIVER; 
    public static final String url = DBConf.URL;
    public static final String user = DBConf.DB_NAME;  
    public static final String password = DBConf.DB_PASSWORD; 
    public Connection conn = null;  
    public PreparedStatement pst = null;  
  
    public DBUtil(String sql) {  
        try {  
            Class.forName(name);//指定连接类型  
            conn = DriverManager.getConnection(url, user, password);//获取连接  
            pst = conn.prepareStatement(sql);//准备执行语句  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    public void close() {
        try {  
            this.pst.close();  //关闭执行命令
            this.conn.close();  //关闭连接
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }  
}

3、定义业务查询:PlanDao.java

public class PlanDao{

    static String sql = null;
    public DBUtil db1 = null;
    private Statement stmt = null;
    static ResultSet ret = null;
    private String[] flags;

public  List<FusePlanCaseEntity>  selectPlanCaseInfoByPlanId(int planId,String sSearch) throws SQLException {
        sql = "select * from  tfci_fuseplan_case where  planId=?";
        db1 = new DBUtil(sql);
        FusePlanCaseEntity entity = null;
        List<FusePlanCaseEntity> list = new ArrayList<FusePlanCaseEntity>();
        db1.pst.setInt(1, planId);//1就是sql的第一个参数的意思,pst.setInt(1,planId); 就是把planId替代sql的第一个问号
        ret = db1.pst.executeQuery();// 执行语句,得到结果集
        while (ret.next()) { //遍历结果
              entity = new FusePlanCaseEntity();
              entity.setId(ret.getInt(1));
              entity.setPlanId(ret.getInt(2));
              entity.setProjectName(ret.getString(3));
              entity.setPlanCaseDec(ret.getString(4));
              entity.setSfSendMail(ret.getString(5));
              entity.setCreater(ret.getString(6));
              entity.setPlanCase(ret.getString(7));
              list.add(entity);
          }//显示数据          
        ret.close();
        db1.close();// 关闭连接
        return list;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值