📄 storage2.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
public class Storage2 extends JFrame {
private JPanel panel1,panel2,panel3;
private JLabel label1,label2,label3,label4,label5,label6,label7,label8,label9,label10;
private JTextField textField1,textField2,textField3,textField4,textField5,textField6;
private JButton button1,button2,button3,button4,button5;
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTable table;
private String book_id,quantity,stor_addr,stor_date_year,stor_date_month,stor_date_day,stor_date,url;
private int operation = 0;
private Connection connection;
private Statement statement;
public Storage2()
{
super( "库存管理" );
table = new JTable();
container = getContentPane();
layout = new GridBagLayout();
constraints = new GridBagConstraints();
panel1 = new JPanel();
panel1.setLayout( layout );
/* panel2 = new JPanel();
panel2.add( table );
panel2.setLayout( new FlowLayout() );
panel3 = new JPanel();
panel3.setLayout( layout );
*/
label1 = new JLabel( "编号" );
label2 = new JLabel( "数量" );
label3 = new JLabel( "地址" );
label4 = new JLabel( "日期" );
label5 = new JLabel( "年" );
label6 = new JLabel( "月" );
label7 = new JLabel( "日" );
textField1 = new JTextField( 24 );
textField2 = new JTextField( 24 );
textField3 = new JTextField( 24 );
textField4 = new JTextField( 8 );
textField5 = new JTextField( 4 );
textField6 = new JTextField( 4 );
button1 = new JButton( "查询" );
button2 = new JButton( "入库" );
button3 = new JButton( "出库" );
button4 = new JButton( "确定" );
button5 = new JButton( "退出" );
// constraints.fill = GridBagConstraints.BOTH;
// constraints.weightx = 1000;
// constraints.weighty = 0;
addComponent( label1, 1, 1, 0, 3, 1 );
addComponent( label2, 1, 2, 0, 3, 1 );
addComponent( label3, 1, 3, 0, 3, 1 );
addComponent( label4, 1, 4, 0, 3, 1 );
addComponent( label5, 1, 4, 7, 1, 1 );
addComponent( label6, 1, 4, 10, 1, 1 );
addComponent( label7, 1, 4, 13, 1, 1 );
addComponent( textField1, 1, 1, 3, 12, 1 );
addComponent( textField2, 1, 2, 3, 12, 1 );
addComponent( textField3, 1, 3, 3, 12, 1 );
addComponent( textField4, 1, 4, 3, 4, 1 );
addComponent( textField5, 1, 4, 8, 2, 1 );
addComponent( textField6, 1, 4, 11, 2, 1 );
addComponent( button1, 1, 0, 0, 5, 1 );
addComponent( button2, 1, 0, 5, 5, 1 );
addComponent( button3, 1, 0, 10, 5, 1 );
addComponent( button4, 1, 5, 5, 5, 2 );
container.add( panel1, BorderLayout.NORTH );
// container.add( table, BorderLayout.CENTER );
// container.add( new JScrollPane( table ) );
// addComponent( button5, 3, 3, 0, 3, 1 );
container.add( button5, BorderLayout.SOUTH );
// container.add( panel3, BorderLayout.SOUTH );
button1.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
operation = 1;
textField1.setEditable( true );
textField2.setEditable( false );
textField3.setEditable( true );
textField4.setEditable( false );
textField5.setEditable( false );
textField6.setEditable( false );
}
}
);
button2.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
operation = 2;
textField1.setEditable( true );
textField2.setEditable( true );
textField3.setEditable( true );
textField4.setEditable( true );
textField5.setEditable( true );
textField6.setEditable( true );
}
}
);
button3.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
operation = 3;
textField1.setEditable( true );
textField2.setEditable( true );
textField3.setEditable( true );
textField4.setEditable( true );
textField5.setEditable( true );
textField6.setEditable( true );
}
}
);
button4.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
book_id = textField1.getText();
quantity = textField2.getText();
stor_addr = textField3.getText();
stor_date_year = textField4.getText();
stor_date_month = textField5.getText();
stor_date_day = textField6.getText();
stor_date = stor_date_year + "/" + stor_date_month + "/" + stor_date_day;
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
url = "jdbc:odbc:myaccess";
connection = DriverManager.getConnection( url );
statement = connection.createStatement();
}
catch( Exception ex ){
System.out.println( ex );
}
if( operation == 0 ){
JOptionPane.showMessageDialog( null, "请先选择操作类型" );
try{
connection.close();
}
catch( Exception ex ){
System.out.println( ex );
}
}
if( operation == 1 ){
try{
String sql = "Select S.Book_id, S.Quantity, S.Stor_addr, S.Stor_date, S.Stor_type, B.Storage_quantity from Storage_info1 S, Book_info B where S.Book_id = B.Book_id";
if( book_id.compareTo( "" ) != 0 )
sql += " and S.Book_id = '" + book_id + "'";
if( stor_addr.compareTo( "" ) != 0 )
sql += " and S.Stor_addr = '" + stor_addr + "'";
// System.out.println( sql );
ResultSet rs = statement.executeQuery( sql );
boolean moreRecords = rs.next(); // 定位到第一条记录
if ( !moreRecords ){
JOptionPane.showMessageDialog( null,"对不起,没有你要找的!" );
}
else{
Vector columnHeads = new Vector();
Vector rows = new Vector();
ResultSetMetaData rsmd = rs.getMetaData();
for( int i = 1; i <= rsmd.getColumnCount(); i++ ){
columnHeads.addElement( rsmd.getColumnName( i ) );
}
do{
rows.addElement( getNextRow( rs, rsmd ) );
}while( rs.next() );
table = new JTable( rows, columnHeads );
}
container.add( table, BorderLayout.CENTER );
container.add( new JScrollPane( table ) );
container.validate();
rs.close();
connection.close();
}
catch( Exception ex ){
System.out.println( ex );
}
}
if( operation == 2 ){
if( book_id.compareTo( "" ) == 0 ||
quantity.compareTo( "" ) == 0 ||
stor_addr.compareTo( "" ) == 0 ||
stor_date_year.compareTo( "") == 0 ||
stor_date_month.compareTo( "") == 0 ||
stor_date_day.compareTo( "" ) == 0 )
JOptionPane.showMessageDialog( null, "信息不完整" );
else{
try{
String sql = "Select Storage_quantity from Book_info where Book_id = '" + book_id + "'";
ResultSet rs = statement.executeQuery( sql );
if( rs.next() ){
int storage_quantity = rs.getInt( "Storage_quantity" );
storage_quantity += Integer.parseInt( quantity );
String sql1 = "Insert into Storage_info1( Book_id, Quantity, Stor_addr, Stor_date, Stor_type ) Values('"
+ book_id + "','" + quantity + "','" + stor_addr + "','" + stor_date + "','In' )";
statement.executeUpdate( sql1 );
System.out.println( sql1 );
String sql2 = "Update Book_info set Storage_quantity = " + storage_quantity + " where Book_id = '"
+ book_id + "'";
statement.executeUpdate( sql2 );
JOptionPane.showMessageDialog( null, "入库成功" );
textField1.setText( null );
textField2.setText( null );
}
else {
Information.main( null );
JOptionPane.showMessageDialog( null, "此书为新书,请先输入书籍信息" );
}
connection.close();
}
catch( Exception ex ){
System.out.println( ex );
JOptionPane.showMessageDialog( null,"入库失败" );
}
}
}
if( operation == 3 ){
if( book_id.compareTo( "" ) == 0 ||
quantity.compareTo( "" ) == 0 ||
stor_addr.compareTo( "" ) == 0 ||
stor_date_year.compareTo( "") == 0 ||
stor_date_month.compareTo( "") == 0 ||
stor_date_day.compareTo( "" ) == 0 )
JOptionPane.showMessageDialog( null, "信息不完整" );
else{
try{
String sql = "Select Storage_quantity from Book_info where Book_id = '" + book_id + "'";
ResultSet rs = statement.executeQuery( sql );
if( rs.next() ){
int storage_quantity = rs.getInt( "Storage_quantity" );
storage_quantity -= Integer.parseInt( quantity );
String sql1 = "Insert into Storage_info1( Book_id, Quantity, Stor_addr, Stor_date, Stor_type ) Values('"
+ book_id + "','" + quantity + "','" + stor_addr + "','" + stor_date + "','Out' )";
statement.executeUpdate( sql1 );
String sql2 = "Update Book_info set Storage_quantity = " + storage_quantity + " where Book_id = '"
+ book_id + "'";
statement.executeUpdate( sql2 );
JOptionPane.showMessageDialog( null, "出库成功" );
textField1.setText( null );
textField2.setText( null );
}
else {
JOptionPane.showMessageDialog( null, "没有这本书" );
}
connection.close();
}
catch( Exception ex ){
System.out.println( ex );
JOptionPane.showMessageDialog( null,"出库失败" );
}
}
}
}
}
);
button5.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
Storage2.this.setVisible( false );
}
}
);
setSize( 500,500 );
setVisible( true );
}
private Vector getNextRow( ResultSet rs,ResultSetMetaData rsmd ) throws SQLException{
Vector currentRow = new Vector();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
currentRow.addElement( rs.getString( i ) );
return currentRow; //返回一条记录
}
private void addComponent( Component component, int panel , int row,
int column, int width, int height )
{
constraints.gridx = column;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints( component, constraints );
if( panel == 1 ) panel1.add( component );
if( panel == 2 ) panel2.add( component );
if( panel == 3 ) panel3.add( component );
}
public static void main( String args[] )
{
Storage2 application = new Storage2();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -