calculatorimpl.java

来自「图书馆的图书管理系统! 图书查询」· Java 代码 · 共 1,028 行 · 第 1/2 页

JAVA
1,028
字号
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.JOptionPane;

//CalculatorImpl.java extends UnicastRemoteObject
//Implementation
public class CalculatorImpl extends UnicastRemoteObject implements Calculator {

	// 这个实现必须有一个显式的构造函数,并且要抛出一个RemoteException异常
	public CalculatorImpl() throws java.rmi.RemoteException, SQLException {
		super();
	}

	public String[] searchbook(int[] a, String[] b, int[] c, int[] d)
			throws java.rmi.RemoteException, SQLException {
		String[] k=new String[4]; 
		String[] k1=new String[4]; 
		for(int i=0;i<4;i++)
		{
			if(a[i]==1)
			{
				k[i]="BookName";
			}
			if(a[i]==2)
			{
				k[i]="Author";
			}
			if(a[i]==3)
			{
				k[i]="BookID";
			}			
			if(a[i]==4)
			{
				k[i]="Publisher";
			}
			if(a[i]==5)
			{
				k[i]="Abstract";
			}
			if(a[i]==6)
			{
				k[i]="Class";
			}
		}
		for(int i=0;i<4;i++)
		{
			if(d[i]==1)
			{
				k1[i]="'%"+b[i]+"%'";
			}
			if(d[i]==2)
			{
				k1[i]="'%"+b[i]+"'";
			}
			if(d[i]==3)
			{
				k1[i]="'"+b[i]+"%'";
			}
			if(d[i]==4)
			{
				k1[i]="'"+b[i]+"'";
			}
		}
		for(int y=0;y<4;y++)
		{
		if(a[y]==3)
		{

				k1[y]=b[y];			
		
		}
		}
		int num = 0;
		for (int i = 0; i < 4; i++) {

			String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
			try {
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String url = "jdbc:odbc:lib";
			Connection con = DriverManager.getConnection(url);
			Statement s = con.createStatement();

			String str = "select* from book where " + k[i] + " like " + k1[i]
					+ "";
			ResultSet re = s.executeQuery(str);
			while(re.next())
			{
				num++;
			}
		}
		
		String[] rz=new String[(num*6)];
		for (int i = 0; i < (num*6); i++) 
		{
			rz[i]="错误";
		}
		int nu=0;

		for (int i = 0; i < 4; i++) {

			String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
			try {
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String url = "jdbc:odbc:lib";
			Connection con = DriverManager.getConnection(url);
			Statement s = con.createStatement();

			String str = "select* from book where " + k[i] + " like " + k1[i]
					+ "";
			ResultSet re = s.executeQuery(str);
			while(re.next())
			{
				for (int e = 0; e < 8; e++) {
					if(e==4)
						e=e+2;
					rz[nu] = (String) re.getString(e + 1);
					nu++;
				}
			}
		}
		

		return rz;

	}

	public String[] searchreader() throws java.rmi.RemoteException,
			SQLException {
		String list[];

		int i, index;

		String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
		try {
			Class.forName(driver);
		} catch (Exception e) {
			System.out.println(e.toString());
			// e.printStackTrace();
		}

		String url = "jdbc:odbc:lib";
		Connection con = DriverManager.getConnection(url);
		Statement s = con.createStatement();
		String str = "select* from reader";
		ResultSet re = s.executeQuery(str);
		Vector<String> hmh = new Vector<String>(4);
		while (re.next()) {
			hmh.addElement(re.getString(1));
		}
		i = hmh.size();
		list = new String[i];
		for (index = 0; index < i; index++)
			list[index] = (String) hmh.elementAt(index);
		// for(index=0;index<i;index++)
		// System.out.println(张迪[index]);
		con.close();
		s.close();

		return list;
	}

	public String searchreader(int a) throws java.rmi.RemoteException,
			SQLException {
		String text = null;
		String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
		try {
			Class.forName(driver);
		} catch (Exception e) {
			System.out.println(e.toString());
			// e.printStackTrace();
		}
		String b = Integer.toString(a);
		String url = "jdbc:odbc:lib";
		Connection con = DriverManager.getConnection(url);
		Statement s = con.createStatement();
		String str = "select* from reader";
		ResultSet re = s.executeQuery(str);

		while (re.next()) {

			String 字段名 = re.getString(1);
			if (字段名.equals(b)) {
				text = "姓名:" + re.getString(2) + "\n" + "读者权限:"
						+ re.getString(3) + "\n" + "联系方式:" + re.getString(4)
						+ "\n" + "电话号码:" + re.getString(5) + "\n" + "电子邮件:"
						+ "\n" + re.getString(6);
				break;
			}
		}
		// s.execute("update 图书 set 图书状态='在馆' where 图书编号="+y);
		con.close();
		s.close();
		return text;
	}

	public int passwd(int a, String b) throws java.rmi.RemoteException,
			SQLException {
		int p = 2;
		String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
		try {
			Class.forName(driver);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		String url = "jdbc:odbc:lib";
		Connection con = DriverManager.getConnection(url);
		Statement s = con.createStatement();
		String s_sql = "select * from administer ";
		ResultSet love = s.executeQuery(s_sql);
		// int i = 0;
		int o = b.length();
		double u = 1;
		char[] ii = new char[o];
		for (int t = 0; t < o; t++) {
			ii[t] = b.charAt(t);
			u = u * (double) ii[t];
		}
		String gyl = "";
		gyl = Long.toString((long) (u * Math.sqrt(519) * 377));
		while (love.next()) {
			int ID = Integer.parseInt(love.getString(2));
			String 密码 = love.getString(3);
			if (ID == a) {
				if (gyl.equals(密码)) {
					p = 0;
					break;
				} else {
					// 输入密码错误
					p = 1;
					break;
				}
			} else {

				// 输入用户名或密码错误
				p = 2;
			}
		}
		con.close();
		s.close();

		return p;
	}

	public int GetTime() throws RemoteException {
		int y, m, d;
		Calendar cal = Calendar.getInstance();
		y = cal.get(Calendar.YEAR);
		m = cal.get(Calendar.MONTH);
		d = cal.get(Calendar.DATE);
		// h=cal.get(Calendar.HOUR_OF_DAY);
		// mi=cal.get(Calendar.MINUTE);
		// s=cal.get(Calendar.SECOND);
		int time = (y * 10000 + m * 100 + d);
		// +h+"时"+mi+"分"+s+"秒"
		return time;
	}

	public String[] outbook(int a, int b) throws java.rmi.RemoteException,
			SQLException {
		String[] rt = new String[4];
		for (int v = 0; v < 4; v++)
			rt[v] = "对不起,此书已经借出";
		String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
		try {
			Class.forName(driver);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		int d = 0;
		int z = 0;
		int time = GetTime();
		String url = "jdbc:odbc:lib";
		Connection con = DriverManager.getConnection(url);
		Statement s = con.createStatement();
		String str = "select* from book";
		Connection con1 = DriverManager.getConnection(url);
		Statement s1 = con1.createStatement();
		String str1 = "select* from recorder";
		ResultSet re = s.executeQuery(str);
		ResultSet re1 = s1.executeQuery(str1);
		System.out.print("uuu");
		while (re.next()) {
			System.out.print("uuu");
			int ID = Integer.parseInt(re.getString(1));
			String ss = re.getString(8);
			if ((ID == a) && (ss.equals("在馆"))) {
				// JOptionPane.showMessageDialog(null,a+re.getString(2)+"状态已更改为:借出");
				rt[0] = re.getString(2);
				rt[1] = re.getString(3);
				rt[2] = re.getString(4);
				rt[3] = Integer.toString(time);
				z = 1;
				break;
			}
		}
		if (z == 1) {
			while (re1.next()) {

				d = Integer.parseInt(re1.getString(1));
			}
			d++;
//			String a1=Integer.toString(a);
			s.execute("update book set BookStatus ='借出' where BookID=" + a+"");
			s.execute("INSERT INTO recorder VALUES(" + d + "," + a + "," + b
					+ "," + time + "," + 1 + ")");
		}
		con.close();
		s.close();
		return rt;
	}

	public String[] inbook(int a) throws java.rmi.RemoteException, SQLException {
		String[] rt = new String[5];
		for (int v = 0; v < 5; v++)
			rt[v] = "错误,此书尚未借出";
		String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
		try {
			Class.forName(driver);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		String url = "jdbc:odbc:lib";
		Connection con = DriverManager.getConnection(url);
		Connection con1 = DriverManager.getConnection(url);
		Statement s = con.createStatement();
		Connection con2 = DriverManager.getConnection(url);
		Statement s2 = con.createStatement();
		Statement s1 = con1.createStatement();
		String str = "select* from book";
		ResultSet re = s.executeQuery(str);

		while (re.next()) {
			int ID = Integer.parseInt(re.getString(1));
			String ss = re.getString(8);
			if ((ID == a) && (ss.equals("借出"))) {

				rt[1] = re.getString(2);
				rt[2] = re.getString(3);
				// rt[4] = re.getString(4);
				s.execute("update book set BookStatus ='在馆' where BookID=" + a);
				break;
			}
		}
		String str1 = "select* from recorder";
		String str2 = "select* from configuration";
		ResultSet re1 = s1.executeQuery(str1);
		ResultSet re2 = s2.executeQuery(str2);
		int md = 0;
		while (re2.next()) {
			md = Integer.parseInt(re2.getString(3));
		}
		while (re1.next()) {
			int ID = Integer.parseInt(re1.getString(2));
			int yx = Integer.parseInt(re1.getString(5));
			if (ID == a && yx == 1) {
				rt[0] = re1.getString(3);
				rt[3] = re1.getString(4);
				int time = GetTime();
				int time1 = Integer.parseInt(rt[3]);
				long day = ((long) (time / 10000) * 365
						+ (long) ((time - (long) (time / 10000) * 10000) / 100)
						* 30 + (long) (time - (long) (time / 10000) * 10000 - (int) ((time - (long) (time / 10000) * 10000) / 100) * 100));

				long day1 = ((long) (time1 / 10000)
						* 365
						+ (long) ((time1 - (long) (time1 / 10000) * 10000) / 100)
						* 30 + (long) (time1 - (long) (time1 / 10000) * 10000 - (int) ((time1 - (long) (time1 / 10000) * 10000) / 100) * 100));

				if ((day - day1) < (md + 1)) {
					rt[4] = "无罚款。";
				} else
					rt[4] = "超过规定时间,请交纳罚款。";
				s1.execute("update recorder set Status =0 where BookID=" + a);
				break;
			}
		}

		con.close();
		s.close();

		return rt;
	}

	public boolean book(int a, String b, String c, String d, double e,
			String f, String g, String h, int i)
			throws java.rmi.RemoteException, SQLException {
		boolean rt = true;
		if (i == 0) {
			String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
			System.out.print(rt);
			try {
				Class.forName(driver);
			} catch (Exception x) {
				System.out.println(x.toString());
			}
			System.out.print(rt);
			String url = "jdbc:odbc:lib";
			Connection con = DriverManager.getConnection(url);
			Statement s = con.createStatement();
			String str = "select * from book";
			ResultSet re = s.executeQuery(str);
			System.out.print(rt);
			while (re.next()) {
				int ID = Integer.parseInt(re.getString(1));
				if (ID == a) {
					rt = false;
					break;
				}

			}
			if (rt == true) {
				String str1 = "INSERT INTO book VALUES(" + a + ",'" + b + "','"
						+ c + "','" + d + "'," + e + ",'" + f + "','" + g
						+ "','" + h + "')";
				s.executeUpdate(str1);
			}
			con.close();
			s.close();
		}
		if (i == 1) {
			String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
			try {
				Class.forName(driver);
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			rt = false;
			String url = "jdbc:odbc:lib";
			Connection con = DriverManager.getConnection(url);
			String str = "select * from reader";
			Statement s = con.createStatement();
			ResultSet re = s.executeQuery(str);
			rt = false;
			while (re.next()) {
				int ID = Integer.parseInt(re.getString(1));
				if (ID == a) {
					rt = true;
					break;
				}
			}

			if (rt == true) {
				s.execute("update book set BookName='" + b + "' where bookID="
						+ a);
				s.execute("update book set Author='" + c + "' where bookID="
						+ a);
				s.execute("update book set BookPrice=" + e + " where bookID="
						+ a);
				s.execute("update book set Abstract='" + f + "' where bookID="
						+ a);
				s
						.execute("update book set Class='" + g
								+ "' where bookID=" + a);
				s.execute("update book set BookStatus='" + h
						+ "' where bookID=" + a);
				s.execute("update book set Publisher='" + d + "' where bookID="
						+ a);
			}
			s.close();
			con.close();
		}

		return rt;

	}

	public String[] book(int a, int b) throws java.rmi.RemoteException,
			SQLException {
		String[] book = new String[8];
		if (a == 0) {
			String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
			try {
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int u = 0;
			String url = "jdbc:odbc:lib";
			Connection con = DriverManager.getConnection(url);
			Connection con1 = DriverManager.getConnection(url);
			Statement s = con.createStatement();
			Statement s1 = con1.createStatement();
			String str = "select* from book";
			ResultSet re = s.executeQuery(str);
			while (re.next()) {
				int ID = Integer.parseInt(re.getString(1));
				if (ID == b) {
					if (re.next()) {
						book[0] = Integer.toString(ID + 1);
						for (int i = 1; i < 8; i++) {
							book[i] = (String) re.getString(i + 1);

⌨️ 快捷键说明

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