0

I am making an application in NetBeans (java). This application has unique id combination of string and integer like abc/111 or xyz/253 and the integer part should increase by when a new entry takes place in the database i.e. abc/112 and xyz/254.

The problem is the value of integer part increase until it has reached 10 in a proper way but after that it does not increase and remain same for further entries in database.

I used the following code -

try{
    String sql = "SELECT RegNumber FROM Death ORDER BY RegNumber DESC   ";
    pst = conn.prepareStatement(sql);

    rs = pst.executeQuery();

    if (rs.next()) {

        String add1 = rs.getString("RegNumber");
        String[] parts= add1.split("/");
        String part1= parts[0];
        String part2= parts[1];
        int a,b;
        a= Integer.parseInt(part2);
        b=a+1;

        jTextField20.setText(""+part1+"/"+b);


        JOptionPane.showMessageDialog(null, "done");
    }
   }

"Integer part increase till 10" means that if I start the first value of id in database like abc/1 then new id generates automatically for the next entry with the increasing value 1 that is abc/2 and for next entry it is abc/3 and so on in sequential order like this: abc/4, ..., abc/10

But when it has reached abc/10 the new generated id remains same i.e. abc/10 for every new entry in database. (I am using MS Access 2007 and the id is of text type). The first id in the database is created by the application itself.

If anyone has another alternative to generate id, please tell me.

4

2 に答える 2

3

問題はそれです

String sql = "SELECT RegNumber FROM Death ORDER BY RegNumber DESC   ";

アルファベットの降順でソートし、アルファベット順に並べ替えます

"abc/9" > "abc/10"

そのため、プログラムは常に 9 を何度もフェッチします...

その列を分割して保存し、数値部分を実際の数値型としてデータベースに保存する必要があると思います。それはおそらく思ったほど難しいことではありません。いつでも 2 つのフィールドで並べ替えることができます。

String sql = "SELECT RegNumber FROM Death ORDER BY RegString DESC, RegNumber DESC   ";

SERIAL挿入ロジックをさらに簡素化するために、特定の場合 (たとえば、文字列部分が変更されたときに RegNumber がリセットされない場合) に RegNumber 部分に (自動インクリメント) データ型を使用することを検討することもできます。

于 2012-07-11T23:18:19.683 に答える
2

選択クエリは、エントリを降順で並べ替えています。これは、Varchar タイプの "SELECT RegNumber FROM Death ORDER BY RegNumber DESC" です。

つまり、取得した値を abc/9、abc/8、abc/7、abc/6、abc/5、abc/4、abc/3、abc/2、abc/10、abc/1 として並べ替えた後です。

つまり、最初の ID は常に 9 であり、次の値は常に 10 になります。

于 2012-07-11T23:21:02.693 に答える