📄 information.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
public class Information extends JFrame {
private JLabel label1,label2,label3,label4,label5,label6,label7,label8;
private JTextField textField1,textField2,textField3,textField4,textField5,textField6,textField7,textField8;
private JButton button1,button2,button3,button4,button5;
private JPanel panel1;
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private String book_id,book_name,author,publishment,publish_date,price,book_type;
private JTable table;
public Information()
{
super( "书籍信息管理" );
layout = new GridBagLayout();
table = new JTable();
container = getContentPane();
constraints = new GridBagConstraints();
panel1 = new JPanel();
panel1.setLayout( layout );
label1 = new JLabel( "编号" );
label2 = new JLabel( "书名" );
label3 = new JLabel( "作者" );
label4 = new JLabel( "出版社" );
label5 = new JLabel( "出版日期" );
label6 = new JLabel( "书价" );
label7 = new JLabel( "类别" );
// label8 = new JLabel( "销售量排行" );
textField1 = new JTextField( 24 );
textField2 = new JTextField( 24 );
textField3 = new JTextField( 24 );
textField4 = new JTextField( 24 );
textField5 = new JTextField( 24 );
textField6 = new JTextField( 24 );
textField7 = new JTextField( 24 );
// textField8 = new JTextField( 24 );
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, 0, 0, 4, 1 );
addComponent( label2, 1, 0, 4, 1 );
addComponent( label3, 2, 0, 4, 1 );
addComponent( label4, 3, 0, 4, 1 );
addComponent( label5, 4, 0, 4, 1 );
addComponent( label6, 5, 0, 4, 1 );
addComponent( label7, 6, 0, 4, 1 );
// addComponent( label8, 7, 0, 4, 1 );
addComponent( textField1, 0, 4, 12, 1 );
addComponent( textField2, 1, 4, 12, 1 );
addComponent( textField3, 2, 4, 12, 1 );
addComponent( textField4, 3, 4, 12, 1 );
addComponent( textField5, 4, 4, 12, 1 );
addComponent( textField6, 5, 4, 12, 1 );
addComponent( textField7, 6, 4, 12, 1 );
// addComponent( textField8, 7, 4, 12, 1 );
addComponent( button1, 8, 0, 3, 1 );
addComponent( button2, 8, 3, 3, 1 );
addComponent( button3, 8, 6, 3, 1 );
addComponent( button4, 8, 9, 3, 1 );
container.add( panel1, BorderLayout.NORTH );
// container.add( table, BorderLayout.CENTER );
container.add( button5, BorderLayout.SOUTH );
button1.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
}
}
);
button2.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
}
}
);
button3.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
}
}
);
button4.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
book_id = textField1.getText();
book_name = textField2.getText();
author = textField3.getText();
publishment = textField4.getText();
publish_date = textField5.getText();
price = textField6.getText();
book_type = textField7.getText();
int a = 0;
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
String url = "jdbc:odbc:myaccess";
Connection connection = DriverManager.getConnection( url );
Statement statement = connection.createStatement();
String sql = "Select Book_id, Book_name, Author, Publishment, Publish_date, Price, Book_type from Book_info ";
if( book_id.compareTo( "" ) != 0 ){
sql += " where ";
a = 1;
sql += "Book_id = '" + book_id + "'";
}
if( book_name.compareTo( "" ) != 0 ){
if( a == 1 ) sql += " and ";
else sql += " where ";
a = 1;
sql += "Book_name = '" + book_name + "'";
}
if( author.compareTo( "" ) != 0 ){
if( a == 1 ) sql += " and ";
else sql += " where ";
a = 1;
sql += "Author = '" + author + "'";
}
if( publishment.compareTo( "" ) != 0 ){
if( a == 1 ) sql += " and ";
else sql += " where ";
a = 1;
sql += "Publishment = '" + publishment + "'";
}
if( publish_date.compareTo( "" ) != 0 ){
if( a == 1 ) sql += " and ";
else sql += " where ";
a = 1;
sql += "Publish_date = #" + publish_date + "#";
}
if( price.compareTo( "" ) != 0 ){
if( a == 1 ) sql += " and ";
else sql += " where ";
a = 1;
sql += "Price = " + price ;
}
if( book_type.compareTo( "" ) != 0 ){
if( a == 1 ) sql += " and ";
else sql += " where ";
a = 1;
sql += "Book_type = '" + book_type + "'";
}
System.out.println( sql );
ResultSet rs = statement.executeQuery( sql );
boolean moreRecords = rs.next(); // 定位到第一条记录
if ( ! moreRecords ){
JOptionPane.showMessageDialog( null,"对不起,没有你要找的!" );
}
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 );
}
}
}
);
button5.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
Information.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 row,
int column, int width, int height )
{
constraints.gridx = column;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints( component, constraints );
panel1.add( component );
}
public static void main( String args[] )
{
Information application = new Information();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -