Skip to content
Advertisement

How to select and view database with specific value?

So I’m trying to search and print the value from the PHPMyAdmin database. But the result shows all row which has variable with containing the value. Like when I try search variable int bulan that has value 2. This also shows a row that has a value of 12, 22 or 23 as a result.

this is my code

System.out.print("Masukkan bulan yang ingin ditampilkan [mm]: ");
                var.mnip=input.nextLine();
                try{
                String sql="SELECT * FROM lembur " + "where bulan like('%" + var.mnip + "%')";
                st= (PreparedStatement) conn.prepareStatement(sql);
                rs = st.executeQuery(sql);
                while (rs.next()) {
                        System.out.println ("");
                        System.out.println ("Nomer Input       :"+rs.getString("number"));
                        System.out.println ("Nama              :"+rs.getString("nama"));
                        System.out.println ("NIP               :"+rs.getString("nip"));
                        System.out.println ("Divisi            :"+rs.getString("divisi"));
                        System.out.println ("Waktu Lembur      :");
                        System.out.println ("dd/mm/yyyy");
                        System.out.println ("tTanggal :"+rs.getString("tanggal"));
                        System.out.println ("tBulan   :"+rs.getString("bulan"));
                        System.out.println ("tTahun   :"+rs.getString("tahun"));
                        System.out.println ("Keterangan Lembur :"+rs.getString("ket_lembur"));
                        System.out.println ("Lama Lembur       :"+rs.getString("divisi"));
                        System.out.println ("");
                        System.out.println ("======================================");
                        System.out.println ("");
                  }
                }
                catch(SQLException e){
                System.out.println("Data Ada Kesalahan" + e.toString());
                    }

Advertisement

Answer

Like operator gives all matches where value 2 is in there that’s why you get 12, 22 or 23 in the output you should use equal to operator for an exact match.

Read More about Like operator

               System.out.print("Masukkan bulan yang ingin ditampilkan [mm]: ");
                var.mnip=input.nextLine();
                try{
                String sql="SELECT * FROM lembur " + "where bulan=" +var.mnip;
                st= (PreparedStatement) conn.prepareStatement(sql);
                rs = st.executeQuery(sql);
                while (rs.next()) {
                        System.out.println ("");
                        System.out.println ("Nomer Input       :"+rs.getString("number"));
                        System.out.println ("Nama              :"+rs.getString("nama"));
                        System.out.println ("NIP               :"+rs.getString("nip"));
                        System.out.println ("Divisi            :"+rs.getString("divisi"));
                        System.out.println ("Waktu Lembur      :");
                        System.out.println ("dd/mm/yyyy");
                        System.out.println ("tTanggal :"+rs.getString("tanggal"));
                        System.out.println ("tBulan   :"+rs.getString("bulan"));
                        System.out.println ("tTahun   :"+rs.getString("tahun"));
                        System.out.println ("Keterangan Lembur :"+rs.getString("ket_lembur"));
                        System.out.println ("Lama Lembur       :"+rs.getString("divisi"));
                        System.out.println ("");
                        System.out.println ("======================================");
                        System.out.println ("");
                  }
                }
                catch(SQLException e){
                System.out.println("Data Ada Kesalahan" + e.toString());
                    }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement