0

I am creating a small database that can store the records of my job numbers. I want to start each job number with 2 zeros (00) and then put the number of the job after. Like this "001-2019". When I go to query the database, it doesnt work. But if I get rid of the zeros and get rid of any special characters, it works fine. How can I use the zeros and special characters to query the database?

        btnOpen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                String jobNumber = txtJobNumber.getText();
                String sql = "SELECT Date FROM JOBCOSTS where Job_Number =" +jobNumber;
                try {
                    pst=conn.prepareStatement(sql);
                    rs = pst.executeQuery();
                    if(rs.next()) {
                    String date=rs.getString("Date");
                    txtJobCostDate.setText(date);

                    }
                }catch (Exception e) {
                    JOptionPane.showMessageDialog(null, e);

                }
            }
        });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Possible duplicate of [SQLITE cuts off zeros, when updating database](https://stackoverflow.com/questions/58480950/sqlite-cuts-off-zeros-when-updating-database) – Shawn Oct 22 '19 at 00:59

1 Answers1

0

First try using parametarized query since you are already using Prepared statements.

String sql = "SELECT Date FROM JOBCOSTS where Job_Number = ?";
pst=conn.prepareStatement(sql);
pst.setString(1, jobNumber);

Second, is your jobNumber should be in string so your leading zeroes will not be truncated.

btnOpen.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        String jobNumber = txtJobNumber.getText();
        String sql = "SELECT Date FROM JOBCOSTS where Job_Number ='" +jobNumber+"'";
        try {
            pst=conn.prepareStatement(sql);
            rs = pst.executeQuery();
            if(rs.next()) {
            String date=rs.getString("Date");
            txtJobCostDate.setText(date);

            }
        }catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);

        }
    }
});
Ed Bangga
  • 12,879
  • 4
  • 16
  • 30
  • Awesome, problem solved. But I was able to achieve proper functioning by just adjusting to = '" +jobNumber+"'"; Can you explain why it works just by adding a single quote, and a double quote at the end? im honestly shocked it was so simple, yet confused. thx again. –  Oct 22 '19 at 03:29