JDBC 资源绑定器 ,处理查询结果集,结果集
分享于 点击 9316 次 点评:89
JDBC 资源绑定器 ,处理查询结果集,结果集
使用资源绑定器绑定属性配置 实际开发中不建议把连接数据库的信息写死到Java程序中//使用资源绑定器绑定属性配置 ResourceBundle bundle = ResourceBundle.getBundle("jdbc"); String driver = bundle.getString("driver"); String url = bundle.getString("url"); String user = bundle.getString("user"); String password = bundle.getString("password");
处理查询结果集 返回类型 方法 int executeUpdate (insert/delete/update)
ResultSet executeQUery (select)DQL 结果集ResultSet
- ResultSet executeQuery(String sql) throws SQLException 执行给定的SQL语句,返回单个ResultSet对象。
//执行sql String sql = "select empno,ename,sal from emp"; rs = stmt.executeQuery(sql);//专门执行DQL的方法 //处理查询结果集 while(rs.next()) {//光标指向的行有数据 //取数据 //JDBC中所有下标从1开始,不是从0开始 //健壮的写法,以字段名(重命名则用改名后的)获取 int empno = rs.getInt("empno"); String ename = rs.getString("ename"); Double sal = rs.getDouble("sal"); System.out.println(empno+","+ename+","+sal); }
相关文章
- 暂无相关文章
用户点评