java数据库的实现

xml 同时被 2 个专栏收录
964 篇文章 0 订阅
779 篇文章 0 订阅
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

  此程序是用java 通过JDBC—DOBC桥利用Access作后台制作的一个图书馆管理系统

  表格:

  BookInfo(书籍信息)

  ISBN VARCHAR 20 NOT NULL PK

  Title: VARCHAR 30

  Publisher VARCHAR 30

  Author VARCHAR 20

  IsLend VARCHAR 4

  Reader(读者信息)

  ReadID VARCHAR 15 NOT NULL PK

  Name VARCHAR 20

  Class VARCHAR 40

  BorrowInfo(借阅信息)

  BorrowID VARCHAR 20 NOT NULL PK

  ReadID VARCHAR 20 NOT NULL

  ISBN VARCHAR 20 NOT NULL

  BorrowDate VARCHAR 20 NOT NULL

  ReturnDate VARCHAR 20 NOT NULL

  源代码如下:

  *****************表格类********************

  //表格的布局

  package Table;

  import java.awt.*;

  import java.awt.event.*;

  import javax.swing.*;

  public class Table extends JPanel

  {

  private JPanel labelPanel, fieldsPanel;//定义两块面板对象

  //labelPanel用来放标签,fieldsPanel用来放文本区

  private String labels[] = //标签名

  { "* ISBN:", "名称:", "作者:", "出版社:", /*"借出情况:",*/ //access table1

  "读者编号:", "姓名:", "单位","借出日期","还书日期"};

  JTextField ISBN, title, author, publisher,/*isLend,*/readerID,name,workPlace,

  borrowDate, returnDate;

  public Table()

  {

  labelPanel = new JPanel();

  labelPanel.setLayout( //指定布局管理器

  new GridLayout( labels.length, 1 ) );

  ImageIcon ii = new ImageIcon( "images/icon.jpg" );

  for ( int i = 0; 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 ( password.compareToIgnoreCase ( 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 );

  fontColorItem = new JMenuItem ( "Font Color" );

  fontColorItem.addActionListener ( this );

  musicItem = new JMenuItem ( "音乐" );

  musicItem.addActionListener ( this );

  stopItem = new JMenuItem ( "暂停" );

  stopItem.addActionListener ( this );

  optionMenu.add ( musicItem );

  optionMenu.add ( stopItem );

  optionMenu.add ( backgroundColorItem );

  optionMenu.add ( fontColorItem );

  backgroundColorItem.setEnabled ( false );

  fontColorItem.setEnabled ( false );

  //musicItem.setEnabled ( false );

  bar.add ( optionMenu );

  /*---------------------------------Help------------------------------*/

  helpMenu = new JMenu ( "帮助" );

  helpMenu.setMnemonic ( 'H' );

  aboutItem = new JMenuItem ( "关于" );

  aboutItem.addActionListener ( this );

  useDisItem = new JMenuItem ( "使用说明" );

  useDisItem.addActionListener ( this );

  helpMenu.add ( useDisItem );

  helpMenu.add ( aboutItem );

  bar.add ( helpMenu );

  /*-------------------------------End Menu---------------------------*/

  /*-------------------- Set up database connection --------------------*/

  try

  {

  url = "jdbc:odbc:Books"; //指定了帮助程序(在本地文件系统或网络上)定位数据库数据库URL

  //String username = "anonymouse";//指定了用户登录的用户名和口令

  //String userpassword = "guest";

  Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );//装入了数据库驱动程序的类定义

  connect = DriverManager.getConnection( url );//尝试与URL指定的数据库的连接

  }

  catch ( ClassNotFoundException cnfex ) //若不成功抛出异常

  {

  // process ClassNotFoundExceptions here

  cnfex.printStackTrace();

  }

  catch ( SQLException sqlex )

  {

  // process SQLExceptions here

  sqlex.printStackTrace();

  }

  catch ( Exception ex )

  {

  //process remaining Exceptions here

  ex.printStackTrace();

  }

  setSize( 530, 500 );//设置显示的区域

  setLocation ( 120 , 35 );

  AudioPlayer.player.start ( asWin );

  show();//显示

  }//end_constructure function

  public void actionPerformed ( ActionEvent e )

  {

  /*--------------------------file menu-----------------------*/

  if ( e.getActionCommand ( ) == "退出" )

  {

  int option = 0;

  option = JOptionPane.showOptionDialog( null ,//询问是否真的退出

  "Are yout sure to exit?",

  "Exit",

  JOptionPane.YES_NO_OPTION,

  JOptionPane.WARNING_MESSAGE,null,null,null );

  //

  //YES_OPTION RETURN 0 , NO_OPTION RETURN 1

  if ( option == JOptionPane.YES_OPTION )

  System.exit ( 0 );

  }

  else if ( e.getActionCommand ( ) == "权限密码" )//升级到管理员权限

  {

  String grant = JOptionPane.showInputDialog ( "权限密码:" );

  if ( grant.compareToIgnoreCase ( comp ) == 0 )

  {

  updateItem.setEnabled ( true );

  insertItem.setEnabled ( true );

  deleteItem.setEnabled ( true );

  borrowItem.setEnabled ( true );

  returnItem.setEnabled ( true );

  advancedItem.setEnabled ( true );

  }//end_if

  }//end_grant

  else if ( e.getActionCommand ( ) == "复位" )//Reset清空表格

  {

  clear();

  }

  /*---------------------------------option menu-------------------------------*/

  /*

  else if ( e.getActionCommand ( ) == "Background Color" )

  {

  color = JColorChooser.showDialog ( BooksManager.this,

  "Choose a color" , color );

  c.setBackground( color );

  c.repaint();

  }

  else if ( e.getActionCommand ( ) == "Font Color" )

  {

  }

  else if ( e.getActionCommand ( ) == "Music" )

  {

  //audio.play ( );

  //play ( codebase ,"hua.mp3" );

  }//end_music

  */

  /*-------------------------------control------------------------------*/

  else if ( e.getActionCommand ( ) == "借阅查询" )//查询所有记录

  {

  boolean record = false;//是否有前一个记录,初始无

  try

  {

  statement = connect.createStatement ( );//实现了接口的对象,

  //现在可以使用statement进行查询

  ///--------------------------extend---------------------------------

  if ( tableArea.ISBN.getText().equals( "" )&&

  tableArea.title.getText().equals( "" )&&

  tableArea.author.getText().equals( "" )&&

  tableArea.publisher.getText().equals( "" )

  )//若用户不输入,默认为查询所用信息

  {

  String query = "SELECT * FROM BookInfo";//查询指令

  resultSet = statement.executeQuery ( query );//执行查询指令

  //将结果保存在ResultSet对象中

  }//end_if

  else if ( tableArea.ISBN.getText().equals( "$" )||

  tableArea.title.getText().equals( "$" )||

  tableArea.publisher.getText().equals( "$" )||

  tableArea.author.getText().equals( "$" )

  )//一旦有$值,查找与之匹配的记录

  {//else_start

  String query = "SELECT DISTINCT ";//查询指令

  if ( tableArea.ISBN.getText().equals( "$" ) )

  {

  if ( record != true )

  {

  query += " ISBN ";

  record = true;

  }

  }

  //---------

  if ( tableArea.title.getText().equals( "$" ) )

  {

  if ( record != true )

  {

  query += " Title ";

  record = true;

  }

  else

  query += " ,Title ";

  }

  //------------------

  if ( tableArea.publisher.getText().equals( "$" ) )

  {

  if ( record != true )

  {

  query += " Publisher ";

  record = true;

  }

  else

  query += " ,Publisher ";

  }//---------

  if ( tableArea.author.getText().equals( "$" ) )

  {

  if ( record != true )

  {

  query += " Author ";

  record = true;

  }

  else

  query += " ,Author";

  }//---------

  query += " FROM BookInfo";

  resultSet = statement.executeQuery ( query );//执行查询指令

  //将结果保存在ResultSet对象中

  clear();

  }//end_first_else

  else //一旦有值,查找与之匹配的记录

  {//else_start

  String query = "SELECT * FROM BookInfo";//查询指令

  if ( ! tableArea.ISBN.getText().equals( "" ) )

  {

  if ( record != true )

  {

  query += " where ISBN LIKE '%"+tableArea.ISBN.getText()+"%' ";

  record = true;

  }

  }

  //----------

  if ( ! tableArea.title.getText().equals( "" ) )

  {

  if ( record != true )

  {

  query += " where Title LIKE '%"+tableArea.title.getText()+"%' ";

  record = true;

  }

  else

  query += " and Title LIKE '%"+tableArea.title.getText()+"%' ";

  }//---------

  if ( ! tableArea.publisher.getText().equals( "" ) )

  {

  if ( record != true )

  {

  query += " where Publisher LIKE '%"+tableArea.publisher.getText()+"%' ";

  record = true;

  }

  else

  query += " and Publisher LIKE '%"+tableArea.publisher.getText()+"%' ";

  }//---------

  if ( ! tableArea.author.getText().equals( "" ) )

  {

  if ( record != true )

  {

  query += " where Author LIKE '%"+tableArea.author.getText()+"%' ";

  record = true;

  }

  else

  query += " and Author LIKE '%"+tableArea.author.getText()+"%' ";

  }//---------

  resultSet = statement.executeQuery ( query );//执行查询指令

  //将结果保存在ResultSet对象中

  }//end_else

  displayResult ( resultSet );//调用显示函数

  statement.close ( );//关闭数据库

  }//end_try

  catch ( SQLException sqlex )

  {

  sqlex.printStackTrace ( );

  }

  }//end_borrowfind

  else if ( e.getActionCommand ( ) == "读者查询" )

  {

  boolean record = false;//是否有前一个记录,初始无

  try

  {

  statement = connect.createStatement ( );//实现了接口的对象,

  //现在可以使用statement进行查询

  ///--------------------------extend---------------------------------

  if ( tableArea.readerID.getText().equals( "" )&&

  tableArea.name.getText().equals( "" )&&

  tableArea.workPlace.getText().equals( "" )

  )//若用户不输入,默认为查询所用信息

  {

  String query = "SELECT BorrowInfo.ReadID, BorrowInfo.ISBN, BorrowInfo.LendDate,"+

  " BorrowInfo.ReturnDate, Reader.Name, Reader.Class, BookInfo.Title"+

  " FROM Reader INNER JOIN (BookInfo INNER JOIN BorrowInfo "+

  " ON BookInfo.ISBN = BorrowInfo.ISBN) ON Reader.ReadID = BorrowInfo.ReadID";

  resultSet = statement.executeQuery ( query );//执行查询指令

  //将结果保存在ResultSet对象中

  }//end_if

  else if ( tableArea.readerID.getText().equals( "$" )||

  tableArea.name.getText().equals( "$" )||

  tableArea.workPlace.getText().equals( "$" )

  )//一旦有$值,查找与之匹配的记录

  {//else_start

  String query = "SELECT DISTINCT ";//查询指令

  if ( tableArea.readerID.getText().equals( "$" ) )

  {

  if ( record != true )

  {

  query += " ReadID ";

  record = true;

  }

  }

  //---------

  if ( tableArea.name.getText().equals( "$" ) )

  {

  if ( record != true )

  {

  query += " Name ";

  record = true;

  }

  else

  query += " ,Name ";

  }

  //------------------

  if ( tableArea.workPlace.getText().equals( "$" ) )

  {

  if ( record != true )

  {

  query += " Class ";

  record = true;

  }

  else

  query += " ,Class ";

  }//---------

  query += " FROM Reader";

  resultSet = statement.executeQuery ( query );//执行查询指令

  //将结果保存在ResultSet对象中

  clear();

  }//end_first_else

  else //一旦有值,查找与之匹配的记录

  {//else_start

  //String query = "SELECT ReadID,Name,Class,ISBN,Title FROM BookInfo,Reader";//查询指令

  String query = "SELECT * FROM Reader";//查询指令

  if ( ! tableArea.readerID.getText().equals( "" ) )

  {

  if ( record != true )

  {

  query += " where ReadID LIKE '%"+tableArea.readerID.getText()+"%' ";

  record = true;

  }

  }

  //----------

  if ( ! tableArea.name.getText().equals( "" ) )

  {

  if ( record != true )

  {

  query += " where Name LIKE '%"+tableArea.name.getText()+"%' ";

  record = true;

  }

  else

  query += " and Name LIKE '%"+tableArea.name.getText()+"%' ";

  }//---------

  if ( ! tableArea.workPlace.getText().equals( "" ) )

  {

  if ( record != true )

  {

  query += " where Class LIKE '%"+tableArea.workPlace.getText()+"%' ";

  record = true;

  }

  else

  query += " and Class LIKE '%"+tableArea.workPlace.getText()+"%' ";

  }//---------

  resultSet = statement.executeQuery ( query );//执行查询指令

  //将结果保存在ResultSet对象中

  }//end_else

  displayResult ( resultSet );//调用显示函数

  statement.close ( );//关闭数据库

  }//end_try

  catch ( SQLException sqlex )

  {

  sqlex.printStackTrace ( );

  }

  }//end_borrowfind

  /* else if ( e.getActionCommand ( ) == "更新" )//修改存在的记录

  {

  try

  {

  statement = connect.createStatement();

  String query = "UPDATE BookInfo SET IsLend='否' WHERE ISBN >='0'";

  boolean record = false;

  if ( ! tableArea.ISBN.getText().equals( "" ) )

  {

  if ( ! tableArea.title.getText().equals( "" ) )

  {

  if ( record != true )

  {

  query += " SET Title = '"+tableArea.title.getText()+"' ";

  record = true;

  }

  }

  //-----------------

  if ( ! tableArea.publisher.getText().equals( "" ) )

  {

  if ( record != true )

  {

  query += " SET Publisher = '"+tableArea.publisher.getText()+"' ";

  record = true;

  }

  else

  query += ", Publisher = '"+tableArea.publisher.getText()+"' ";

  }

  //--------------------

  if ( ! tableArea.author.getText().equals( "" ) )

  {

  if ( record != true )

  {

  query += " SET Author = '"+tableArea.author.getText()+"' ";

  record = true;

  }

  else

  query += ", Author = '"+tableArea.author.getText()+"' ";

  }

  //--------------------

  query +=" WHERE ISBN = '" +tableArea.ISBN.getText()+ "' ";

  int result = statement.executeUpdate( query );//executeUpdate返回一个整型值

  //执行成功为1,

  if ( result == 1 )

  { //清空表格

  clear();

  String Jquery = "SELECT * FROM BookInfo";//查询指令

  resultSet = statement.executeQuery ( Jquery );//执行查询指令

  //将结果保存在ResultSet对象中

  displayResult ( resultSet );

  }

  else

  JOptionPane.showMessageDialog ( null,

  "You may only update existing record",

  "Warning",

  JOptionPane.ERROR_MESSAGE );

  statement.close();//关闭连接

  // }//end_if

  }//end_try

  catch ( SQLException sqlex ) //抛出异常

  {

  JOptionPane.showMessageDialog ( null , "Can't connect ",

  "Warning",

  JOptionPane.ERROR_MESSAGE );

  sqlex.printStackTrace();

  }//end_catch

  }//end_update

  */

  else if ( e.getActionCommand ( ) == "插入书籍" )//插入操作

  {

  try

  {

  statement = connect.createStatement();

  //返回表格中的值,并存放在临时变量中

  String sisbn = tableArea.ISBN.getText();

  String stitle = tableArea.title.getText();

  String spublisher = tableArea.publisher.getText();

  String sauthor = tableArea.author.getText();

  if ( ! tableArea.ISBN.getText().equals( "" ) )

  {

  String query= "INSERT INTO BookInfo( ISBN, Title,Publisher,Author,IsLend )VALUES( '"+sisbn+"','"+stitle+"','"+spublisher+"','"+sauthor+"','否')";

  int result = statement.executeUpdate( query );//executeUpdate返回一个整型值

  //执行成功为1,

  if ( result == 1 )

  {

  clear ( );

  String Jquery = "SELECT * FROM BookInfo";//查询指令

  resultSet = statement.executeQuery ( Jquery );//执行查询指令

  //将结果保存在ResultSet对象中

  }//end_if

  }//end_if(s)

  else //如果ISBN无值

  {

  JOptionPane.showMessageDialog ( null , "You must input a value in the ISBN",

  "Warning",JOptionPane.INFORMATION_MESSAGE );

  displayResult ( resultSet );

  clear ( );

  }

  displayResult ( resultSet );

  statement.close();//关闭连接

  }//end_try

  catch ( SQLException sqlex )

  {

  JOptionPane.showMessageDialog ( null , "Can't connect /n Check you PK ",

  "Warning",

  JOptionPane.ERROR_MESSAGE );

  sqlex.printStackTrace();

  }

  } //end_insert

  else if ( e.getActionCommand ( ) == "删除书籍" )//删除操作

  {

  try

  {

  statement = connect.createStatement();

  //初步处理,只能输入主键值

  if ( !tableArea.ISBN.getText().equals( "" ) )

  {

  String query = "DELETE FROM BookInfo WHERE ISBN = '" +tableArea.ISBN.getText() +"' ";

  int result = statement.executeUpdate( query );

  if ( result == 1 )

  {

  clear ( );

  String Jquery = "SELECT * FROM BookInfo";//查询指令

  resultSet = statement.executeQuery ( Jquery );//执行查询指令

  //将结果保存在ResultSet对象中

  }//end _if ( result )

  else

  {

  JOptionPane.showMessageDialog ( null , "The values has been deleted /n or you input a wrong value /n Check it and reinput",

  "Warning",

  JOptionPane.INFORMATION_MESSAGE );

  clear ( );

  }

  displayResult ( resultSet );

  }//end first if

  else

  {

  JOptionPane.showMessageDialog ( null , "You must input a value in the ISBN /n or you input a wrong value /n Check it and reinput",

  "Warning",

  JOptionPane.INFORMATION_MESSAGE );

  }

  statement.close ( );

  }//end_try

  catch ( SQLException sqlex )

  {

  sqlex.printStackTrace();

  }

  }//end_delete

  else if ( e.getActionCommand ( ) == "借阅" )//借阅操作

  {

  try

  {

  statement = connect.createStatement();

  //返回表格中的值,并存放在临时变量中

  String sreaderID = tableArea.readerID.getText();

  String sname = tableArea.name.getText();

  String sclass = tableArea.workPlace.getText();

  String sisbn = tableArea.ISBN.getText();

  String sbordate = tableArea.borrowDate.getText();

  String sretdate = tableArea.returnDate.getText();

  if ( ! tableArea.readerID.getText().equals( "" )&&

  ! tableArea.ISBN.getText().equals( "" )&&

  ! tableArea.name.getText().equals( "" )&&

  ! tableArea.workPlace.getText().equals( "" )

  )

  {

  String query= "INSERT INTO Reader VALUES ( '"+sreaderID+"','"+sname+"','"+sclass+"')";

  int result = statement.executeUpdate( query );//executeUpdate返回一个整型值

  //执行成功为1,

  if ( result == 1 )

  {

  clear ( );

  String Uquery = "UPDATE BookInfo SET IsLend='是' WHERE ISBN='"+sisbn+"'";

  statement.executeUpdate( Uquery );

  String Iquery= "INSERT INTO BorrowInfo (ReadID,ISBN,LendDate,ReturnDate ) VALUES ( '"+sreaderID+"','"+sisbn+"','"+sbordate+"','"+sretdate+"')";

  statement.executeUpdate( Iquery );

  String Jquery = "SELECT * FROM BookInfo";//查询指令

  resultSet = statement.executeQuery ( Jquery );//执行查询指令

  //将结果保存在ResultSet对象中

  }//end_if

  }//end_if(s)

  else //如果readerID无值

  {

  JOptionPane.showMessageDialog ( null , "You must input a value in the 'readerID'",

  "Warning",JOptionPane.INFORMATION_MESSAGE );

  displayResult ( resultSet );

  clear ( );

  }

  displayResult ( resultSet );

  statement.close();//关闭连接

  }//end_try

  catch ( SQLException sqlex )

  {

  JOptionPane.showMessageDialog ( null , "Can't connect /n Check you PK ",

  "Warning",

  JOptionPane.ERROR_MESSAGE );

  sqlex.printStackTrace();

  }

  } //end_borrow

  //

  //

  else if ( e.getActionCommand ( ) == "归还" )//归还操作

  {

  try

  {

  statement = connect.createStatement ( );

  if ( ! tableArea.readerID.getText().equals( "" )&&

  ! tableArea.ISBN.getText().equals( "" )

  )

  {

  String Dquery = "DELETE FROM BorrowInfo WHERE ISBN = '" +tableArea.ISBN.getText() +"' ";

  int result = statement.executeUpdate( Dquery );

  String query = "DELETE FROM Reader WHERE ReadID = '"+tableArea.readerID.getText()+"'";

  result = statement.executeUpdate( query );

  String Uquery = "UPDATE BookInfo SET IsLend='否' WHERE ISBN='" +tableArea.ISBN.getText() +"' ";

  result = statement.executeUpdate( Uquery );

  if ( result == 1 )

  {

  clear ( );

  String Fquery = "SELECT * FROM BookInfo";

  resultSet = statement.executeQuery ( Fquery );

  displayResult ( resultSet );//显示结果

  statement.close ( );

  }//end_if

  }//end_if(s)

  else //如果readerID无值

  {

  JOptionPane.showMessageDialog ( null , "You must input a value in the 'readerID'",

  "Warning",JOptionPane.INFORMATION_MESSAGE );

  displayResult ( resultSet );

  clear ( );

  }

  //displayResult ( resultSet );

  statement.close();//关闭连接

  }//end_try

  catch ( SQLException sqlex )

  {

  JOptionPane.showMessageDialog ( null , "Can't connect /n Check you PK ",

  "Warning",

  JOptionPane.ERROR_MESSAGE );

  sqlex.printStackTrace();

  }

  } //end_borrow

 

  else if ( e.getActionCommand ( ) == "高级" )//高级用户操作

  //允许用户自己键入查询指令

  {

  try

  {

  statement = connect.createStatement ( );

  String Query = JOptionPane.showInputDialog ( "Please input the SQL" );

  //返回用户键入的查询指令

  ResultSet result = statement.executeQuery ( Query );

  displayResult ( result );

  statement.close ( );

  }

  catch ( SQLException sqlex )

  {

  JOptionPane.showMessageDialog ( null , "Unvailable syntax",

  "Warning",

  JOptionPane.ERROR_MESSAGE );

  sqlex.printStackTrace();

  }

  }//end_advanced

  /*-------------------Option-------------------------------*/

  else if ( e.getActionCommand ( ) == "音乐" )

  {

  if ( asDouble == null )

  {

  JOptionPane.showMessageDialog ( null , "Can't create",

  "Warning",

  JOptionPane.ERROR_MESSAGE );

  return;

  }

  else

  {

  AudioPlayer.player.start ( asDouble );

  }

  }//end_music

  else if ( e.getActionCommand ( ) == "暂停" )

  {

  AudioPlayer.player.stop ( asDouble );

  }//end_stop

  /*-------------------------------Help----------------------------*/

  else if ( e.getActionCommand ( ) == "关于" )//关于对话框

  {

  JOptionPane.showMessageDialog ( null , "LibraryManager 2.1 改良版 /n 信控学院 计算机五班 齐辉 24 /n E-mail: lionqi742@sohu.com",

  "JDBC-ODBC",

  JOptionPane.INFORMATION_MESSAGE );

  }//end_about

  else if ( e.getActionCommand ( ) == "使用说明" )// 使用说明对话框

  {

  String s = "欢迎使用本软件/n 查找:若不在文本中输入内容,则默认查找全部/n"+

  "若在查找文本框中输入$,查找该属性项/n"+

  "否则查找限定的内容。/n 输入管理员权限密码可执行高级功能/n"+

  "允许用户自己输入查询语句查找自己需要的信息/n"+

  " 插入和删除功能中必须键入ISBN,然后按照文本框中/n"+

  "指定的内容修改";

  JTextArea useDis = new JTextArea ( s, 10, 20 );

  JScrollPane use = new JScrollPane ( useDis );

  JOptionPane.showMessageDialog ( null ,useDis,

  "使用说明",

  JOptionPane.INFORMATION_MESSAGE );

  }//end_使用说明

  else if ( e.getActionCommand ( ) == "刷新" )//刷新,将删除、插入后的操作的结果显示在屏幕上

  {

  String query = "SELECT * FROM BookInfo";

  try

  {

  statement = connect.createStatement ( );

  resultSet = statement.executeQuery ( query );

  displayResult ( resultSet );//显示结果

  statement.close ( );

  }

  catch ( SQLException sqlex )

  {

  sqlex.printStackTrace ( );

  }

  }//end_refresh

  } //end_actionPerformed

  /*----------------------------------------------------------------------*/

  private void clear()

  {

  tableArea.ISBN.setText( "" );

  tableArea.title.setText( "" );

  tableArea.publisher.setText( "" );

  tableArea.author.setText( "" );

  tableArea.readerID.setText( "" );

  tableArea.name.setText( "" );

  tableArea.workPlace.setText( "" );

  tableArea.borrowDate.setText( "" );

  tableArea.returnDate.setText( "" );

  }

  /*---------------------------------Display Result----------------------*/

  private void displayResult ( ResultSet rs ) throws SQLException

  {

  boolean moreRecords = rs.next(); //定位于第一条记录

  if ( ! moreRecords ) //若无记录,显示对话框

  {

  JOptionPane.showMessageDialog( this,

  "ResultSet contained no records" );

  setTitle( "No records to display" );

  return;

  }

  Vector columnHeads = new Vector();//定义矢量数组存放属性

  Vector rows = new Vector();//定义矢量数组存放内容

  try

  { //该方法可获取ResultSet中关于列的名字和类型的信息

  ResultSetMetaData rsmd = rs.getMetaData();

  //循环查找记录

  for ( int i = 1; i

  columnHeads.addElement( rsmd.getColumnName( i ) );

  do //获得行数据

  {

  rows.addElement( getNextRow( rs, rsmd ) );

  } while ( rs.next() );

  output = new JTable ( rows, columnHeads );//将显示数据的output实例化

  textpane = new JScrollPane( output );

  c.remove( 1 );

  c.add( textpane , BorderLayout.CENTER );

  c.validate();

  }//end_try

  catch ( SQLException sqlex )

  {

  sqlex.printStackTrace();

  }

  }//end_displayResult

  /*-----------------------------getNextRow---------------------------------*/

  private Vector getNextRow( ResultSet rs, ResultSetMetaData rsmd )

  throws SQLException

  {

  Vector currentRow = new Vector();

  for ( int i = 1; i

  switch( rsmd.getColumnType( i ) ) //判断数据类型

  {

  case Types.VARCHAR:

  case Types.LONGVARCHAR:

  currentRow.addElement( rs.getString( i ) );

  break;

  case Types.INTEGER:

  currentRow.addElement(

  new Long( rs.getLong( i ) ) );

  break;

  default:

  System.out.println( "Type was: " +

  rsmd.getColumnTypeName( i ) );

  }//end_Switch

  return currentRow;//返回当前行

  }//end_getNextRow

  public static void main( String args[] )

  {

  LibManager app = new LibManager();

  app.addWindowListener(

  new WindowAdapter()

  {

  public void windowClosing( WindowEvent e )

  {

  System.exit( 0 );

  }

  }

  );

  }

  }

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值