ResultSet结果集的长度length或size

博客涉及Java后端开发及JDBC相关知识,虽未给出具体内容,但可推测围绕Java在后端的应用以及JDBC的使用等信息技术关键信息展开。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

rs.last();
int len = rs.getRow();
package ui; import sql.ConnectionManager; import javax.swing.*; import javax.swing.GroupLayout.Alignment; import javax.swing.border.TitledBorder; import javax.swing.table.DefaultTableModel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class AccountManagerPanel extends JPanel { private static final long serialVersionUID = 1L; private JPanel top, center, bottom; private JLabel label, lblMess; private JTextField txtAccountId, txtAccountType; private JButton btnAddOk, btnReset; private JLabel lblDel; private JComboBox<String> comboBoxTypeName; private JButton btnDeleteOk; private JTable table; private JScrollPane scrollPane; public AccountManagerPanel() { top = new JPanel(); top.setBorder(new TitledBorder("账号新类型")); label = new JLabel("新增帐号类型:"); txtAccountType = new JTextField(); lblMess = new JLabel("(名字长度不能超过10个汉字)"); txtAccountId = new JTextField(); btnAddOk = new JButton("添加"); btnReset = new JButton("重置"); btnAddOk.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String accountType = txtAccountType.getText().trim(); if (accountType.isEmpty()) { JOptionPane.showMessageDialog(AccountManagerPanel.this, "请输入账号类型名称", "输入错误", JOptionPane.ERROR_MESSAGE); return; } if (accountType.length() > 10) { JOptionPane.showMessageDialog(AccountManagerPanel.this, "账号类型名称长度不能超过10个汉字", "输入错误", JOptionPane.ERROR_MESSAGE); return; } if (isExist(accountType)) { JOptionPane.showMessageDialog(AccountManagerPanel.this, "该账号类型已存在", "错误", JOptionPane.ERROR_MESSAGE); return; } Connection connection = null; PreparedStatement pstmt = null; try { connection = ConnectionManager.getConn(); int newTypeId = generateNewTypeId(connection); String sql = "INSERT INTO MuserType (iTypeId, cTypeName) VALUES (?, ?)"; pstmt = connection.prepareStatement(sql); pstmt.setInt(1, newTypeId); pstmt.setString(2, accountType); pstmt.executeUpdate(); JOptionPane.showMessageDialog(AccountManagerPanel.this, "账号类型添加成功!", "成功", JOptionPane.INFORMATION_MESSAGE); clearData(); loadData(); loadTableData(); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(AccountManagerPanel.this, "添加账号类型失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } finally { try { if (pstmt != null) pstmt.close(); if (connection != null) connection.close(); } catch (SQLException ex) { ex.printStackTrace(); } } } }); btnReset.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { clearData(); } }); this.addComponentListener(new ComponentAdapter() { public void componentShown(ComponentEvent e) { loadData(); generateId(); loadTableData(); } }); GroupLayout group1 = new GroupLayout(top); GroupLayout.SequentialGroup hgroup = group1.createSequentialGroup(); hgroup.addContainerGap(); hgroup.addGroup(group1.createParallelGroup() .addComponent(label)); hgroup.addContainerGap(); hgroup.addGroup(group1.createParallelGroup() .addComponent(txtAccountType, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE) .addComponent(btnAddOk, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)); hgroup.addContainerGap(); hgroup.addGroup(group1.createParallelGroup() .addComponent(lblMess, GroupLayout.PREFERRED_SIZE, 264, GroupLayout.PREFERRED_SIZE) .addComponent(btnReset, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)); group1.setHorizontalGroup(hgroup); GroupLayout.SequentialGroup vgroup = group1.createSequentialGroup(); vgroup.addContainerGap(); vgroup.addGroup(group1.createParallelGroup(Alignment.CENTER) .addComponent(label) .addComponent(txtAccountType, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE) .addComponent(lblMess) ); vgroup.addContainerGap(8, Short.MAX_VALUE); vgroup.addGroup(group1.createParallelGroup() .addComponent(btnAddOk) .addComponent(btnReset) ); vgroup.addContainerGap(); group1.setVerticalGroup(vgroup); top.setLayout(group1); center = new JPanel(); center.setBorder(new TitledBorder("删除帐号类型")); lblDel = new JLabel("选择要删除的账号类型:"); comboBoxTypeName = new JComboBox<>(); loadData(); btnDeleteOk = new JButton("确 定 删 除"); btnDeleteOk.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String selectedType = (String) comboBoxTypeName.getSelectedItem(); if (selectedType == null || selectedType.isEmpty()) { JOptionPane.showMessageDialog(AccountManagerPanel.this, "请选择要删除的账号类型", "错误", JOptionPane.ERROR_MESSAGE); return; } Connection connection = null; PreparedStatement pstmt = null; try { connection = ConnectionManager.getConn(); String sql = "DELETE FROM MuserType WHERE cTypeName = ?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, selectedType); pstmt.executeUpdate(); JOptionPane.showMessageDialog(AccountManagerPanel.this, "账号类型删除成功!", "成功", JOptionPane.INFORMATION_MESSAGE); loadData(); loadTableData(); generateId(); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(AccountManagerPanel.this, "删除账号类型失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } finally { try { if (pstmt != null) pstmt.close(); if (connection != null) connection.close(); } catch (SQLException ex) { ex.printStackTrace(); } } } }); GroupLayout group2 = new GroupLayout(center); GroupLayout.SequentialGroup hgroup2 = group2.createSequentialGroup(); hgroup2.addGroup(group2.createParallelGroup() .addComponent(lblDel, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)); hgroup2.addGroup(group2.createParallelGroup() .addComponent(comboBoxTypeName, GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE)); hgroup2.addContainerGap(35, 95); hgroup2.addGroup(group2.createParallelGroup() .addComponent(btnDeleteOk, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)); group2.setHorizontalGroup(hgroup2); GroupLayout.SequentialGroup vgroup2 = group2.createSequentialGroup(); vgroup2.addGroup(group2.createParallelGroup(Alignment.CENTER) .addComponent(lblDel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE) .addComponent(comboBoxTypeName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(btnDeleteOk)); group2.setVerticalGroup(vgroup2); center.setLayout(group2); bottom = new JPanel(); bottom.setBorder(new TitledBorder("账号类型一览表")); JButton btnSubmit = new JButton("提交修改"); JButton btnGiveUp = new JButton("放弃修改"); DefaultTableModel tableModel = new DefaultTableModel(new Object[]{"账号类型编号", "账号类型名"}, 0); table = new JTable(tableModel); scrollPane = new JScrollPane(table); GroupLayout group3 = new GroupLayout(bottom); GroupLayout.SequentialGroup hgroup3 = group3.createSequentialGroup(); hgroup3.addGroup(group3.createParallelGroup() .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 440, GroupLayout.PREFERRED_SIZE)); hgroup3.addContainerGap(5, 30); hgroup3.addGroup(group3.createParallelGroup() .addComponent(btnSubmit) .addComponent(btnGiveUp)); group3.setHorizontalGroup(hgroup3); GroupLayout.SequentialGroup vgroup3 = group3.createSequentialGroup(); vgroup3.addGroup(group3.createParallelGroup() .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE) .addGroup(group3.createSequentialGroup() .addComponent(btnSubmit) .addContainerGap(3, 10) .addComponent(btnGiveUp))); group3.setVerticalGroup(vgroup3); bottom.setLayout(group3); GroupLayout group = new GroupLayout(this); GroupLayout.SequentialGroup hgroup1 = group.createSequentialGroup(); hgroup1.addContainerGap(15, Short.MAX_VALUE); hgroup1.addGroup(group.createParallelGroup() .addComponent(top, GroupLayout.PREFERRED_SIZE, 600, GroupLayout.PREFERRED_SIZE) .addGap(8) .addComponent(center, GroupLayout.PREFERRED_SIZE, 600, GroupLayout.PREFERRED_SIZE) .addGap(8) .addComponent(bottom, GroupLayout.PREFERRED_SIZE, 600, GroupLayout.PREFERRED_SIZE)); hgroup1.addContainerGap(10, Short.MAX_VALUE); group.setHorizontalGroup(hgroup1); GroupLayout.SequentialGroup vgroup1 = group.createSequentialGroup(); vgroup1.addGroup(group.createParallelGroup() .addComponent(top, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)); vgroup1.addContainerGap(10, Short.MAX_VALUE); vgroup1.addGroup(group.createParallelGroup() .addComponent(center, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)); vgroup1.addContainerGap(10, Short.MAX_VALUE); vgroup1.addGroup(group.createParallelGroup() .addComponent(bottom, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)); group.setVerticalGroup(vgroup1); this.setLayout(group); } public void clearData() { generateId(); txtAccountType.setText(""); } void loadData() { comboBoxTypeName.removeAllItems(); String sql = "SELECT cTypeName FROM MuserType"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = ConnectionManager.getConn(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { comboBoxTypeName.addItem(rs.getString(1)); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void loadTableData() { DefaultTableModel model = (DefaultTableModel) table.getModel(); model.setRowCount(0); String sql = "SELECT iTypeId, cTypeName FROM MuserType"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = ConnectionManager.getConn(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { int typeId = rs.getInt("iTypeId"); String typeName = rs.getString("cTypeName"); model.addRow(new Object[]{typeId, typeName}); } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "加载账号类型失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void generateId() { String sql = "SELECT MAX(iTypeId) FROM MuserType"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = ConnectionManager.getConn(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); int id = 0; if (rs.next()) { id = rs.getInt(1); } id++; this.txtAccountId.setText(String.valueOf(id)); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public boolean isExist(String name) { String sql = "SELECT cTypeName FROM MuserType WHERE cTypeName = ?"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = ConnectionManager.getConn(); pstmt = conn.prepareStatement(sql); pstmt.setString(1, name); rs = pstmt.executeQuery(); if (rs.next()) { return true; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return false; } private int generateNewTypeId(Connection connection) throws SQLException { String sql = "SELECT MAX(iTypeId) FROM MuserType"; PreparedStatement pstmt = connection.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); int newTypeId = 1; if (rs.next()) { Integer maxTypeId = rs.getInt(1); if (maxTypeId != null) { newTypeId = maxTypeId + 1; } } return newTypeId; } }换一种方法实现
最新发布
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值