Value Always Set to 0 instead of 1 When Creating Auto-Generated ID in Netbeans












0















I'm trying to make unique auto-generated ID using combination of char, date, and value. This is my code :



int getValue;    

public void generateNOS(String query) throws SQLException {
try {
Connection con = koneksi.koneksiDB();
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(query);
if (rs.next()) {
getValue = Integer.parseInt(rs.getString(5));
}
} catch (Exception e) {

}
}
public void autonumberNOS() throws SQLException {
generateNOS("SELECT count(no_surat)+1 FROM surat_masuk");

try {
String NOS = "NOS/1/"+new SimpleDateFormat("yyyyMMdd").format(new Date())+"/"+getValue;
txtNOS.setText(NOS);
} catch (Exception e) {

}
}


For example, the ID supposed to "NOS/1/20181120/1". But i got "NOS/1/20181120/0". What was missing ? Is the SQL syntax wrong ? Thanks for any help.










share|improve this question

























  • this seems to be asking for duplicate IDs to be created (e.g. in the case of more than one user using the system simultaneously). Better to use an autoincrement field in the DB and get an ID based on that. You can always generate this kind of more user-friendly ID on-demand for display purposes later, based on the data in the table.

    – ADyson
    Nov 20 '18 at 14:01


















0















I'm trying to make unique auto-generated ID using combination of char, date, and value. This is my code :



int getValue;    

public void generateNOS(String query) throws SQLException {
try {
Connection con = koneksi.koneksiDB();
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(query);
if (rs.next()) {
getValue = Integer.parseInt(rs.getString(5));
}
} catch (Exception e) {

}
}
public void autonumberNOS() throws SQLException {
generateNOS("SELECT count(no_surat)+1 FROM surat_masuk");

try {
String NOS = "NOS/1/"+new SimpleDateFormat("yyyyMMdd").format(new Date())+"/"+getValue;
txtNOS.setText(NOS);
} catch (Exception e) {

}
}


For example, the ID supposed to "NOS/1/20181120/1". But i got "NOS/1/20181120/0". What was missing ? Is the SQL syntax wrong ? Thanks for any help.










share|improve this question

























  • this seems to be asking for duplicate IDs to be created (e.g. in the case of more than one user using the system simultaneously). Better to use an autoincrement field in the DB and get an ID based on that. You can always generate this kind of more user-friendly ID on-demand for display purposes later, based on the data in the table.

    – ADyson
    Nov 20 '18 at 14:01
















0












0








0








I'm trying to make unique auto-generated ID using combination of char, date, and value. This is my code :



int getValue;    

public void generateNOS(String query) throws SQLException {
try {
Connection con = koneksi.koneksiDB();
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(query);
if (rs.next()) {
getValue = Integer.parseInt(rs.getString(5));
}
} catch (Exception e) {

}
}
public void autonumberNOS() throws SQLException {
generateNOS("SELECT count(no_surat)+1 FROM surat_masuk");

try {
String NOS = "NOS/1/"+new SimpleDateFormat("yyyyMMdd").format(new Date())+"/"+getValue;
txtNOS.setText(NOS);
} catch (Exception e) {

}
}


For example, the ID supposed to "NOS/1/20181120/1". But i got "NOS/1/20181120/0". What was missing ? Is the SQL syntax wrong ? Thanks for any help.










share|improve this question
















I'm trying to make unique auto-generated ID using combination of char, date, and value. This is my code :



int getValue;    

public void generateNOS(String query) throws SQLException {
try {
Connection con = koneksi.koneksiDB();
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(query);
if (rs.next()) {
getValue = Integer.parseInt(rs.getString(5));
}
} catch (Exception e) {

}
}
public void autonumberNOS() throws SQLException {
generateNOS("SELECT count(no_surat)+1 FROM surat_masuk");

try {
String NOS = "NOS/1/"+new SimpleDateFormat("yyyyMMdd").format(new Date())+"/"+getValue;
txtNOS.setText(NOS);
} catch (Exception e) {

}
}


For example, the ID supposed to "NOS/1/20181120/1". But i got "NOS/1/20181120/0". What was missing ? Is the SQL syntax wrong ? Thanks for any help.







mysql netbeans unique id autonumber






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 14:36







abw1904

















asked Nov 20 '18 at 13:48









abw1904abw1904

105




105













  • this seems to be asking for duplicate IDs to be created (e.g. in the case of more than one user using the system simultaneously). Better to use an autoincrement field in the DB and get an ID based on that. You can always generate this kind of more user-friendly ID on-demand for display purposes later, based on the data in the table.

    – ADyson
    Nov 20 '18 at 14:01





















  • this seems to be asking for duplicate IDs to be created (e.g. in the case of more than one user using the system simultaneously). Better to use an autoincrement field in the DB and get an ID based on that. You can always generate this kind of more user-friendly ID on-demand for display purposes later, based on the data in the table.

    – ADyson
    Nov 20 '18 at 14:01



















this seems to be asking for duplicate IDs to be created (e.g. in the case of more than one user using the system simultaneously). Better to use an autoincrement field in the DB and get an ID based on that. You can always generate this kind of more user-friendly ID on-demand for display purposes later, based on the data in the table.

– ADyson
Nov 20 '18 at 14:01







this seems to be asking for duplicate IDs to be created (e.g. in the case of more than one user using the system simultaneously). Better to use an autoincrement field in the DB and get an ID based on that. You can always generate this kind of more user-friendly ID on-demand for display purposes later, based on the data in the table.

– ADyson
Nov 20 '18 at 14:01














1 Answer
1






active

oldest

