试图将我的java gui连接到我的mysql数据库,试图将数据插入数据库

时间:2013-10-14 19:27:45

标签: java mysql database-connection

import java.awt.BorderLayout;


public class Credit_Deposit extends JFrame {

private JPanel contentPane;
private JTextField contractField;
private JTextField creditDescriptionField;
private JTextField vendorField;
private JTextField numberCredit;
private JTextField equivalentHoursField;
private JTextField equivalentCostField;
private JTextField expDateField;
private JTextField serviceManagerField;
private JButton btnNewButton_1;
// contract type, if you need to add another contract place it inside the French brackets.
private String [] c_Type={"Select Type","Professional Training","Hardware","Equipment Rebates"};

/**
 * Launch the application.

 */
public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Credit_Deposit frame = new Credit_Deposit();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 * @throws ClassNotFoundException 
 * @throws SQLException 
 */


public static Connection connectDb(){
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3308/cox_communication","root","speedy");
        return con;
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    JOptionPane.showMessageDialog(null, "No Connection!");
    return null;
}


public Credit_Deposit() {
;
    setType(Type.POPUP);
    setBackground(new Color(173, 216, 230));
    setTitle("           Credit Deposit");
    setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\coolm_000\\Pictures\\Cox.jpg"));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 608, 512);
    contentPane = new JPanel();
    contentPane.setForeground(Color.WHITE);
    contentPane.setBackground(new Color(135, 206, 250));
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    SpringLayout sl_contentPane = new SpringLayout();
    contentPane.setLayout(sl_contentPane);

    JLabel contractLabel = new JLabel("Contract Number:");
    sl_contentPane.putConstraint(SpringLayout.NORTH, contractLabel, 23, SpringLayout.NORTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.WEST, contractLabel, 10, SpringLayout.WEST, contentPane);
    contractLabel.setForeground(new Color(128, 128, 128));
    contractLabel.setFont(new Font("Arial Narrow", Font.PLAIN, 15));
    contentPane.add(contractLabel);

    JLabel vendorLabel = new JLabel("Vendor:");
    sl_contentPane.putConstraint(SpringLayout.NORTH, vendorLabel, 22, SpringLayout.SOUTH, contractLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, vendorLabel, 10, SpringLayout.WEST, contentPane);
    vendorLabel.setForeground(new Color(128, 128, 128));
    vendorLabel.setFont(new Font("Arial Narrow", Font.PLAIN, 15));
    contentPane.add(vendorLabel);

    JLabel creditLabel = new JLabel("Credit Type:");
    sl_contentPane.putConstraint(SpringLayout.NORTH, creditLabel, 17, SpringLayout.SOUTH, vendorLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, vendorLabel, -12, SpringLayout.EAST, creditLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, creditLabel, 0, SpringLayout.WEST, contractLabel);
    creditLabel.setFont(new Font("Arial Narrow", Font.PLAIN, 15));
    creditLabel.setForeground(new Color(128, 128, 128));
    contentPane.add(creditLabel);

    JLabel descriptionLabel = new JLabel("Credit Description:");
    sl_contentPane.putConstraint(SpringLayout.NORTH, descriptionLabel, 22, SpringLayout.SOUTH, creditLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, descriptionLabel, 0, SpringLayout.EAST, contractLabel);
    descriptionLabel.setForeground(new Color(128, 128, 128));
    descriptionLabel.setFont(new Font("Arial Narrow", Font.PLAIN, 15));
    contentPane.add(descriptionLabel);

    JLabel numberCreditsLabel = new JLabel("Number Of Credits:");
    numberCreditsLabel.setFont(new Font("Arial Narrow", Font.PLAIN, 15));
    sl_contentPane.putConstraint(SpringLayout.NORTH, numberCreditsLabel, 31, SpringLayout.SOUTH, descriptionLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, numberCreditsLabel, 0, SpringLayout.WEST, descriptionLabel);
    numberCreditsLabel.setForeground(new Color(128, 128, 128));
    contentPane.add(numberCreditsLabel);

    contractField = new JTextField();
    sl_contentPane.putConstraint(SpringLayout.NORTH, contractField, 0, SpringLayout.NORTH, contractLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, contractField, 6, SpringLayout.EAST, contractLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, contractField, -324, SpringLayout.EAST, contentPane);
    contentPane.add(contractField);
    contractField.setColumns(10);

    vendorField = new JTextField();
    sl_contentPane.putConstraint(SpringLayout.NORTH, vendorField, 19, SpringLayout.SOUTH, contractField);
    sl_contentPane.putConstraint(SpringLayout.WEST, vendorField, 114, SpringLayout.WEST, contentPane);
    sl_contentPane.putConstraint(SpringLayout.EAST, vendorField, 0, SpringLayout.EAST, contractField);
    vendorField.setColumns(10);
    contentPane.add(vendorField);

    final JComboBox creditTypeField= new JComboBox(c_Type);
    sl_contentPane.putConstraint(SpringLayout.NORTH, creditTypeField, 0, SpringLayout.NORTH, creditLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, creditTypeField, 0, SpringLayout.WEST, contractField);
    sl_contentPane.putConstraint(SpringLayout.EAST, creditTypeField, -324, SpringLayout.EAST, contentPane);
    contentPane.add(creditTypeField);

    creditDescriptionField = new JTextField();
    sl_contentPane.putConstraint(SpringLayout.NORTH, creditDescriptionField, -1, SpringLayout.NORTH, descriptionLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, creditDescriptionField, 4, SpringLayout.EAST, descriptionLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, creditDescriptionField, -324, SpringLayout.EAST, contentPane);
    creditDescriptionField.setColumns(10);
    contentPane.add(creditDescriptionField);

    numberCredit = new JTextField();
    sl_contentPane.putConstraint(SpringLayout.WEST, numberCredit, 0, SpringLayout.WEST, creditDescriptionField);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, numberCredit, 0, SpringLayout.SOUTH, numberCreditsLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, numberCredit, 0, SpringLayout.EAST, contractField);
    numberCredit.setColumns(10);
    contentPane.add(numberCredit);

    JLabel manHoursLabel = new JLabel("Equivalent Man-Hours:");
    sl_contentPane.putConstraint(SpringLayout.NORTH, manHoursLabel, 0, SpringLayout.NORTH, contractLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, manHoursLabel, 15, SpringLayout.EAST, contractField);
    manHoursLabel.setFont(new Font("Arial Narrow", Font.PLAIN, 15));
    manHoursLabel.setForeground(new Color(128, 128, 128));
    contentPane.add(manHoursLabel);

    JLabel costLabel = new JLabel("Equivalent Cost:");
    sl_contentPane.putConstraint(SpringLayout.NORTH, costLabel, 0, SpringLayout.NORTH, vendorLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, costLabel, 0, SpringLayout.WEST, manHoursLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, costLabel, -23, SpringLayout.EAST, manHoursLabel);
    costLabel.setForeground(new Color(128, 128, 128));
    costLabel.setFont(new Font("Arial Narrow", Font.PLAIN, 15));
    contentPane.add(costLabel);

    JLabel expDateLabel = new JLabel("Credit Exp Date:");
    sl_contentPane.putConstraint(SpringLayout.NORTH, expDateLabel, 0, SpringLayout.NORTH, creditLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, expDateLabel, 0, SpringLayout.WEST, manHoursLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, expDateLabel, -19, SpringLayout.EAST, manHoursLabel);
    expDateLabel.setFont(new Font("Arial Narrow", Font.PLAIN, 15));
    expDateLabel.setForeground(new Color(128, 128, 128));
    contentPane.add(expDateLabel);

    JLabel managerLabel = new JLabel("Service Manager");
    sl_contentPane.putConstraint(SpringLayout.NORTH, managerLabel, 0, SpringLayout.NORTH, descriptionLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, managerLabel, 0, SpringLayout.WEST, manHoursLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, managerLabel, 0, SpringLayout.EAST, costLabel);
    managerLabel.setFont(new Font("Arial Narrow", Font.PLAIN, 15));
    managerLabel.setForeground(new Color(128, 128, 128));
    contentPane.add(managerLabel);

    equivalentHoursField = new JTextField();
    sl_contentPane.putConstraint(SpringLayout.NORTH, equivalentHoursField, 0, SpringLayout.NORTH, contractLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, equivalentHoursField, 6, SpringLayout.EAST, manHoursLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, equivalentHoursField, -51, SpringLayout.EAST, contentPane);
    equivalentHoursField.setColumns(10);
    contentPane.add(equivalentHoursField);

    equivalentCostField = new JTextField();
    sl_contentPane.putConstraint(SpringLayout.NORTH, equivalentCostField, 0, SpringLayout.NORTH, vendorLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, equivalentCostField, 0, SpringLayout.WEST, equivalentHoursField);
    sl_contentPane.putConstraint(SpringLayout.EAST, equivalentCostField, -51, SpringLayout.EAST, contentPane);
    contentPane.add(equivalentCostField);
    equivalentCostField.setColumns(10);

    expDateField = new JTextField(" XX/XX/XXX ");
    sl_contentPane.putConstraint(SpringLayout.NORTH, expDateField, 0, SpringLayout.NORTH, creditLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, expDateField, 0, SpringLayout.WEST, equivalentHoursField);
    sl_contentPane.putConstraint(SpringLayout.EAST, expDateField, -111, SpringLayout.EAST, contentPane);
    contentPane.add(expDateField);
    expDateField.setColumns(10);

    serviceManagerField = new JTextField();
    sl_contentPane.putConstraint(SpringLayout.NORTH, serviceManagerField, 0, SpringLayout.NORTH, descriptionLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, serviceManagerField, 0, SpringLayout.WEST, equivalentHoursField);
    sl_contentPane.putConstraint(SpringLayout.EAST, serviceManagerField, 0, SpringLayout.EAST, equivalentHoursField);
    contentPane.add(serviceManagerField);
    serviceManagerField.setColumns(10);

    JLabel noteLabel = new JLabel("Notes:");
    sl_contentPane.putConstraint(SpringLayout.NORTH, noteLabel, 59, SpringLayout.SOUTH, numberCreditsLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, noteLabel, 0, SpringLayout.WEST, contractLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, noteLabel, -20, SpringLayout.EAST, contractLabel);
    noteLabel.setFont(new Font("Arial Narrow", Font.PLAIN, 15));
    noteLabel.setForeground(new Color(128, 128, 128));
    contentPane.add(noteLabel);

    JScrollPane noteScroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    sl_contentPane.putConstraint(SpringLayout.NORTH, noteScroll, 297, SpringLayout.NORTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.WEST, noteScroll, 74, SpringLayout.WEST, contentPane);
    sl_contentPane.putConstraint(SpringLayout.EAST, noteScroll, -77, SpringLayout.EAST, contentPane);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, noteLabel, -6, SpringLayout.NORTH, noteScroll);
    contentPane.add(noteScroll);

    final JTextArea noteField = new JTextArea();
    noteScroll.setViewportView(noteField);

    JButton btnNewButton = new JButton("Submit");
    sl_contentPane.putConstraint(SpringLayout.SOUTH, noteScroll, -6, SpringLayout.NORTH, btnNewButton);
    sl_contentPane.putConstraint(SpringLayout.WEST, btnNewButton, 194, SpringLayout.WEST, contentPane);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, btnNewButton, 0, SpringLayout.SOUTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.EAST, btnNewButton, -315, SpringLayout.EAST, contentPane);
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        Connection con= connectDb();

        int contract=Integer.parseInt(contractField.getText());
        String vendor=vendorField.getText();
        String credit = creditTypeField.getSelectedItem().toString();
        String creditDesc = creditDescriptionField.getText();
        int numCredit = Integer.parseInt(numberCredit.getText());
        int hours = Integer.parseInt(equivalentHoursField.getText());
        int cost = Integer.parseInt(equivalentCostField.getText());
        String mydate =expDateField.getText().replace("/", "-");
        String manager = serviceManagerField.getText();
        String notes = noteField.getText();
        try {
            String sql="INSERT INTO credit_deposit_forum (ContractNumber,Vendor,CreditType,CreditDescription,NumberCredits,EquivalentHours,EquivalentCost,CreditExpDate,ServiceManager,Notes) "
                    + "VALUES ('?','?','?','?','?','?','?','?','?','?')";

            PreparedStatement pst = con.prepareStatement(sql);

            pst.setInt(1,contract );
            pst.setString(2, vendor);   
            pst.setString(3, credit);
            pst.setString(4, creditDesc);
            pst.setInt(5,numCredit);
            pst.setInt(6, hours);
            pst.setInt(7,cost);     
            pst.setString(8,mydate);
            pst.setString(9, manager);
            pst.setString(10, notes);
            pst.executeUpdate(sql);
            JOptionPane.showMessageDialog(null, "Successfully Updated!");
            pst.close();
        } catch (SQLException e ) {
            JOptionPane.showMessageDialog(null, "you did note fill out all the required fields");

        }       

        }
    });
    btnNewButton.setFont(new Font("Arial Narrow", Font.PLAIN, 13));
    btnNewButton.setForeground(new Color(128, 128, 128));
    contentPane.add(btnNewButton);

    btnNewButton_1 = new JButton("Back");
    sl_contentPane.putConstraint(SpringLayout.NORTH, btnNewButton_1, 0, SpringLayout.NORTH, btnNewButton);
    sl_contentPane.putConstraint(SpringLayout.WEST, btnNewButton_1, 77, SpringLayout.EAST, btnNewButton);
    sl_contentPane.putConstraint(SpringLayout.EAST, btnNewButton_1, 422, SpringLayout.WEST, contentPane);
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            dispose();  
        }
    });
    btnNewButton_1.setFont(new Font("Arial Narrow", Font.PLAIN, 13));
    btnNewButton_1.setForeground(new Color(128, 128, 128));
    contentPane.add(btnNewButton_1);
}

}

我正在使用的表是credit_deposit_forum

-ContractNumber pk int(11)
-vendor varchar(255)
-CreditType varchar(255)
-CreditDescription varchar(255)
-NumberCredits int(11)
-EquivalentHours int(11)
-EquivalentCost int (11)
-CreditExpDate date
-ServiceManager int(11)
-Notes varchar(8000)

我可以连接到数据库,但我无法将数据插入数据库,真的会寻求帮助。

0 个答案:

没有答案