ResultSet 返回ArrayList

public   java.util.ArrayList   executeQuery(String   sqlQueryStmt)   {  
          System.out.println("SQL   String   is   "   +   sqlQueryStmt);  
          java.util.ArrayList   rows   =   new   java.util.ArrayList();  
          java.sql.Connection   conn   =   null;  
          java.sql.Statement   stmt   =   null;  
          java.sql.ResultSet   rs   =   null;  
          try   {  
              conn   =   this.getConnection(1000);  
              stmt   =   conn.createStatement();  
              rs   =   stmt.executeQuery(sqlQueryStmt);  
              ResultSetMetaData   rsmd   =   rs.getMetaData();  
              int   columnCount   =   rsmd.getColumnCount();  
              while   (rs.next())   {  
                  HashMap   row   =   new   HashMap();  
                  for   (int   i   =   1;   i   <=   columnCount;   i++)   {  
                      String   cname   =   rsmd.getColumnName(i);  
                      row.put(cname,   rs.getObject(i));  
                  }  
                  rows.add(row);  
              }  
              return   rows;  
          }  
          catch   (Exception   e)   {  
              System.out.println("查询数据库出错,SQL语句为:"   +   sqlQueryStmt   +   "/n错误信息为:"   +  
                                                    e.getMessage());  
              return   null;  
          }  
          finally   {  
              try   {  
                  rs.close();  
                  stmt.close();  
                  //关闭连接,返回连接  
                  this.freeConnection(conn);  
                  conn   =   null;  
              }  
              catch   (Exception   e)   {  
                  System.out.println("释放连接出错,错误信息为:"   +   e.getMessage());  
                  //     return   rows;  
              }  
          }  
      }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.sql.*; import java.util.*; /** * * Title: 数据库工具类 * * * Description: 将大部分的数据库操作放入这个类中, 包括数据库连接的建立, 自动释放等. * * * @author beansoft 日期: 2004年04月 * @version 2.0 */ public class DatabaseUtil { /** 数据库连接 */ private java.sql.Connection connection; /** * All database resources created by this class, should be free after all * operations, holds: ResultSet, Statement, PreparedStatement, etc. */ private ArrayList resourcesList = new ArrayList(5); public DatabaseUtil() { } /** 关闭数据库连接并释放所有数据库资源 */ public void close() { closeAllResources(); close(getConnection()); } /** * Close given connection. * * @param connection * Connection */ public static void close(Connection connection) { try { connection.close(); } catch (Exception ex) { System.err.println("Exception when close a connection: " + ex.getMessage()); } } /** * Close all resources created by this class. */ public void closeAllResources() { for (int i = 0; i < this.getResourcesList().size(); i++) { closeJDBCResource(getResourcesList().get(i)); } } /** * Close a jdbc resource, such as ResultSet, Statement, Connection.... All * these objects must have a method signature is void close(). * * @param resource - * jdbc resouce to close */ public void closeJDBCResource(Object resource) { try { Class clazz = resource.getClass(); java.lang.reflect.Method method = clazz.getMethod("close", null); method.invoke(resource, null); } catch (Exception e) { // e.printStackTrace(); } } /** * 执行 SELECT 等 SQL 语句并返回结果集. * * @param sql * 需要发送到数据库 SQL 语句 * @return a ResultSet object that contains the data produced * by the given query; never null */ public ResultSet executeQuery(String sql) { try { Statement statement = getStatement(); ResultSet rs = statement.executeQuery(sql); this.getResourcesList().add(rs); this.getResourcesList().add(statement);// BUG fix at 2006-04-29 by BeanSoft, added this to res list // MySql 数据库要求必需关闭 statement 对象, 否则释放不掉资源 // - 此观点错误, 因为关闭此对象后有时数据无法读出 //statement.close(); return rs; } catch (Exception ex) { System.out.println("Error in executeQuery(\"" + sql + "\"):" + ex); // ex.printStackTrace(); return null; } } /** * Executes the given SQL statement, which may be an INSERT, * UPDATE, or DELETE statement or an SQL * statement that returns nothing, such as an SQL DDL statement. 执行给定的 SQL * 语句, 这些语句可能是 INSERT, UPDATE 或者 DELETE 语句, 或者是一个不返回任何东西的 SQL 语句, 例如一个 SQL * DDL 语句. * * @param sql * an SQL INSERT,UPDATE or * DELETE statement or an SQL statement that * returns nothing * @return either the row count for INSERT, * UPDATE or DELETE statements, or * 0 for SQL statements that return nothing */ public int executeUpdate(String sql) { try { Statement statement = getStatement(); return statement.executeUpdate(sql); // MySql 数据库要求必需关闭 statement 对象, 否则释放不掉资源 // - 此观点错误, 因为关闭此对象后有时数据无法读出 //statement.close(); } catch (Exception ex) { System.out.println("Error in executeUpdate(): " + sql + " " + ex); //System.out.println("executeUpdate:" + sql); ex.printStackTrace(); } return -1; } /** * 返回记录总数, 使用方法: getAllCount("SELECT count(ID) from tableName") 2004-06-09 * 可滚动的 Statement 不能执行 SELECT MAX(ID) 之类的查询语句(SQLServer 2000) * * @param sql * 需要执行的 SQL * @return 记录总数 */ public int getAllCount(String sql) { try { Statement statement = getConnection().createStatement(); this.getResourcesList().add(statement); ResultSet rs = statement.executeQuery(sql); rs.next(); int cnt = rs.getInt(1); rs.close(); try { statement.close(); this.getResourcesList().remove(statement); } catch (Exception ex) { ex.printStackTrace(); } return cnt; } catch (Exception ex) { System.out.println("Exception in DatabaseUtil.getAllCount(" + sql + "):" + ex); ex.printStackTrace(); return 0; } } /** * 返回当前数据库连接. */ public java.sql.Connection getConnection() { return connection; } /** * 连接新的数据库对象到这个工具类, 首先尝试关闭老连接. */ public void setConnection(java.sql.Connection connection) { if (this.connection != null) { try { getConnection().close(); } catch (Exception ex) { } } this.connection = connection; } /** * Create a common statement from the database connection and return it. * * @return Statement */ public Statement getStatement() { // 首先尝试获取可滚动的 Statement, 然后才是普通 Statement Statement updatableStmt = getUpdatableStatement(); if (updatableStmt != null) return updatableStmt; try { Statement statement = getConnection().createStatement(); this.getResourcesList().add(statement); return statement; } catch (Exception ex) { System.out.println("Error in getStatement(): " + ex); } return null; } /** * Create a updatable and scrollable statement from the database connection * and return it. * * @return Statement */ public Statement getUpdatableStatement() { try { Statement statement = getConnection() .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); this.getResourcesList().add(statement); return statement; } catch (Exception ex) { System.out.println("Error in getUpdatableStatement(): " + ex); } return null; } /** * Create a prepared statement and return it. * * @param sql * String SQL to prepare * @throws SQLException * any database exception * @return PreparedStatement the prepared statement */ public PreparedStatement getPreparedStatement(String sql) throws SQLException { try { PreparedStatement preparedStatement = getConnection() .prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); this.getResourcesList().add(preparedStatement); return preparedStatement; } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * Return the resources list of this class. * * @return ArrayList the resources list */ public ArrayList getResourcesList() { return resourcesList; } /** * Fetch a string from the result set, and avoid return a null string. * * @param rs * the ResultSet * @param columnName * the column name * @return the fetched string */ public static String getString(ResultSet rs, String columnName) { try { String result = rs.getString(columnName); if (result == null) { result = ""; } return result; } catch (Exception ex) { } return ""; } /** * Get all the column labels * * @param resultSet * ResultSet * @return String[] */ public static String[] getColumns(ResultSet resultSet) { if (resultSet == null) { return null; } try { ResultSetMetaData metaData = resultSet.getMetaData(); int numberOfColumns = metaData.getColumnCount(); if (numberOfColumns <= 0) { return null; } String[] columns = new String[numberOfColumns]; //System.err.println("numberOfColumns=" + numberOfColumns); // Get the column names for (int column = 0; column < numberOfColumns; column++) { // System.out.print(metaData.getColumnLabel(column + 1) + "\t"); columns[column] = metaData.getColumnName(column + 1); } return columns; } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * Get the row count of the result set. * * @param resultset * ResultSet * @throws SQLException * if a database access error occurs or the result set type is * TYPE_FORWARD_ONLY * @return int the row count * @since 1.2 */ public static int getRowCount(ResultSet resultset) throws SQLException { int row = 0; try { int currentRow = resultset.getRow(); // Remember old row position resultset.last(); row = resultset.getRow(); if (currentRow > 0) { resultset.absolute(row); } } catch (Exception ex) { ex.printStackTrace(); } return row; } /** * Get the column count of the result set. * * @param resultSet * ResultSet * @return int the column count */ public static int getColumnCount(ResultSet resultSet) { if (resultSet == null) { return 0; } try { ResultSetMetaData metaData = resultSet.getMetaData(); int numberOfColumns = metaData.getColumnCount(); return numberOfColumns; } catch (Exception ex) { ex.printStackTrace(); } return 0; } /** * Read one row's data from result set automatically and put the result it a * hashtable. Stored as "columnName" = "value", where value is converted to * String. * * @param resultSet * ResultSet * @return Hashtable */ public static final Hashtable readResultToHashtable(ResultSet resultSet) { if (resultSet == null) { return null; } Hashtable resultHash = new Hashtable(); try { String[] columns = getColumns(resultSet); if (columns != null) { // Read data column by column for (int i = 0; i < columns.length; i++) { resultHash.put(columns[i], getString(resultSet, columns[i])); } } } catch (Exception ex) { ex.printStackTrace(); } return resultHash; } /** * Read data from result set automatically and put the result it a * hashtable. Stored as "columnName" = "value", where value is converted to * String. * * Note: assume the default database string encoding is ISO8859-1. * * @param resultSet * ResultSet * @return Hashtable */ @SuppressWarnings("unchecked") public static final Hashtable readResultToHashtableISO(ResultSet resultSet) { if (resultSet == null) { return null; } Hashtable resultHash = new Hashtable(); try { String[] columns = getColumns(resultSet); if (columns != null) { // Read data column by column for (int i = 0; i < columns.length; i++) { String isoString = getString(resultSet, columns[i]); try { resultHash.put(columns[i], new String(isoString .getBytes("ISO8859-1"), "GBK")); } catch (Exception ex) { resultHash.put(columns[i], isoString); } } } } catch (Exception ex) { ex.printStackTrace(); } return resultHash; } /** Test this class. */ public static void main(String[] args) throws Exception { DatabaseUtil util = new DatabaseUtil(); // TODO: 从连接池工厂获取连接 // util.setConnection(ConnectionFactory.getConnection()); ResultSet rs = util.executeQuery("SELECT * FROM e_hyx_trans_info"); while (rs.next()) { Hashtable hash = readResultToHashtableISO(rs); Enumeration keys = hash.keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); System.out.println(key + "=" + hash.get(key)); } } rs.close(); util.close(); } }
货架仓库管理package real.action.dao; import java.io.PrintStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.swing.ComboBoxModel; import real.action.data.SupplierData; import real.action.sql.ConnectionFactory; public class SupplierDao { private Connection conn; private Statement statement; private PreparedStatement preparedStatement;//SQL 语句被预编译并存储在 PreparedStatement 对象中。然后可以使用此对象多次高效地执行该语句。 private ResultSet resultSet;//结果集 public boolean addSupplier(SupplierData s)//添加供应商 { String sql = "insert into suppliers values(?,?,?,?,?,?,?,?)"; try { this.conn = ConnectionFactory.getConnection(); this.preparedStatement = this.conn.prepareStatement(sql); this.preparedStatement.setInt(1, s.getSupplierId()); this.preparedStatement.setString(2, s.getSupplierName()); this.preparedStatement.setString(3, s.getSupplierAddress()); this.preparedStatement.setString(4, s.getPostCode()); this.preparedStatement.setString(5, s.getSupplierTelephone()); this.preparedStatement.setString(6, s.getSupplierFax()); this.preparedStatement.setString(7, s.getSupplierRelationer()); this.preparedStatement.setString(8, s.getSupplierEmail()); int flag = this.preparedStatement.executeUpdate(); if (flag > 0) { return true; } } catch (Exception e) { e.printStackTrace(); } return false; } public boolean isExistSupplierData() { boolean flag = true; int row = 0; String sql = "select count(sup_id) as count from suppliers"; try { this.conn = ConnectionFactory.getConnection(); this.statement = this.conn.createStatement(); this.resultSet = this.statement.executeQuery(sql); if (this.resultSet.next()) { row = this.resultSet.getInt("count"); } if (row == 0) { return false; } } catch (Exception e) { e.printStackTrace(); } return flag; } public boolean isExistSupplierById(int number)//ID已存在 { boolean flag = false; String sql = "select sup_id from suppliers"; try { this.conn = ConnectionFactory.getConnection(); this.statement = this.conn.createStatement(); this.resultSet = this.statement.executeQuery(sql); while (this.resultSet.next()) { SupplierData supplierData = new SupplierData(); supplierData.setSupplierId(this.resultSet.getInt(1));//设置ID if (supplierData.getSupplierId() == number) {//比较ID flag = true; break; } } } catch (Exception e) { e.printStackTrace(); } return flag; } public List<Object> getFindAllSupplierId()//获取所有供应商id { List listsupplierId = new ArrayList();//滚动列表 String sql = "select sup_id from suppliers "; try { this.conn = ConnectionFactory.getConnection(); this.statement = this.conn.createStatement(); this.resultSet = this.statement.executeQuery(sql); while (this.resultSet.next()) listsupplierId.add(Integer.valueOf(this.resultSet.getInt(1))); } catch (Exception e) { e.printStackTrace(); } return listsupplierId; } public SupplierData getSupplierbySupplierName(String name) { SupplierData supplierData = null; String sql = "select * from suppliers where sup_name = ?"; try { this.conn = ConnectionFactory.getConnection(); this.preparedStatement = this.conn.prepareStatement(sql); this.preparedStatement.setString(1, name); this.resultSet = this.preparedStatement.executeQuery(); if (this.resultSet.next()) { supplierData = new SupplierData(); supplierData.setSupplierId(this.resultSet.getInt(1)); supplierData.setSupplierName(this.resultSet.getString(2)); supplierData.setSupplierAddress(this.resultSet.getString(3)); supplierData.setPostCode(this.resultSet.getString(4)); supplierData.setSupplierTelephone(this.resultSet.getString(5)); supplierData.setSupplierFax(this.resultSet.getString(6)); supplierData.setSupplierRelationer(this.resultSet.getString(7)); supplierData.setSupplierEmail(this.resultSet.getString(8)); } } catch (Exception e) { e.printStackTrace(); } return supplierData; } public Map<Integer, String> getAllSupplier() { Map supplierData = new HashMap();//基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作 String sql = "select sup_id,sup_name,sup_address,postcode,sup_telephone,sup_fax,sup_relationer,sup_email from suppliers"; try { conn = ConnectionFactory.getConnection(); preparedStatement = conn.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while (resultSet.next())// 在此映射中关联指定值与指定键。使供应商ID与名称关联 supplierData.put(Integer.valueOf(resultSet.getInt("sup_id")), resultSet.getString("sup_name")); } catch (Exception e) { e.printStackTrace(); } return supplierData;//返回映射关系 } public SupplierData getSupplierbySupplierId(String number) { SupplierData supplierData = null; String sql = "select sup_id,sup_name,sup_address,postcode,sup_telephone,sup_fax,sup_relationer,sup_email from suppliers where sup_id = ?"; try { this.conn = ConnectionFactory.getConnection(); this.preparedStatement = this.conn.prepareStatement(sql); this.preparedStatement.setString(1, number); this.resultSet = this.preparedStatement.executeQuery(); if (this.resultSet.next()) { supplierData = new SupplierData(); supplierData.setSupplierId(this.resultSet.getInt(1)); supplierData.setSupplierName(this.resultSet.getString(2)); supplierData.setSupplierAddress(this.resultSet.getString(3)); supplierData.setPostCode(this.resultSet.getString(4)); supplierData.setSupplierTelephone(this.resultSet.getString(5)); supplierData.setSupplierFax(this.resultSet.getString(6)); supplierData.setSupplierRelationer(this.resultSet.getString(7)); supplierData.setSupplierEmail(this.resultSet.getString(8)); } } catch (Exception e) { e.printStackTrace(); } return supplierData; } public List<Object> getFindSupplier() { List lstSupplierData = new ArrayList(); String sql = "select sup_id,sup_name,sup_address,postcode,sup_telephone,sup_fax,sup_relationer,sup_email from suppliers"; try { this.conn = ConnectionFactory.getConnection(); this.statement = this.conn.createStatement(); this.resultSet = this.statement.executeQuery(sql); while (this.resultSet.next()) { lstSupplierData.add(Integer.valueOf(this.resultSet.getInt(1))); lstSupplierData.add(this.resultSet.getString(2)); lstSupplierData.add(this.resultSet.getString(3)); lstSupplierData.add(this.resultSet.getString(4)); lstSupplierData.add(this.resultSet.getString(5)); lstSupplierData.add(this.resultSet.getString(6)); lstSupplierData.add(this.resultSet.getString(7)); lstSupplierData.add(this.resultSet.getString(8)); } } catch (Exception e) { e.printStackTrace(); } return lstSupplierData; } public boolean updateSupplierById(SupplierData s)//按ID更新供应商 { boolean flag = false; String sql = "update suppliers set sup_name = ?,sup_address = ? ,postcode = ? ,sup_telephone = ? ,sup_fax = ? , sup_relationer = ? , sup_email = ? where sup_id = ?"; try { this.conn = ConnectionFactory.getConnection(); this.preparedStatement = this.conn.prepareStatement(sql); this.preparedStatement.setInt(8, s.getSupplierId()); this.preparedStatement.setString(1, s.getSupplierName()); this.preparedStatement.setString(2, s.getSupplierAddress()); this.preparedStatement.setString(3, s.getPostCode()); this.preparedStatement.setString(4, s.getSupplierTelephone()); this.preparedStatement.setString(5, s.getSupplierFax()); this.preparedStatement.setString(6, s.getSupplierRelationer()); this.preparedStatement.setString(7, s.getSupplierEmail()); int insertReow = this.preparedStatement.executeUpdate(); if (insertReow > 0) flag = true; } catch (Exception e) { e.printStackTrace(); } return flag; } public boolean deleteSupplierById(int number) { boolean flag = false; String sql = "delete from suppliers where sup_id=?"; try { this.conn = ConnectionFactory.getConnection(); this.preparedStatement = this.conn.prepareStatement(sql); this.preparedStatement.setInt(1, number); int insertReow = this.preparedStatement.executeUpdate(); if (insertReow > 0) { flag = true; } } catch (Exception e) { e.printStackTrace(); } return flag; } public List<Object> getSupplier()//获取所有供应商 { List lstSuppplierData = new ArrayList(); String sql = "select sup_id,sup_name,sup_address,postcode,sup_telephone,sup_fax,sup_relationer,sup_email from suppliers"; try { this.conn = ConnectionFactory.getConnection(); this.statement = this.conn.createStatement(); this.resultSet = this.statement.executeQuery(sql); while (this.resultSet.next()) { lstSuppplierData.add(Integer.valueOf(this.resultSet.getInt(1))); lstSuppplierData.add(this.resultSet.getString(2)); lstSuppplierData.add(this.resultSet.getString(3)); lstSuppplierData.add(this.resultSet.getString(4)); lstSuppplierData.add(this.resultSet.getString(5)); lstSuppplierData.add(this.resultSet.getString(6)); lstSuppplierData.add(this.resultSet.getString(7)); lstSuppplierData.add(this.resultSet.getString(8)); } } catch (Exception e) { e.printStackTrace(); } return lstSuppplierData; } public List<Object> getFoundSuppliers(String string)//获取指定ID供应商 { List lstSupplierData = new ArrayList(); try { this.conn = ConnectionFactory.getConnection(); this.statement = this.conn.createStatement(); this.resultSet = this.statement.executeQuery(string); while (this.resultSet.next()) { lstSupplierData.add(Integer.valueOf(this.resultSet.getInt(1))); lstSupplierData.add(this.resultSet.getString(2)); lstSupplierData.add(this.resultSet.getString(3)); lstSupplierData.add(this.resultSet.getString(4)); lstSupplierData.add(this.resultSet.getString(5)); lstSupplierData.add(this.resultSet.getString(6)); lstSupplierData.add(this.resultSet.getString(7)); lstSupplierData.add(this.resultSet.getString(8)); } } catch (Exception e) { e.printStackTrace(); } return lstSupplierData; } public ComboBoxModel getProductId() { // TODO Auto-generated method stub return null; } }
JAVA银行管理系统设计全文共8页,当前为第1页。JAVA银行管理系统设计全文共8页,当前为第1页。JAVA银行管理系统设计 JAVA银行管理系统设计全文共8页,当前为第1页。 JAVA银行管理系统设计全文共8页,当前为第1页。   综合实践项目 银行管理系统设计   第一部分 案例描述   案例目的    学习使用JAVA+JDBC对数据库的数据进行增加、修改、删除、查询等操作;学习JDBC调用存储过程;学习主要数据库操作对象Connection、Statement、PreparedStatement、ResultSet的使用。  案例难度     案例覆盖技能点    使用JDBC操作数据库、常用数据库操作对象的使用、ArrayList的使用、加深对集合框架的理解、异常的处理、scanner的使用  推荐案例完成时间  1天    适用课程和对象    JAVA面向对象编程基础   第二部分 需求和开发环境   使用技术和开发环境    JAVA、Eclipse或以上、或以上   案例需求   银行存取款系统软件是一款通用性极强的银行存取款管理系统,软件功能囊括了银行从用户开户到最终销户等业务的全过程。软件适用于各级各类的银行。软件覆盖银行业JAVA银行管理系统设计全文共8页,当前为第2页。JAVA银行管理系统设计全文共8页,当前为第2页。的现金办理与金融账业务,软件的各个模块操作界面简单、实用,软件帮助系统让用户可以在最短的时间内掌握软件的使用方法,帮助用户生意早日更上一层楼。软件系统采用银行软件业务能用的控制台操作界面,操作简单易学。   于性能要求,软件采用SQL Server作为持久化存储设备。   系统基本模块包括: 功能点 开户 存款 取款 账 查询 修改密码 销户   难度    功能点介绍   1、开户   开户时需要储户输入个人信息,包括姓名,开户金额,完成后储户信息被保存到一个储户基本信息文件中,并反馈给储户开户后的账号,初始密码,开户金额,开户日期。同时生成一个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,旧密码,新密码。   2、存款   存款时储户提供事先获得的账号及存储的金额,将金额加到账户原有金额中,然后返回本次操作的信息包括存储的金额,账号中现有金额,操作时间。同时生成一个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,旧密码,新密码。 JAVA银行管理系统设计全文共8页,当前为第3页。JAVA银行管理系统设计全文共8页,当前为第3页。  3、取款   取款时储户提供事先获得的账号及要提取的金额,将账号中原有的金额减去要提取的金额,将现金交给储户,然后返回本次操作的信息包括提取的金额,账号中现有的金额,操作时间。同时生成一个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,旧密码,新密码   4、账   账时储户提供事先获得出账号,出账号密码,入账号,账金额,将出账号中的金额减去账金额,入账号中的金额加上账金额,然后返回本次操作的信息包括账的金额。   出账号现有的金额,本次操作的时间。同时生成一个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,旧密码,新密码。   5、查询   查询时储户提供账号,密码。返回查询信息包括账户的的信息和该账户的历史操作。   6、修改密码   修改密码时储户提供账号及密码,根据提示输入新密码两次,完成后密码修改成功。同时生成一个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,旧密码,新密码。 JAVA银行管理系统设计全文共8页,当前为第4页。JAVA银行管理系统设计全文共8页,当前为第4页。  7、销户   销户时储户提供账号及密码,将账号中的金额全部取出,然后将本账号的信息从文件中删除,返回销户成功。   同时生成2个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,旧密码,新密码。   要求,按照如下结构创建包:   …………   所有参考界面如下所示:   主菜单 开户 存款 取款 账 查询 修改密码   销户   第三部分 考核评价点   序号 1 2 3 4 5 6 7   开户 存款 取款 账 查询 修改密码 销户  功能列表 功能描述 分数  必做 说明 必做 必做 必做 选做 必做 选做   综合实践项目 银行管理系统设计   第一部分 案例描述   案例目的    学习使用JAVA+JDBC对数据库的数据进行增加、修改、删除、查询等操作;学习JDBC调用存储过程;学习主要数据库操作对象Connection、Statement、PreparedStatement、ResultSet的使用。  案例难度     案例覆JAVA银行管理系统设计全文共8页,
ResultSet 换为 JSON 可以通过以下步骤完成: 1. 首先,创建一个包含 ResultSet 数据的 ArrayList。 2. 将 ResultSet 中的每一行数据换为一个 HashMap 对象。 3. 将每个 HashMap 对象添加到 ArrayList 中。 4. 使用 JSON 库将 ArrayList 换为 JSON 字符串。 以下是一个示例代码: ```java import org.json.JSONArray; import org.json.JSONObject; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class ResultSetToJson { public static JSONArray convert(ResultSet resultSet) throws Exception { JSONArray jsonArray = new JSONArray(); ResultSetMetaData metaData = resultSet.getMetaData(); int columns = metaData.getColumnCount(); while (resultSet.next()) { Map<String, Object> map = new HashMap<String, Object>(); for (int i = 1; i <= columns; i++) { String columnName = metaData.getColumnName(i); Object columnValue = resultSet.getObject(columnName); map.put(columnName, columnValue); } jsonArray.put(map); } return jsonArray; } public static void main(String[] args) throws Exception { // TODO: Replace with your SQL query and connection details ResultSet resultSet = MyDbConnection.executeQuery("SELECT * FROM my_table"); JSONArray jsonArray = ResultSetToJson.convert(resultSet); String jsonString = jsonArray.toString(); System.out.println(jsonString); } } ``` 其中,MyDbConnection.executeQuery() 是自定义方法,用于执行 SQL 查询并返回 ResultSet 对象。你需要根据自己的数据库连接方式进行修改。 上述代码中,我们使用了 JSON 库中的 JSONArray 和 JSONObject 类来创建 JSON 数据。在循环中,我们将每一行 ResultSet 换为一个 HashMap 对象,并将其添加到 JSON 数组中。最后,我们将 JSON 数组换为字符串并打印出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值