votes


















0














I don't fully follow your code, but I can point out some problems with the JDBC logic. Here is what you are doing right now:



Connection con = koneksi.koneksiDB();
Statement stm = con.createStatement();
String query = "SELECT COUNT(no_surat)+1 FROM surat_masuk";
ResultSet rs = stm.executeQuery(query);
if (rs.next()) {
getValue = Integer.parseInt(rs.getString(5));
}


It makes no sense to be asking for the fifth column, when your select only has one column in the result set. You should probably be going after the first column index, i.e.



if (rs.next()) {
getValue = rs.getInt(1);
}


Note also that I am using ResultSet#getInt() here, rather than ResultSet#getString(), because the count would typically be represented by an integer at the database level.



An alternative to requesting the first column index would be to give the count term an alias, and then access that:



String query = "SELECT COUNT(no_surat) + 1 AS cnt FROM surat_masuk";
ResultSet rs = stm.executeQuery(query);
if (rs.next()) {
getValue = rs.getInt("cnt");
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394474%2fvalue-always-set-to-0-instead-of-1-when-creating-auto-generated-id-in-netbeans%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I don't fully follow your code, but I can point out some problems with the JDBC logic. Here is what you are doing right now:



    Connection con = koneksi.koneksiDB();
    Statement stm = con.createStatement();
    String query = "SELECT COUNT(no_surat)+1 FROM surat_masuk";
    ResultSet rs = stm.executeQuery(query);
    if (rs.next()) {
    getValue = Integer.parseInt(rs.getString(5));
    }


    It makes no sense to be asking for the fifth column, when your select only has one column in the result set. You should probably be going after the first column index, i.e.



    if (rs.next()) {
    getValue = rs.getInt(1);
    }


    Note also that I am using ResultSet#getInt() here, rather than ResultSet#getString(), because the count would typically be represented by an integer at the database level.



    An alternative to requesting the first column index would be to give the count term an alias, and then access that:



    String query = "SELECT COUNT(no_surat) + 1 AS cnt FROM surat_masuk";
    ResultSet rs = stm.executeQuery(query);
    if (rs.next()) {
    getValue = rs.getInt("cnt");
    }





    share|improve this answer




























      0














      I don't fully follow your code, but I can point out some problems with the JDBC logic. Here is what you are doing right now:



      Connection con = koneksi.koneksiDB();
      Statement stm = con.createStatement();
      String query = "SELECT COUNT(no_surat)+1 FROM surat_masuk";
      ResultSet rs = stm.executeQuery(query);
      if (rs.next()) {
      getValue = Integer.parseInt(rs.getString(5));
      }


      It makes no sense to be asking for the fifth column, when your select only has one column in the result set. You should probably be going after the first column index, i.e.



      if (rs.next()) {
      getValue = rs.getInt(1);
      }


      Note also that I am using ResultSet#getInt() here, rather than ResultSet#getString(), because the count would typically be represented by an integer at the database level.



      An alternative to requesting the first column index would be to give the count term an alias, and then access that:



      String query = "SELECT COUNT(no_surat) + 1 AS cnt FROM surat_masuk";
      ResultSet rs = stm.executeQuery(query);
      if (rs.next()) {
      getValue = rs.getInt("cnt");
      }





      share|improve this answer


























        0












        0








        0







        I don't fully follow your code, but I can point out some problems with the JDBC logic. Here is what you are doing right now:



        Connection con = koneksi.koneksiDB();
        Statement stm = con.createStatement();
        String query = "SELECT COUNT(no_surat)+1 FROM surat_masuk";
        ResultSet rs = stm.executeQuery(query);
        if (rs.next()) {
        getValue = Integer.parseInt(rs.getString(5));
        }


        It makes no sense to be asking for the fifth column, when your select only has one column in the result set. You should probably be going after the first column index, i.e.



        if (rs.next()) {
        getValue = rs.getInt(1);
        }


        Note also that I am using ResultSet#getInt() here, rather than ResultSet#getString(), because the count would typically be represented by an integer at the database level.



        An alternative to requesting the first column index would be to give the count term an alias, and then access that:



        String query = "SELECT COUNT(no_surat) + 1 AS cnt FROM surat_masuk";
        ResultSet rs = stm.executeQuery(query);
        if (rs.next()) {
        getValue = rs.getInt("cnt");
        }





        share|improve this answer













        I don't fully follow your code, but I can point out some problems with the JDBC logic. Here is what you are doing right now:



        Connection con = koneksi.koneksiDB();
        Statement stm = con.createStatement();
        String query = "SELECT COUNT(no_surat)+1 FROM surat_masuk";
        ResultSet rs = stm.executeQuery(query);
        if (rs.next()) {
        getValue = Integer.parseInt(rs.getString(5));
        }


        It makes no sense to be asking for the fifth column, when your select only has one column in the result set. You should probably be going after the first column index, i.e.



        if (rs.next()) {
        getValue = rs.getInt(1);
        }


        Note also that I am using ResultSet#getInt() here, rather than ResultSet#getString(), because the count would typically be represented by an integer at the database level.



        An alternative to requesting the first column index would be to give the count term an alias, and then access that:



        String query = "SELECT COUNT(no_surat) + 1 AS cnt FROM surat_masuk";
        ResultSet rs = stm.executeQuery(query);
        if (rs.next()) {
        getValue = rs.getInt("cnt");
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 13:56









        Tim BiegeleisenTim Biegeleisen

        226k1392145




        226k1392145
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394474%2fvalue-always-set-to-0-instead-of-1-when-creating-auto-generated-id-in-netbeans%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents