繁体   English   中英

java.sql.SQLSyntaxErrorException:表/视图“客户”不存在

[英]java.sql.SQLSyntaxErrorException: Table/View 'CUSTOMER' does not exist

我有一个从 jframe 获取输入并尝试将该数据存储到 derby 嵌入式数据库中的编。 我试图运行一个简单的插入语句来测试数据库,它给了我“java.sql.SQLSyntaxErrorException:表/视图‘客户’不存在”。 错误。 我做错了什么?`

import java.awt.BorderLayout;

import java.awt.EventQueue;
import java.awt.Label;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JList;
import java.awt.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSetMetaData;

public class Auto extends javax.swing.JFrame implements ActionListener  {

    /**
     * 
     */
    private static final long serialVersionUID = -5777093322764526515L;
    private JPanel contentPane;
    private JTextField f_name;
    private JTextField l_name;
    private JTextField f_name2;
    private JTextField l_name2;
    private JTextField Age;
    private JTextField issuing;
    private JTextField liscense_num;
    private JTextField rental;
    private JTextField return_d;
    private JTextField total;
    private JTextField Age2;
    private JTextField issuing2;
    private JTextField liscense_num2;
    private JTextField rental2;
    private JTextField return_d2;
    private JTextField total2;
    private JTextField agreement_id;
    private Button Submit_button;
    private Button Clear_button;
    private JButton load_button;
    private Button Update_button;
    private Button return_car;
    private JComboBox<?> car_select, car_select2;
    private JRadioButton cc_button;
    private JRadioButton cc_button2;
    private JRadioButton debit_button;
    private JRadioButton debit_button2;
    private static String url;
    private static Connection myConnection;
    private static Statement myStatement;


    /**
     * Launch the application.
     */
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                    url = "jdbc:derby:MyDB;create=true";
                    myConnection = DriverManager.getConnection(url);
                    myStatement = myConnection.createStatement( );
                    Auto frame = new Auto();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Auto() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 694, 497);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTabbedPane Auto_Pane = new JTabbedPane(JTabbedPane.TOP);
        Auto_Pane.setBounds(5, 5, 422, 432);
        contentPane.add(Auto_Pane);

        JPanel New_rent = new JPanel();
        Auto_Pane.addTab("New Rental", null, New_rent, null);
        New_rent.setLayout(null);

        JLabel first_name = new JLabel("First Name: ");
        first_name.setBounds(12, 8, 84, 16);
        New_rent.add(first_name);

        f_name = new JTextField(15);
        f_name.setBounds(97, 5, 116, 22);
        New_rent.add(f_name);
        f_name.setColumns(10);

        l_name = new JTextField(15);
        l_name.setBounds(97, 40, 116, 22);
        l_name.setColumns(10);
        New_rent.add(l_name);

        JLabel last_name = new JLabel("Last Name: ");
        last_name.setBounds(12, 43, 84, 16);
        New_rent.add(last_name);

        JLabel age = new JLabel("Age: ");
        age.setBounds(12, 76, 84, 16);
        New_rent.add(age);

        Age = new JTextField(2);
        Age.setBounds(97, 75, 35, 22);
        Age.setColumns(10);
        New_rent.add(Age);

        JLabel liscense = new JLabel("License #: ");
        liscense.setBounds(12, 113, 84, 16);
        New_rent.add(liscense);


        JLabel issuing_st = new JLabel("Issuing State: ");
        issuing_st.setBounds(12, 146, 84, 16);
        New_rent.add(issuing_st);

        issuing = new JTextField(2);
        issuing.setBounds(97, 143, 116, 22);
        issuing.setColumns(10);
        New_rent.add(issuing);

        liscense_num = new JTextField(13);
        liscense_num.setBounds(97, 110, 116, 22);
        liscense_num.setColumns(10);
        New_rent.add(liscense_num);

        String[] carStrings = { "Compact", "Midsized", "Standard", "Full Size", "SUV", "Mini Van" };
        car_select = new JComboBox<Object>(carStrings);
        car_select.setBounds(97, 178, 112, 22);
        New_rent.add(car_select);

        JLabel car_type = new JLabel("Car Type:");
        car_type.setBounds(12, 181, 84, 16);
        New_rent.add(car_type);

        JLabel rent_date = new JLabel("Rental Date:");
        rent_date.setBounds(12, 216, 84, 16);
        New_rent.add(rent_date);

        rental = new JTextField(10);
        rental.setBounds(97, 213, 116, 22);
        rental.setColumns(10);
        New_rent.add(rental);

        JLabel return_date = new JLabel("Return Date:");
        return_date.setBounds(12, 251, 84, 16);
        New_rent.add(return_date);

        return_d = new JTextField(10);
        return_d.setBounds(97, 248, 116, 22);
        return_d.setColumns(10);
        New_rent.add(return_d);

        JLabel total_num = new JLabel("Total:");
        total_num.setBounds(12, 283, 84, 16);
        New_rent.add(total_num);

        total = new JTextField(7);
        total.setBounds(97, 280, 116, 22);
        total.setColumns(10);
        New_rent.add(total);

        JLabel payment_type = new JLabel("Payment Type:");
        payment_type.setBounds(12, 317, 100, 16);
        New_rent.add(payment_type);

        cc_button = new JRadioButton("Credit Card");
        cc_button.setBounds(128, 313, 127, 25);
        New_rent.add(cc_button);

        debit_button = new JRadioButton("Debit");
        debit_button.setBounds(259, 313, 127, 25);
        New_rent.add(debit_button);

        ButtonGroup group = new ButtonGroup();
        group.add(debit_button);
        group.add(cc_button);

        Submit_button = new Button("Submit");
        Submit_button.setBounds(12, 356, 99, 24);
        Submit_button.addActionListener(this);
        New_rent.add(Submit_button);

        Clear_button = new Button("Clear Form");
        Clear_button.setBounds(128, 356, 99, 24);
        Clear_button.addActionListener(this);
        New_rent.add(Clear_button);

        JPanel Current_rent = new JPanel();
        Auto_Pane.addTab("Current Rental", null, Current_rent, null);
        Current_rent.setLayout(null);

        load_button = new JButton("Load Current Rental Agreements");
        load_button.addActionListener(this);
        load_button.setBounds(12, 13, 393, 25);
        Current_rent.add(load_button);

        List load_list = new List();
        load_list.setBounds(12, 54, 393, 229);
        Current_rent.add(load_list);

        JPanel Rental_ret = new JPanel();
        Auto_Pane.addTab("Rental Details", null, Rental_ret, null);
        Rental_ret.setLayout(null);

        JLabel first_name2 = new JLabel("First Name: ");
        first_name2.setBounds(15, 48, 71, 16);
        Rental_ret.add(first_name2);

        f_name2 = new JTextField(15);
        f_name2.setBounds(109, 45, 116, 22);
        Rental_ret.add(f_name2);
        f_name2.setColumns(10);

        l_name2 = new JTextField(15);
        l_name2.setBounds(109, 74, 116, 22);
        l_name2.setColumns(10);
        Rental_ret.add(l_name2);

        JLabel last_name2 = new JLabel("Last Name: ");
        last_name2.setBounds(15, 77, 69, 16);
        Rental_ret.add(last_name2);

        Age2 = new JTextField(2);
        Age2.setBounds(109, 109, 116, 22);
        Age2.setColumns(10);
        Rental_ret.add(Age2);

        JLabel liscense2 = new JLabel("License #: ");
        liscense2.setBounds(15, 141, 64, 16);
        Rental_ret.add(liscense2);


        JLabel issuing_st2 = new JLabel("Issuing State: ");
        issuing_st2.setBounds(15, 170, 83, 16);
        Rental_ret.add(issuing_st2);

        issuing2 = new JTextField(2);
        issuing2.setBounds(109, 167, 116, 22);
        issuing2.setColumns(10);
        Rental_ret.add(issuing2);

        liscense_num2 = new JTextField(13);
        liscense_num2.setBounds(109, 138, 116, 22);
        liscense_num2.setColumns(10);
        Rental_ret.add(liscense_num2);


        car_select2 = new JComboBox<Object>(carStrings);
        car_select2.setBounds(109, 197, 116, 22);
        Rental_ret.add(car_select2);

        JLabel car_type2 = new JLabel("Car Type:");
        car_type2.setBounds(15, 199, 57, 16);
        Rental_ret.add(car_type2);

        JLabel rent_date2 = new JLabel("Rental Date:");
        rent_date2.setBounds(15, 234, 71, 16);
        Rental_ret.add(rent_date2);

        JTextField rental2 = new JTextField(10);
        rental2.setBounds(109, 231, 116, 22);
        rental2.setColumns(10);
        Rental_ret.add(rental2);

        JLabel return_date2 = new JLabel("Return Date:");
        return_date2.setBounds(15, 269, 73, 16);
        Rental_ret.add(return_date2);

        JTextField return_d2 = new JTextField(10);
        return_d2.setColumns(10);
        return_d2.setBounds(109, 266, 116, 22);
        Rental_ret.add(return_d2);

        JLabel total_num2 = new JLabel("Total:");
        total_num2.setBounds(15, 298, 34, 16);
        Rental_ret.add(total_num2);

        JTextField total2 = new JTextField(7);
        total2.setColumns(10);
        total2.setBounds(109, 295, 116, 22);
        Rental_ret.add(total2);

        JLabel payment_type2 = new JLabel("Payment Type:");
        payment_type2.setBounds(15, 332, 86, 16);
        Rental_ret.add(payment_type2);

        cc_button2 = new JRadioButton("Credit Card");
        cc_button2.setBounds(109, 328, 93, 25);
        Rental_ret.add(cc_button2);

        debit_button2 = new JRadioButton("Debit");
        debit_button2.setBounds(220, 326, 57, 25);
        Rental_ret.add(debit_button2);

        ButtonGroup group2 = new ButtonGroup();
        group2.add(debit_button);
        group2.add(cc_button);

        JLabel age2 = new JLabel("Age: ");
        age2.setBounds(15, 112, 31, 16);
        Rental_ret.add(age2);

        Update_button = new Button("Update");
        Update_button.setBounds(15, 368, 83, 24);
        Rental_ret.add(Update_button);
        Update_button.addActionListener(this);

        JLabel Agreement = new JLabel("Agreement ID: ");
        Agreement.setBounds(15, 13, 93, 16);
        Rental_ret.add(Agreement);

        agreement_id = new JTextField();
        agreement_id.setColumns(10);
        agreement_id.setBounds(109, 10, 116, 22);
        Rental_ret.add(agreement_id);

        return_car = new Button("Return Car");
        return_car.setBounds(109, 368, 83, 24);
        Rental_ret.add(return_car);
        return_car.addActionListener(this);
    }



    public void actionPerformed(ActionEvent action) {

              if(action.getSource() == Submit_button){
                  try {
                      myStatement.executeUpdate("INSERT INTO Customer (FNAME, LNAME, AGE, LICNUM, STATE, CAR_TYPE, RENTDATE, RETURNDATE, TOTAL, PAYTYPE, RETURNED) VALUES('chen','lee', 40,'S633-433-83-400-0', 'FL', 'C', '2016-04-07', '2016-04-10', 213.47, 'Credit', true)");
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


              }
              if(action.getSource() == load_button);
              if(action.getSource() == Clear_button);
              if(action.getSource() == return_car);
              if(action.getSource() == Update_button);


    }
}

您的数据库中需要有Customers表。 您的代码插入但不创建表。

有必要通过您的代码或直接在您的 DBMS 中创建表。

例子:

try {
    c = DriverManager.getConnection("jdbc...", "user", "pass");
    String com = "CREATE TABLE Customers(col1 VARCHAR(20) PRIMARY KEY, coln VARCHAR(50) NOT NULL);";
    PreparedStatement st = c.prepareStatement(com);
    st.execute();
    st.close();
    System.out.println("OK!");
}
catch (Exception e) {
    e.printStackTrace();
}

其中cConnection

好的,我在 APP 下创建了表。 但是我的 INSERT 语句不起作用,即使格式正确并且查询创建表从程序工作。 Submit 按钮有动作监听器,所以这不是问题。 为什么这个语句不起作用?

try { myStatement.execute("创建表 Customer (AgreementID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1000, INCREMENT BY 1), " + "FNAME VARCHAR(25) NOT NULL, LNAME VARCHAR(25) NOT NULL, AGE INTEGER NOT NULL, " + "LICNUM VARCHAR(20) NOT NULL, STATE VARCHAR(2) NOT NULL," + "CAR_TYPE VARCHAR(12) NOT NULL, RENTDATE VARCHAR(25) NOT NULL, RETURNDATE VARCHAR(25) NOT NULL, TOTAL DECIMAL(7,2) NOT NULL, " + "PAYTYPE VARCHAR(20) NOT NULL, RETURNED BOOLEAN, 约束 customer_id_pk 主键 (AgreementID))"); } catch (SQLException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); }

try {
        myStatement.execute("create table Customer (AgreementID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1000, INCREMENT BY 1),  "
                + "FNAME VARCHAR(25) NOT NULL,  LNAME VARCHAR(25) NOT NULL, AGE INTEGER NOT NULL, "
                + "LICNUM VARCHAR(20) NOT NULL,  STATE VARCHAR(2) NOT NULL, "
                + "CAR_TYPE VARCHAR(12) NOT NULL, RENTDATE VARCHAR(25) NOT NULL,  RETURNDATE VARCHAR(25) NOT NULL,  TOTAL DECIMAL(7,2) NOT NULL,  "
                + "PAYTYPE VARCHAR(20) NOT NULL, RETURNED BOOLEAN, constraint customer_id_pk primary key (AgreementID))");
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM