-2
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.*;

public class TTShow extends JFrame implements ActionListener {

    String item1;
    JPanel Left, Right, Up, Down, Cntr;
    JButton clos, hlp, add;
    String str;
    WizardSub frm_tch;
    JComboBox subj;
    String sub_code;
    String sub_name;
    int lpw;
    int dur;
    static DBInfo objj0, objj1, objj2, objj3, objj4, objj5, objj6;
    static Statement st0, st1, st2, st3, st5, st6;
    PreparedStatement pstmt4;
    static ResultSet rs0, rs1, rs2, rs3, rs5;;
    static Connection cn0, cn1, cn2, cn3, cn4, cn5, cn6;
    static String str1;
    private JTable table;

    public static Vector rows() {

        Vector data = new Vector();
        String sql = "select * from tt";

        try {
            objj0 = new DBInfo();// 0
            cn0 = objj0.getCon();
            st0 = cn0.createStatement();
            rs0 = st0.executeQuery(sql);
            ResultSetMetaData md = rs0.getMetaData();
            int columns = md.getColumnCount();

            while (rs0.next()) {

                Vector row = new Vector(columns);
                for (int i = 2; i <= columns; i++) {
                    row.addElement(rs0.getObject(i));
                }
                data.addElement(row);
            }

            rs0.close();
            st0.close();
            cn0.close();

        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getStackTrace());
        }

        return data;
    }

    public static Vector columns()
    {
        Vector cols = new Vector();
        String sql2 = "select * from tt";
        try {
            objj1 = new DBInfo();// 1
            cn1 = objj1.getCon();
            st1 = cn1.createStatement();
            rs1 = st1.executeQuery(sql2);

            ResultSetMetaData md = rs1.getMetaData();
            int columns = md.getColumnCount();
            for (int i = 2; i <= columns; i++) {
                cols.addElement(md.getColumnName(i));
            }

            rs1.close();
            st1.close();
            cn1.close();
        }

        catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getStackTrace());
        }

        return cols;
    }

    public TTShow(String title) {
        super(title);
        setLayout(new BorderLayout());
        setSize(900, 600);
        setLocationRelativeTo(this);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);

        Left = new JPanel();
        Left.setLayout(new GridLayout(8, 1, 0, 10));

        Right = new JPanel();
        Right.setLayout(new GridLayout(12, 1, 0, 20));

        Up = new JPanel();
        Down = new JPanel();

        Cntr = new JPanel();
        Cntr.setBorder(BorderFactory.createTitledBorder(BorderFactory
                .createEtchedBorder(), ""));

        subj = new JComboBox();
        String strr = "select * from ttg_sub";
        try {
            objj2 = new DBInfo();// 2
            cn2 = objj2.getCon();
            st2 = cn2.createStatement();
            rs2 = st2.executeQuery(strr);
            while (rs2.next()) {
                sub_code = rs2.getString(1);
                sub_name = rs2.getString(2);
                lpw = rs2.getInt(3);
                dur = rs2.getInt(4);
                subj.addItem(sub_name);
            }

            rs2.close();
            st2.close();
            cn2.close();
        }

        catch (Exception e) {
        }

        add = new JButton("add");
        add.addActionListener(this);
        Left.add(add);
        Left.add(subj);

        final JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 85, 474, 187);
        Cntr.add(scrollPane);

        table = new JTable(rows(), columns()); // chargement de JTable
        scrollPane.setViewportView(table);
        add(Cntr, BorderLayout.CENTER);
        add(Left, BorderLayout.WEST);
    }

    public void getSub(){
        String ssq = "select * from ttg_sub";
        try {
            objj3=new DBInfo();//3
            cn3=(Connection) objj3.getCon();
            st3 = cn3.createStatement();
            rs3 = st3.executeQuery(ssq);
            while (rs3.next()) {
                sub_code = rs3.getString(1);
                sub_name = rs3.getString(2);
                lpw = rs3.getInt(3);
                dur = rs3.getInt(4);
            }

            rs3.close();
            st3.close();
            cn3.close();
        }
        catch(Exception e){}
    }

    public void actionPerformed(ActionEvent ls) {
        if (ls.getSource() == clos)
        {
            System.exit(0);
        }
        else if (ls.getSource() == add)
        {
            item1 = (String) subj.getSelectedItem();
            System.out.print(item1);
            String item = item1;
            Connection connn;
            String ssqs = "insert into tt(I) values(?)";
            try {
                objj4 = new DBInfo();// 4
                cn4 = (Connection) objj4.getCon();
                pstmt4 = cn4.prepareStatement(ssqs);
                pstmt4.setString(3, item1);
                int i = pstmt4.executeUpdate();
                System.out.print(i);

                pstmt4.close();
                cn4.close();
            }

            catch (Exception e) {
            }
        }
    }

    public void putSub() {
        Connection conn;
        String ssq = "select * from ttg_sub";
        try {
            objj5 = new DBInfo();// 5
            cn5 = (Connection) objj5.getCon();
            st5 = cn5.createStatement();
            rs5 = st5.executeQuery(ssq);
            while (rs5.next()) {
                sub_code = rs5.getString(1);
                sub_name = rs5.getString(2);
                lpw = rs5.getInt(3);
                dur = rs5.getInt(4);
            }

            rs5.close();
            st5.close();
            cn5.close();
        }

        catch (Exception e) {
        }
    }

    public void setSub() {
        Connection connn;
        String ssq = "insert into tt(I) values(item1)";
        try {
            objj6 = new DBInfo();// 6
            cn6 = (Connection) objj6.getCon();
            st6 = cn6.createStatement();
            st6.close();
            st6.close();
            cn6.close();
        }

        catch (Exception e) {
        }
    }

    public static void main(String args[])
    {
        try {
            TTShow dialog = new TTShow(str1);
            dialog.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
4

1 回答 1

1

你目前有

String ssqs = "insert into tt(I) values(?)";
pstmt4 = cn4.prepareStatement(ssqs);
pstmt4.setString(3, item1);

但是PreparedStatement参数索引从开始,1所以你需要:

pstmt4.setString(1, item1);

与此相关,不要在其中进行数据库调用,EDT因为它们会阻塞该线程。改用 a SwingWorker。这是一个例子

于 2012-12-26T20:42:36.970 回答