⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 operations.java

📁 微型图书信息管理系统 基本实现所有图书信息管理的功能。
💻 JAVA
字号:
package pkg;


import javax.swing.*;
import java.io.*;
import java.util.*;
import java.sql.*;

class OPerations {
	
		Connection con=null;		
		Statement stmt=null;		
		ResultSet rs=null;
		String SQL ;
		String updateSQL;
		String URL="jdbc:odbc:图书信息管理";
	
		public OPerations(){
			
		}
		
		public boolean inits(boolean init){
					
				/*
				 *初始化
				 */
				init=true;
				try {
						//加载驱动程序
						Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
						}catch(ClassNotFoundException ex){
							System.exit(-1);
							String output = "加载失败!";
							JOptionPane.showMessageDialog(null,output,"ErrorMessage",
								JOptionPane.INFORMATION_MESSAGE);
							}
				String output = "加载数据库成功!";
				JOptionPane.showMessageDialog(null,output,"ErrorMessage",
						JOptionPane.INFORMATION_MESSAGE);	
				return init;
				}
			
			
		public void addup(Bookinfo Books){
				/*
				 *将新数据添加
				 */
				 Connection con=null;
				 try{
				 	con = DriverManager.getConnection("jdbc:odbc:图书信息管理");
				 	String SQL = "SELECT 图书编号,图书名,图书作者,图书分类,图书出版,图书状态 FROM 图书管理信息";
					Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
     				ResultSet rs = stmt.executeQuery(SQL);
     				//添加一个新的记录
     				rs.moveToInsertRow();
     				rs.updateInt("图书编号",Books.BookNum);
    				rs.updateString("图书名",Books.BookName);
     				rs.updateString("图书作者",Books.BookAuther);
     				rs.updateString("图书分类",Books.Classic);
     				rs.updateString("图书出版",Books.BookPub);
     				rs.updateBoolean("图书状态",Books.IsRent);
     				rs.insertRow();
     				
     				String SQLX = "SELECT 图书名 FROM 图书管理信息";
     				Statement stmtx = con.createStatement();
     				ResultSet rsx = stmtx.executeQuery(SQLX);
     				boolean f = false;
     				while(rsx.next()){
     					if(rsx.getString("图书名").equals(Books.BookName)){
     						f = true;
     						break;
     						}
     					}
     				if(f){
     					String output = "已成功添加";
						JOptionPane.showMessageDialog(null,output,"Message",
								JOptionPane.INFORMATION_MESSAGE);
						}
					else {
						String output = "成功失败!";
						JOptionPane.showMessageDialog(null,output,"Message",
								JOptionPane.INFORMATION_MESSAGE);
						}
					rsx.close();
					stmtx.close();
					rs.close();
					stmt.close();
					}
					catch(SQLException e){
						}
					finally{
						try{
							con.close();}
							catch(SQLException e){
								}
						}
				}
			


		public void Delete(Bookinfo Books){
				/*
				 *将新的数据添加进BookList
				 */
				Connection con=null;
				try {
					 con = DriverManager.getConnection("jdbc:odbc:图书信息管理");
					 //试图通过ODBC建立一个与学生管理数据库的连接
					 String SQL = "DELETE  FROM 图书管理信息"+" WHERE 图书编号=";
					 SQL += Books.BookNum;
					 Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
					 stmt.executeUpdate(SQL);
					 stmt.close();
					 String SQLS = "DELETE  FROM 图书管理信息"+" WHERE 图书名='";
					 SQLS += Books.BookName;
					 SQLS +="'";
					 Statement stmts = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
					 stmts.executeUpdate(SQLS);
					 stmts.close();
				/*	 String SQL = "SELECT 图书编号 FROM 图书管理信息";
					 Statement stmt = con.createStatement();
					 ResultSet rs = stmt.executeQuery(SQL);
					 boolean f = false;
					 while(rs.next()){//&&(rs.getString("图书名").equals(Books.BookName))){
					 	if(rs.getInt("图书编号")==Books.BookNum)
					 	f = true;
					 	}
					 if(f){
					 	rs.relative(0);
					 	rs.deleteRow();
					 	String output = "已成功删除数据";
					 	JOptionPane.showMessageDialog(null,output,"Message",
								JOptionPane.INFORMATION_MESSAGE);
						}
					else {
						String output = "查无此书 删除失败!";
						JOptionPane.showMessageDialog(null,output,"Message",
								JOptionPane.INFORMATION_MESSAGE);
						}*/
				//  rs.close();
				}
				catch(SQLException ex){
					}
					finally{
						try{
							con.close();}
							catch(SQLException ex){
								}
							}
			}

			
		public Bookinfo locateName(Bookinfo Books,Bookinfo chosebook)  {
				/*
				 *按书名查询并返回所查询到的图书信息
				 */
				Connection con=null;	
				try {
					 con = DriverManager.getConnection("jdbc:odbc:图书信息管理");
					 //试图通过ODBC建立一个与学生管理数据库的连接
					 //定义带参数的预编译语句
					 String SQL = "SELECT 图书编号,图书名,图书作者,图书分类,图书出版,图书状态 FROM 图书管理信息"
					 +" WHERE 图书名=?";
					 PreparedStatement preSt = con.prepareStatement(SQL);
					 //为参数指定值
					 preSt.setString(1,Books.BookName);
					 ResultSet rs=preSt.executeQuery();
					 //将结果集中的数据添加到文本框中
					 rs.next();
					 chosebook.BookNum = rs.getInt("图书编号");
					 chosebook.BookAuther = rs.getString("图书作者");
					 chosebook.BookName = rs.getString("图书名") ;
					 chosebook.BookPub = rs.getString("图书出版") ;
					 chosebook.Classic = rs.getString("图书分类");
					 chosebook.IsRent = rs.getBoolean("图书状态"); 
					 rs.close();
					 preSt.close();
			/*		 String SQLX = "SELECT 图书名 FROM 图书管理信息";
			 		 Statement stmtx = con.createStatement();
			 		 ResultSet rsx = stmtx.executeQuery(SQLX);
			 		 boolean f = false;
			 		 while(rsx.next()){
			 		 	if(rsx.getString("图书名").equals(Books.BookName)){
			 		 		f = true;
			 		 		break;
			 		 		}
			 		 	}
			 		 if(f){
			 		 	}
			 		 else {
			 		 	String output = "查无此书!";
			 		 	JOptionPane.showMessageDialog(null,output,"Message",
								JOptionPane.INFORMATION_MESSAGE);
						System.exit(0);
						}
					rsx.close();
					stmtx.close();
					
					*/
					}
					catch(SQLException ex){
						}
						finally{
							try{
								con.close();
								}
								catch(SQLException ex){
									}
					}
				return chosebook;
				}
			
			
			
		
		public Bookinfo locateNum(String booknum,Bookinfo chosebook){
				/*
				 *按编号查询并返回所查询到的图书信息
				 */
				 Connection con=null;
				 try {
					 con = DriverManager.getConnection("jdbc:odbc:图书信息管理");
					 //试图通过ODBC建立一个与学生管理数据库的连接
					 //定义带参数的预编译语句
					 String SQL = "SELECT 图书编号,图书名,图书作者,图书分类,图书出版,图书状态 FROM 图书管理信息"
					 	+" WHERE 图书编号=?";
					 PreparedStatement preSt = con.prepareStatement(SQL);
					 //为参数指定值
					 preSt.setString(1,booknum);
					 ResultSet rs=preSt.executeQuery();
					 //将结果集中的数据添加到文本框中
					 rs.next();
					 chosebook.BookNum = rs.getInt("图书编号");
					 chosebook.BookAuther = rs.getString("图书作者");
					 chosebook.BookName = rs.getString("图书名") ;
					 chosebook.BookPub = rs.getString("图书出版") ;
					 chosebook.Classic = rs.getString("图书分类");
					 chosebook.IsRent = rs.getBoolean("图书状态"); 
					 rs.close();
					 preSt.close();
					 }
					 catch(SQLException ex){
					 	}
					 	finally{
					 		try{
					 			con.close();
					 			}
					 			catch(SQLException ex){
					 				}
					 }
			/*	String SQLX = "SELECT 图书名 FROM 图书管理信息";
			 	Statement stmtx = con.createStatement();
			 	ResultSet rsx = stmtx.executeQuery(SQLX);
			 	boolean f = false;
			 	while(rsx.next()){
			 		if(rsx.getString("图书名").equals(Books.BookName)){
			 			f = true;
			 			break;
			 			}
			 		}
			 	if(f){
			 		}
			 	else {
			 		String output = "查无此书!";
			 		JOptionPane.showMessageDialog(null,output,"Message",
								JOptionPane.INFORMATION_MESSAGE);
					System.exit(0);
					}
				rsx.close();
				stmtx.close();*/
				return chosebook;
				}
		
	
			
