0

I'm trying to make a search function in JTable in my program, but i got sql syntax error when i use this syntax. I've been read much question and answer in this problem and i never got the right syntax. Hope you can help me.

private void fillTable(String keyword) throws SQLException{
    if (!keyword.equals("")){
        try( Connection con = DriverManager.getConnection
                            ("jdbc:mysql://localhost:3306/inventory?zero"
                                    + "DateTimeBehavior=convertToNull","root","");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM basis"
                    + "WHERE barcode LIKE '%"+keyword+"%' or "
                    + "namaProduk LIKE '% "+keyword+" %'");               
        ){
            jTable1.setModel(buildTableModel(rs));
        }  
    }
    else fillTable();
}

2 Answers 2

4

You are missing 1 space between basis and WHERE (reason for the syntax error).

There is 1 unwanted space between % and your keyword (makes your query irrelevant).

private void fillTable(String keyword) throws SQLException{
    if (!keyword.equals("")){
        try( Connection con = DriverManager.getConnection
                            ("jdbc:mysql://localhost:3306/inventory?zero"
                                    + "DateTimeBehavior=convertToNull","root","");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM basis "
                    + "WHERE barcode LIKE '%"+keyword+"%' or "
                    + "namaProduk LIKE '%"+keyword+" %'");               
        ){
            jTable1.setModel(buildTableModel(rs));
        }  
    }
    else fillTable();
}
Sign up to request clarification or add additional context in comments.

Comments

1

What I can see here is that there's a space missing behind the table name "basis". Generally, you should test your SQL statements manually via an existing DB client and then copy them into your code.

If you're open to tools that will ease your pain, try QueryDSL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.