使用jdbc不正确地插入sql db

时间:2017-10-29 06:15:01

标签: java oracle jdbc applet

问题是运行测试代码以使用applet将数据插入数据库以获取输入。一旦我插入数据并关闭applet,数据按顺序,在db中相应的EMPLOYEE表中。但是当再次重新启动applet并再次插入时,数据将插入到表的开头而不是按顺序插入。

我似乎无法理解这个问题。

这里是相应的代码,

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.sql.*;

public class Test extends Frame {
    TextField tf;
    Label l;
    Button b;
    int count=1;

    Test() {
        tf = new TextField();
        l = new Label("Enter Name");
        b = new Button("Join");
        tf.setBounds(100, 130, 100, 30);
        l.setBounds(100, 100, 100, 30);
        b.setBounds(100, 160, 100, 30);

        add(l);
        add(tf);
        add(b);
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                join(tf.getText());

            }
        });

        setSize(500, 500);
        setLayout(null);
        setVisible(true);
    }

    private void join(String name) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");

            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@hemesh-pc:1521:xe","system", "*****");

            Statement s = conn.createStatement();

            conn.setAutoCommit(false);

            ResultSet resultSet = s.executeQuery("SELECT count(*) FROM EMPLOYEE");

            resultSet.next();

            System.out.println(resultSet.getInt("count(*)"));

            count = resultSet.getInt("count(*)")+1;

            String query = "INSERT INTO EMPLOYEE VALUES("+count+",'"+name+"')";

            int res = s.executeUpdate(query);

            System.out.println(res+"Row created");

            conn.commit();

            s.close();

            conn.close();
        }catch(Exception c){ 
            System.out.println(c);
        }
    }

    public static void main(String args[]) {
        Test test = new Test();
    }
}

enter image description here

0 个答案:

没有答案