在java程序中插入不起作用的字符串
嗨,我有一个程序连接到一个嵌入式derby数据库,它应该引用数据库中的数据。我试图使用一个简单的INSERT语句来测试数据库,但它不起作用。请帮忙在java程序中插入不起作用的字符串,java,swing,derby,Java,Swing,Derby,嗨,我有一个程序连接到一个嵌入式derby数据库,它应该引用数据库中的数据。我试图使用一个简单的INSERT语句来测试数据库,但它不起作用。请帮忙 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 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 String com;
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.execute("INSERT INTO Customer (FNAME, LNAME, AGE, LICNUM, STATE, CAR_TYPE, RENTDATE, RETURNDATE, TOTAL, PAYTYPE, RETURNED) VALUES('Tony','Mahan', 56,'S622-400-55-400-0', 'AK', 'A', '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);
}
}
导入java.awt.BorderLayout;
导入java.awt.EventQueue;
导入java.awt.Label;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.border.EmptyBorder;
导入javax.swing.JTabbedPane;
导入javax.swing.JLabel;
导入javax.swing.JTextField;
导入javax.swing.ButtonGroup;
导入javax.swing.JComboBox;
导入javax.swing.JRadioButton;
导入java.awt.Button;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入javax.swing.JButton;
导入javax.swing.JList;
导入java.awt.List;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.sql.ResultSetMetaData;
公共类自动扩展javax.swing.JFrame实现ActionListener{
/**
*
*/
私有静态最终长serialVersionUID=-577709332727645215L;
私有JPanel内容窗格;
私有jtextfu名称字段;
私有JTextField l_名称;
专用JTextField f_name2;
私有JTextField l_name2;
私人领域年龄;
私人领域发行;
私有JTextField liscense_num;
私人租赁;
私有JTextField返回;
私有JTextField总数;
私有JTextField Age2;
私人JTextField发行2;
私人JTextField liscense_num2;
私人JTextField rental2;
私有JTextField返回_d2;
私有JTextField total2;
私人JTextField协议\u id;
私人按钮提交按钮;
专用按钮清除按钮;
专用JButton加载按钮;
私人按钮更新按钮;
私人按钮返回车;
私人JComboBox车选,车选2;
专用JRadioButton cc_按钮;
私人JRadioButton cc_按钮2;
专用JRadioButton借记按钮;
私人JRadioButton借记按钮2;
私有静态字符串url;
私有字符串com;
专用静态连接myConnection;
私人静态陈述;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args)抛出SQLException、ClassNotFoundException{
invokeLater(新的Runnable(){
公开募捐{
试一试{
forName(“org.apache.derby.jdbc.EmbeddedDriver”);
url=“jdbc:derby:MyDB;create=true”;
myConnection=DriverManager.getConnection(url);
myStatement=myConnection.createStatement();
自动帧=新自动();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共汽车(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100694497);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTabbedPane Auto_Pane=新的JTabbedPane(JTabbedPane.TOP);
自动窗格设置边界(5、5、422、432);
contentPane.add(自动窗格);
JPanel New_rent=新JPanel();
Auto_Pane.addTab(“新租金”,null,新租金,null);
新租金设置布局(空);
JLabel first_name=新JLabel(“名字:”);
第一个名字。挫折(12,8,84,16);
新租金。添加(名字);
f_name=新的JTextField(15);
f_name.setBounds(97,5,116,22);
新租金。添加(f_名称);
f_name.setColumns(10);
l_name=新的JTextField(15);
l_name.setBounds(97,40,116,22);
l_name.setColumns(10);
新租金。添加(l_名称);
JLabel last_name=新JLabel(“姓氏:”);
姓氏:挫折(12,43,84,16);
新租金。添加(姓氏);
JLabel年龄=新JLabel(“年龄:”);
年龄.挫折(12,76,84,16);
新租金加上(年龄);
年龄=新的JTextField(2);
年龄.挫折(97,75,35,22);
年龄.设置栏(10);
新租金加上(年龄);
JLabel liscense=新的JLabel(“许可证:”;
立根(12,113,84,16);
新增租金(liscense);
JLabel发布=新JLabel(“发布状态:”);
发行证券(12、146、84、16);
新租金。增加(签发);
发布=新的JTextField(2);
发行.退票(97、143、116、22);
发布。设置列(10);
新增租金(已发放);
liscense_num=新的JTextField(13);
liscense_num.setBounds(97110116,22);
liscense_num.setColumns(10);
新增租金(liscense_num);
String[]carStrings={“紧凑型”、“中型”、“标准型”、“全尺寸”、“SUV”、“迷你面包车”};
car_select=新JComboBox(carStrings);
车辆选择立根(97178112,22);
新租金。添加(车辆选择);
JLabel car_type=新JLabel(“car type:”);
轿厢型立根(12181,84,16);
新增租金。添加(汽车类型);
JLabel rent_date=新JLabel(“租赁日期”);
租金(12,216,84,16),;
新租金。添加(租金日期);
租金=新的JTextField(10);
租金.挫折(97213 116 22);
租赁.设置栏(10);
新增租金。添加(租金);
JLabel return_date=新JLabel(“返回日期:”);
返回日期(12,251,84,16);
新增租金。添加(归还日期);
return_d=新的JTextField(10);
回程退步(97248116,22);
返回设置列(10);
新租金。添加(退还);
JLabel total_num=新JLabel(“总计:”);
总进退次数(12283,84,16);
新增租金(合计);
myStatement.execute("INSERT INTO Customer (FNAME, LNAME, AGE, LICNUM, STATE, CAR_TYPE, RENTDATE, RETURNDATE, TOTAL, PAYTYPE, RETURNED) VALUES('Tony','Mahan', 56,'S622-400-55-400-0', 'AK', 'A', '2016-04-07', '2016-04-10', 213.47, 'Credit', true)");