[英]Getting name of JComponent and assigning as parameter in PreparedStatement
Okay, so this is what I have so far and it works. 好的,这就是我到目前为止所能完成的工作。 However, I'm not able to specify to which ?
但是,我无法指定哪个?
(or parameter
) the int 1
or 0
( boolean
converted to int
) is set (through setInt()
). (或parameter
)设置int 1
或0
(将boolean
转换为int
)(通过setInt()
)。 I tried using the getName()
to see the component variable name
but it returns null
when I print it. 我尝试使用getName()
查看组件variable name
但在我打印它时返回null
。
How can I assign a checkbox's value as parameter to my PreparedStatement if I'm not able to specify correctly on which textbox the value of 1 or 0 is from? 如果无法正确指定值1或0来自哪个文本框,如何将复选框的值作为参数分配给PreparedStatement?
ps.setInt(1,valueOfUnknownCheckbox);
//need to put the correct checkbox as 2nd argument.
I saw some information about using Java Reflection
which I'm totally unfamiliar with. 我看到了一些我完全不熟悉的有关使用Java Reflection
信息。 Is there any way without using Java Reflection
? 有没有不用Java Reflection
吗?
Some of the checkbox values are not going to the correct database table's column. 一些复选框的值不会进入正确的数据库表的列。
I hope you can help with me guys. 我希望你能帮助我。 I wanted to be able to use List
to collect components from containers to lessen the lines of codes. 我希望能够使用List
从容器中收集组件,以减少代码行。
Here's my code. 这是我的代码。
private void saveAdminPermissions(){
List<Component> adminPermissionsChbxs = fm.getComponentsAsList(administrationPermissionsCheckBoxPanel);
Boolean bool = null;
String updateSQL =
"UPDATE allusers_admin_permissions SET CURC_BTN=?, DISCOUNTS_BTN=?, SECTIONS_BTN=?,"
+ " USERS_BTN=?, SCHEDULING_BTN=?, YRLEVELS_BTN=?, ACCTG_BTN=? WHERE USERID=? ";
try(Connection con = DBUtil.getConnection(DBType.MYSQL);
PreparedStatement ps = con.prepareStatement(updateSQL);)
{
int x=1;
for(Component c : adminPermissionsChbxs){
if(c instanceof JCheckBox){
bool = ( (JCheckBox)c ).isSelected();
JOptionPane.showMessageDialog(null,"Name: "+ ((JCheckBox)c).getName() );
JOptionPane.showMessageDialog(null,bool);
} //--end of if
int boolToInt = (bool)?1:0 ;
ps.setInt(x, boolToInt);
x++;
}//--end of forloop
ps.setInt(8, um.getIdOfSelected(usersList));
ps.executeUpdate(); JOptionPane.showMessageDialog(null,"Update successful");
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"Error@saveAdminPermissions\n"+ e.getMessage());
}
}//--end of method
Thanks. 谢谢。
Several approaches are possible: 几种方法是可能的:
Instead of looping though view components, create a permission model having a collection of states such as List<Boolean>
. 与其遍历视图组件,不如创建一个权限模型,该权限模型具有一组状态,例如List<Boolean>
。 As shown in How to Use Check Boxes , let each JCheckBox
have an ItemListener
that updates its Boolean
value in the List
; 如如何使用复选框所示,让每个JCheckBox
都有一个ItemListener
来更新List
Boolean
值; an example is seen here . 这里有一个例子。 Use the list in constructing your PreparedStatement
. 使用该列表构造您的PreparedStatement
。
Alternatively, store the permission description and state in a suitable TableModel
, and use a JTable
as the view. 或者,将权限描述和状态存储在合适的TableModel
,并使用JTable
作为视图。 As described in How to Use Tables: Concepts: Editors and Renderers , the default renderer/editor for a model value of type Boolean
is a check box. 如如何使用表:概念:编辑器和渲染器中所述,模型类型为Boolean
的默认渲染器/编辑器是一个复选框。 Neither the renderer nor editor remember the value between calls to render or edit a cell's value, but the table underlying TableModel
must do so. 渲染器和编辑器都不会记住两次调用之间的值以渲染或编辑单元格的值,但是TableModel
基础的表必须这样做。 Some guidelines and a typical example are cited here . 这里引用了一些准则和典型示例。 You should be able to invoke the table's getValueAt()
method to find the current setting of the desired cell in a particular row and column, as shown here . 你应该能够调用表的getValueAt()
方法来找到所需电池的电流设定在特定的行和列,如图所示这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.