图书管理系统 日期处理问题
我目前在做一个图书管理系统,但是在增加书籍那个功能里遇到了问题,首先就是日期,包括书籍出版日期和书籍添加进图书馆的日期,这两个都是在输入框中输入的,也就是字符串类型,要怎样转成Date类型的?是哪个包中的Date类型?util包还是sql包?转换好之后,那个插入语句怎么写?也就是int affectedRow=sta.executeUpdate(str);的str怎么写?下面是这个功能的源码,包括UI设计和事务处理
package com.farsigt.bookmanagesystemUI;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import com.farsigt.workAction.addbookWorkAction;
public class addBookUI extends JPanel implements ActionListener{
private JLabel bookNameLabel,authorLabel,publishdateLabel,
priceLabel,adddateLabel,bookTypeLabel,publishCompanyLabel;
private JTextField bookNameField,authorField,publishdateField,
priceField,adddateField,publishCompanyField;
private JButton registeButton,cancelButton;
private JComboBox bookTypeComboBox;
private DateFormat df;
private Date publishdate,adddate;
public addBookUI() {
df = new SimpleDateFormat("yyyy-MM-dd");
bookNameLabel = new JLabel("书名");
authorLabel = new JLabel("作者");
publishCompanyLabel = new JLabel("出版社");
publishdateLabel = new JLabel("出版日期");
priceLabel = new JLabel("价格");
adddateLabel = new JLabel("添加时间");
bookNameField = new JTextField(10);
authorField = new JTextField(10);
publishdateField = new JTextField(10);
priceField = new JTextField(10);
adddateField = new JTextField(10);
publishCompanyField = new JTextField(10);
bookTypeLabel = new JLabel("书籍类型");
String[] str = {"请选择书籍类别","文学","计算机科学","农业","经济金融","理学","武功秘籍","菜谱","体育学","杂志"};
bookTypeComboBox = new JComboBox(str);
setLayout(new FlowLayout());
add(bookNameLabel);
add(bookNameField);
add(authorLabel);
add(authorField);
add(publishdateLabel);
add(publishdateField);
add(priceLabel);
add(priceField);
add(adddateLabel);
add(adddateField);
add(publishCompanyLabel);
add(publishCompanyField);
add(bookTypeLabel);
add(bookTypeComboBox);
registeButton = new JButton("提交");
cancelButton = new JButton("取消");
adddateField.setText(df.format(new Date()));
add(registeButton);
add(cancelButton);
registeButton.addActionListener(this);
cancelButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == "提交") {
if(bookNameField.getText().isEmpty() && authorField.getText().isEmpty() &&
publishdateField.getText().isEmpty() && priceField.getText().isEmpty() &&
adddateField.getText().isEmpty() && publishCompanyField.getText().isEmpty()) {
JOptionPane.showMessageDialog(null,"请输入要添加书籍的各种信息!");
}else {
try {
adddate = df.parse(adddateField.getText());
publishdate = df.parse(publishdateField.getText());
addbookWorkAction.addbook(bookNameField,authorField,publishdate,
priceField,adddate,publishCompanyField,bookTypeComboBox.getSelectedItem().toString());
} catch (
ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}else {
setVisible(false);
}
}
}
后面是事务处理的代码
package com.farsigt.workAction;
import java.sql.*;
import javax.swing.*;
public class addbookWorkAction {
static Connection conn;
static Statement sta;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bookmanagesystem";
conn = DriverManager.getConnection(url,"root","oyf19900716");
sta = conn.createStatement();
} catch (
ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void addbook(JTextField bookNameField,
JTextField authorField, java.util.Date d,
JTextField priceField, java.util.Date adddate,
JTextField publishCompanyField,String bookTypeComboBox) {
// TODO Auto-generated method stub
String str = "insert into booktable values(null,'"+bookNameField.getText()+"','"
+authorField.getText()+"','"+ d +"','"+ adddate +"','"+publishCompanyField.getText()
+"','+Double.parseDouble(priceField.getText())+','"+bookTypeComboBox
+"'1,0);";
try {
int affectedRow=sta.executeUpdate(str);
JOptionPane.showMessageDialog(null, "增加信息成功!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {