爱心区 公益下载 公益文章 人在旅途 小博士乐园

>>首页 -> 公益文章 -> 技术文栏 -> java文栏 -> java基本文章 -> java数据库的实现



java数据库的实现


作者javasqllike 来源lovesqljava 加入时间:2005-9-20
摘要:
java数据库的实现
此程序是用Java 通过JDBC—DOBC桥利用database/access/ Access作后台制作的一个图书馆管理系统

表格:
BookInfo(书籍信息)
ISBN VARCHAR 20 NOT NULL PK

转载:转载请保留本信息,本文来自
http://www.51dibs.com/info/23997.htm

java数据库的实现
此程序是用Java 通过JDBC—DOBC桥利用database/access/ 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 < 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 ( 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 = "U