熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java數據庫的實現

2022-06-13   來源: Java核心技術 

  此程序是用Java 通過JDBC—DOBC橋利用Access作後台制作的一個圖書館管理系統
  
  表格:
  BookInfo(書籍信息)
  ISBN VARCHAR NOT NULL PK
  Title: VARCHAR
  Publisher VARCHAR
  Author VARCHAR
  IsLend VARCHAR
  
  Reader(讀者信息)
  ReadID VARCHAR NOT NULL PK
  Name VARCHAR
  Class VARCHAR
  
  BorrowInfo(借閱信息)
  BorrowID VARCHAR NOT NULL PK
  ReadID VARCHAR NOT NULL
  ISBN VARCHAR NOT NULL
  BorrowDate VARCHAR NOT NULL
  ReturnDate VARCHAR NOT NULL
  源代碼如下
  *****************表格類********************
  //表格的布局
  package Table;
  import javaawt*;
  import javaawtevent*;
  import javaxswing*;
  
  public class Table extends JPanel
  {
  private JPanel labelPanel fieldsPanel;//定義兩塊面板對象
  //labelPanel用來放標簽fieldsPanel用來放文本區
  private String labels[] = //標簽名
  { * ISBN: 名稱: 作者: 出版社: /*借出情況:*/ //access table
  讀者編號: 姓名: 單位借出日期還書日期};
  
  JTextField ISBN title author publisher/*isLend*/readerIDnameworkPlace
  borrowDate returnDate;
  
  public Table()
  {
  labelPanel = new JPanel();
  labelPanelsetLayout( //指定布局管理器
  new GridLayout( labelslength ) );
  
  ImageIcon ii = new ImageIcon( images/iconjpg );
  
  for ( int i = ; i < labels.length; i++ )//加載
  labelPanel.add( new JLabel( labels[ i ], ii, 0) );
  
  // TextField panel
  fieldsPanel = new JPanel();
  fieldsPanel.setLayout( new GridLayout( labels.length, 1 ) );
  
  ISBN = new JTextField( 20 );
  fieldsPanel.add( ISBN );
  
  title = new JTextField( 20 );
  fieldsPanel.add( title );
  
  author = new JTextField( 20 );
  fieldsPanel.add( author );
  
  publisher = new JTextField( 20 );
  fieldsPanel.add( publisher );
  
  readerID = new JTextField( 20 );
  fieldsPanel.add( readerID );
  
  name = new JTextField( 20 );
  fieldsPanel.add( name );
  
  workPlace = new JTextField( 20 );
  fieldsPanel.add( workPlace );
  
  borrowDate = new JTextField( 20 );
  fieldsPanel.add( borrowDate );
  
  returnDate = new JTextField( 20 );
  fieldsPanel.add( returnDate );
  
  setLayout( new GridLayout( 1, 2 ) );//兩塊面板在GridLayout中呈一行兩列
  add( labelPanel );
  add( fieldsPanel );
  }
  }
  ****************操作*********
  
  import Table;
  import java.sql.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import java.util.*;
  import sun.audio.*;
  import java.io.*;
  
  public class LibManager extends JFrame implements ActionListener
  {
  private Table tableArea;//調用Table類
  private JTable output; //顯示表的內容
  
  private String url; //指定了幫助程序(在本地文件系統或網絡上)定位數據庫的數據庫URL
  private Connection connect;//Connection對象管理java程序和數據庫的連接
  private JScrollPane textpane;//滾動區域對象
  private ResultSet resultSet;//保存結果
  private String password;//用戶登錄權限密碼
  private String comp = "manager";//制定密碼
  Statement statement ;//將向數據庫遞交查詢
  
  /*--------------------------Menu Component----------------------*/
  private JMenuBar bar;//菜單欄對象
  private JMenu fileMenu,controlMenu,optionMenu,helpMenu;//菜單對象
  private JMenuItem exitItem, grantItem; //菜單項對象
  private JMenuItem findItem1,findItem2,updateItem,insertItem,deleteItem;
  private JMenuItem borrowItem,returnItem,resetItem,refreshItem,advancedItem;
  
  private JMenuItem backgroundColorItem,fontColorItem,musicItem,stopItem;
  private JMenuItem useDisItem, aboutItem;
  
  private Container c = getContentPane();
  private Color color = Color.lightGray;
  private AudioStream asWin, asDouble;
  
  public LibManager()
  {
  super( "LibraryManager 2.0" );
  try
  {
  InputStream inDouble = new FileInputStream ( "絕代雙驕.wav" );
  asDouble = new AudioStream ( inDouble );
  InputStream inWin = new FileInputStream ( "Windows 登錄音.wav" );
  asWin = new AudioStream ( inWin );
  }
  catch (Exception e )
  {
  e.printStackTrace ( );
  }
  tableArea = new Table( );
  output = new JTable (0, 0);
  
  c.setLayout( new BorderLayout() );//布局管理器
  c.add( new JScrollPane( tableArea ),BorderLayout.NORTH );
  
  textpane = new JScrollPane( output );
  c.add( new JScrollPane( output ),
  BorderLayout.CENTER );
  
  
  /*---------------------------------------------------*/
  //加載菜單對象並注冊監聽
  /*--------------------------Menu-----------------------*/
  bar = new JMenuBar ( );
  setJMenuBar ( bar );
  /*----------------------File----------------------------*/
  fileMenu = new JMenu ( "系統設置" );
  
  grantItem = new JMenuItem ( "權限密碼" );
  grantItem.addActionListener ( this );
  
  exitItem = new JMenuItem ( "退出" );
  exitItem.addActionListener ( this );
  
  fileMenu.add ( grantItem );
  fileMenu.addSeparator ( );
  fileMenu.add ( exitItem );
  
  bar.add ( fileMenu );
  
  /*------------------------------------Control------------------------------*/
  controlMenu = new JMenu ( "功能" );
  
  findItem1 = new JMenuItem ( "借閱查詢" );
  findItem1.addActionListener ( this );
  
  findItem2 = new JMenuItem ( "讀者查詢" );
  findItem2.addActionListener ( this );
  
  updateItem = new JMenuItem ( "更新" );
  updateItem.addActionListener ( this );
  
  insertItem = new JMenuItem ( "插入書籍" );
  insertItem.addActionListener ( this );
  
  deleteItem = new JMenuItem ( "刪除書籍" );
  deleteItem.addActionListener ( this );
  
  borrowItem = new JMenuItem ( "借閱" );
  borrowItem.addActionListener ( this );
  
  returnItem = new JMenuItem ( "歸還" );
  returnItem.addActionListener ( this );
  
  resetItem = new JMenuItem ( "復位" );
  resetItem.addActionListener ( this );
  
  refreshItem = new JMenuItem ( "刷新" );
  refreshItem.addActionListener ( this );
  
  advancedItem = new JMenuItem ( "高級" );
  advancedItem.addActionListener ( this );
  /////////////////////////////////////////
  password = JOptionPane.showInputDialog ( "管理員權限密碼" );
  
  controlMenu.add ( findItem1 );
  controlMenu.add ( findItem2 );
  controlMenu.add ( updateItem );
  controlMenu.add ( insertItem );
  controlMenu.add ( deleteItem );
  controlMenu.add ( borrowItem );
  controlMenu.add ( returnItem );
  controlMenu.add ( resetItem );
  controlMenu.add ( refreshItem );
  controlMenu.addSeparator ( );
  controlMenu.add ( advancedItem );
  if ( pareToIgnoreCase ( comp ) != 0 )//判斷兩個字符串是否相等
  {
  updateItem.setEnabled ( false );
  insertItem.setEnabled ( false );
  deleteItem.setEnabled ( false );
  borrowItem.setEnabled ( false );
  returnItem.setEnabled ( false );
  advancedItem.setEnabled ( false );
  }//end_if
  bar.add ( controlMenu );
  
  /*------------------------------Option-------------------------------*/
  optionMenu = new JMenu ( "選項" );
  
  backgroundColorItem = new JMenuItem ( "Background Color" );
  backgroundColorItem.addActionListener ( this );<
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27077.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.