Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过使用自动更新jtable的jtextfield KeyReleased事件将模式匹配到Sql数据库_Java_Mysql_Sql_Jtable_Jtextfield - Fatal编程技术网

Java 通过使用自动更新jtable的jtextfield KeyReleased事件将模式匹配到Sql数据库

Java 通过使用自动更新jtable的jtextfield KeyReleased事件将模式匹配到Sql数据库,java,mysql,sql,jtable,jtextfield,Java,Mysql,Sql,Jtable,Jtextfield,我试图使用jTextfield tableQuery上的keyReleased事件自动更新包含数据库内容的jtable表 我正在mysql中使用%功能进行模式匹配 但是,如果运气不好,在文本字段中键入单个字符时,会出现错误java.lang.ArrayIndexOutofBoundsException 0 请告诉我我的代码出了什么问题 我正在为此使用Netbeans GUI Builder 这是我的密码,请告诉我这有什么问题 ... import net.proteanit.sql.DbUtil

我试图使用jTextfield tableQuery上的keyReleased事件自动更新包含数据库内容的jtable表

我正在mysql中使用%功能进行模式匹配

但是,如果运气不好,在文本字段中键入单个字符时,会出现错误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) {

            }
        }
    }     

您需要在setString方法中设置%s。像这样:

pst.setString(1, "%" + yourString +"%");

以下是在任何java应用程序中对KeyReleased事件验证用户名或电子邮件的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());
}

看看这个,去掉逗号后,我现在遇到了这个错误:SQL错误或缺少靠近%:错误的数据库。我甚至尝试过:stringsql=从empoyeinfo中选择empoyeid、name、姓氏、年龄,其中name像%'+tableQuery.getText+'%;;得到了同样的结果error@Lars我的查询使用此代码并通过按文本字段上的任意键来工作:String sql=从EmpoyeInfo中选择EmpoyeId、name、姓氏、年龄,其中名称类似于“%mario%”;;但不是什么时候?包括在内