我正在尝试使用jTextfield(tableQuery)上的keyReleased事件自动使用数据库内容更新jtable(Table_world)。
我在mysql中使用%feature来进行模式匹配。
但是没有运气我收到错误java.lang.ArrayIndexOutofBoundsException 0,当我在文本字段中键入单个字符时。
请告诉我我的代码有什么问题?
我正在使用Netbeans GUI Builder。
这是我的代码请告诉我这是什么问题。
...
import net.proteanit.sql.DbUtils;
....
private void tableQueryKeyReleased(java.awt.event.KeyEvent evt) {
try {
String sql = "select empoyeeid, name, surname, age from empoyeeinfo where name LIKE '%?%';";
pst=conn.prepareStatement(sql);
pst.setString(1, tableQuery.getText());
rs = pst.executeQuery();
Table_world.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch(Exception e) {
}
}
}
答案 0 :(得分:1)
您需要设置"%"在setString方法中。像这样:
pst.setString(1, "%" + yourString +"%");
答案 1 :(得分:-1)
这是用于在KeyReleased事件中的任何Java应用程序中验证用户名或电子邮件的Java代码。
我将Netbeans用于我的应用程序,并将mysql作为数据库使用。
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/tayal_india_pvt_ltd","root","1234");
ResultSet rs=null;
String str="select * from login where username like '%"+jTextField6.getText()+"%';";
PreparedStatement psmt=con.prepareStatement(str);
rs=psmt.executeQuery();
if(rs.next()){
String username=rs.getString("username");
Pattern p=Pattern.compile(username);
if (p.matcher(jTextField6.getText()).matches()) {
jLabel16.setText("Username already exists");
}
else{
jLabel16.setText("");
}
}
con.close();
psmt.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e.getMessage());
}