		public Bookinfo chBook(Bookinfo Books,Bookinfo chosebook){
				/*
				 *返回所选中的图书信息
				 */
				 Connection con=null;
				 try {
					 con = DriverManager.getConnection("jdbc:odbc:图书信息管理");
					 //试图通过ODBC建立一个与学生管理数据库的连接
					 //定义带参数的预编译语句
					 String SQL = "SELECT 图书编号,图书名,图书作者,图书分类,图书出版,图书状态 FROM 图书管理信息"
					 	+" WHERE 图书编号=?";
					 	String sql = "";
					 	PreparedStatement preSt = con.prepareStatement(SQL);
					 	//为参数指定值
					 	preSt.setString(1,sql+Books.BookNum);
					 	ResultSet rs=preSt.executeQuery();
					 	//将结果集中的数据添加到文本框中
					 	rs.next();
					 	chosebook.BookNum = rs.getInt("图书编号");
					 	chosebook.BookAuther = rs.getString("图书作者");
					 	chosebook.BookName = rs.getString("图书名") ;
					 	chosebook.BookPub = rs.getString("图书出版") ;
					 	chosebook.Classic = rs.getString("图书分类");
					 	chosebook.IsRent = rs.getBoolean("图书状态"); 
					 	rs.close();
					 	preSt.close();
					 	}
					 	catch(SQLException ex){
					 		}
					 		finally{
					 			try{
					 				con.close();
					 				}
					 				catch(SQLException ex){
					 					}
					}
			/*		String SQLX = "SELECT 图书名 FROM 图书管理信息";
			 		Statement stmtx = con.createStatement();
			 		ResultSet rsx = stmtx.executeQuery(SQLX);
			 		boolean f = false;
			 		while(rsx.next()){
			 			if(rsx.getString("图书名").equals(Books.BookName)){
			 				f = true;
			 				break;
			 				}
			 			}
			 		if(f){
			 			}
			 		else {
			 			String output = "查无此书!";
			 			JOptionPane.showMessageDialog(null,output,"Message",
								JOptionPane.INFORMATION_MESSAGE);
						System.exit(0);
						}
					rsx.close();
					stmtx.close();*/
					
					return chosebook;
					}
		
		public Bookinfo buttOK(boolean flagrent,Bookinfo chosebook){
			   /*
				*返回修改后的图书列表
				*/
				Connection con=null;
					
				String output = "已修改此书状态!";
				JOptionPane.showMessageDialog(null,output,"Message",
								JOptionPane.INFORMATION_MESSAGE);
		
				if(flagrent==true)	
					{
						try {
							con = DriverManager.getConnection("jdbc:odbc:图书信息管理");
							//试图通过ODBC建立一个与学生管理数据库的连接
							String SQL = "UPDATE 图书管理信息 "+"SET 图书状态=true"+" WHERE 图书编号=";
							SQL += chosebook.BookNum;
							Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
							stmt.executeUpdate(SQL);
							stmt.close();
							}
							catch(SQLException ex){
								}
							finally{
								try{
									con.close();
									}
									catch(SQLException ex){
										}
									}
							}		
				else {
					try {
						con = DriverManager.getConnection("jdbc:odbc:图书信息管理");
						//试图通过ODBC建立一个与学生管理数据库的连接
						String SQL = "UPDATE 图书管理信息 "+"SET 图书状态=false"+" WHERE 图书编号=";
						SQL += chosebook.BookNum;
						Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
						stmt.executeUpdate(SQL);
						stmt.close();
						}
						catch(SQLException ex){
							}
							finally{
								try{
									con.close();
									}
									catch(SQLException ex){
										}
									}
						}
				return chosebook;
				}
			
		public void aboutInfo(){
			/*
			 *版本信息
			 */
			String output = "计算机0411\n张聪 040421116\n(按钮停留稍许有提示使用方法)\nVersion 04";
				JOptionPane.showMessageDialog(null,output,"About小型图书管理系统",
								JOptionPane.INFORMATION_MESSAGE);
			}
			
		public void helpInfoFile(){
			/*
			 *File帮助信息
			 */
			String output = "加载并连接数据库:以加载初始化数据库\n";
				output += "关闭退出所有:退出系统";
					JOptionPane.showMessageDialog(null,output,"Message",
								JOptionPane.INFORMATION_MESSAGE);
			}
			
		public void helpInfoOperate(){
			/*
			 *Operate帮助信息
			 */
			String output = "添加:添加新图书\n删除:删除图书数据\n查询:开始图书信息查询\n";
				output += "选中:选中图书信息\n确定:确定修改图书状态";
					JOptionPane.showMessageDialog(null,output,"Message",
								JOptionPane.INFORMATION_MESSAGE);
			}
	}					

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -