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

📄 librarybooks.java

📁 本系统为一个在线图书馆系统
💻 JAVA
字号:
package library;
import java.sql.*; 

/**
 * Class <b>LibraryBooks</b> contains
 * the specification of a JaveBean storeing 
 * all of the books currently in the library.
 *
 * @author Class 3,Software
 * @version 1.0
 */
public class LibraryBooks
{
       
	private Connection conn = null; 
	private Statement st = null;
	private ResultSet rs = null; 
	
	private int num;
	
	/**
	 * the String of the error message
	 */
	private String strError = "";
	
	/**
	 * the code number of the error
	 * 0  -- no error
	 * -1 -- the error of database operation 
	 * -2 -- the error of coding
	 */
	private int errCode = 0;

    /**
     * Empty constructor. 
     * All JavaBeans must have an empty constructor.
     */
    public LibraryBooks(){}
    
    /**
     * The ConnectDBialization of the database 
     */
    public boolean ConnectDB(){	   		
	   
       try {
           Class.forName("org.postgresql.Driver");
           conn = DriverManager.getConnection("jdbc:postgresql:postgres","postgres","1234"); 
           st = conn.createStatement(); 
           return true;
       }      
       catch(java.lang.ClassNotFoundException e){ 
    	   errCode = -2;
    	   strError = e.getMessage();   
    	   return false;
        } 
       catch(SQLException ex){ 
    	   errCode = -1;
    	   strError = ex.getMessage();
    	   return false;
    	} 
    }
    
    /**
     * 
     * @param sql - the query statement for the database
     * @return the a resultset of the query 
     */
    public ResultSet executeQuery(String sql) { 

       try{rs = st.executeQuery(sql);} 
       catch(SQLException ex){ 
    	errCode = -1;
    	strError = ex.getMessage();
       } 
      return rs; 
    } 
    
    /**
     * Close the database
     */
    public void DisConnectDB() {
   
        try {  
     	   if (rs != null){rs.close();}
     	   if (st != null){st.close();}
     	   if (conn != null){conn.close();}
         }
        catch(SQLException ex){ 
      	  errCode = -1;
      	  strError = ex.getMessage();    	      
         } 
     }
    
    /**
     * 
     * @return the titles in the library as arrays of Strings 
     * @throws SQLException 
     * @throws SQLException
     */
	public String[] getTitles() {

		String[] titleArray = new String[num];

		if (!ConnectDB()) return null;
		try { 
	        ResultSet rs = executeQuery("select name from title5");
	        int curPos = 0;
	        while (rs.next()){	    	
	    	    titleArray[ curPos ] = rs.getString("name");
	    	    curPos++;
	        }
	        DisConnectDB();
		}	    
		catch(Exception e){}
	    return titleArray;
	}

    /**
     * 
     * @return the ISBNs in the library as arrays of Strings 
     * @throws SQLException
     */
	public String[] getIsbns() {		
	
		String[] isbnArray = new String[num];
		
		if (!ConnectDB()) return null;
		try {
	        ResultSet rs = executeQuery("select isbn from title5");
	        int curPos = 0;
	        while (rs.next()){
	    	     isbnArray[ curPos++ ] = rs.getString("isbn");	    	
	        }
	        DisConnectDB();			   	
	     }
		 catch(Exception e){}
		 return isbnArray;
	}
	
	/**
	 * 
	 * @param isbn --the isbn of the book
	 * @return the title of the book according to the isbn
	 * @throws SQLException
	 */
	public String getTitleByIsbn( String isbn )  {
		
		 String title = "";

		 if (!ConnectDB()) return null;
		 try {			
		     ResultSet rs = executeQuery("select name from title5 where isbn ='" + isbn + "'");
		     if ( rs.next() )
		         title = rs.getString("name");
		     DisConnectDB();	
		 }
		 catch(Exception e){}
		 return title;
	}
	
   /**
    * 获取指定SQL查询语句执行后返回的记录数
    * @return the row count of result set
    * @throws SQLException
    */
   public int getRowCount(){	   	
	   
	   if (!ConnectDB()) return -1;
	   try{
            ResultSet rs = executeQuery("select count(*) from title5");
            rs.next();
            num = rs.getInt(1);
            DisConnectDB();	 	   
	   }
       catch(Exception e){}
	   return num;
   }
   
   /**
    * 
    * @return the string of the error message
    */
   public String getStrError() {
	  
       return strError;	
   }
  
    /**
     * 
     * @return the number of the error 
     */
    public int getErrCode() {
	  
	   return errCode;	
  }   
}

⌨️ 快捷键说明

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