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

📄 librarybooks.java

📁 卡耐基梅隆大学数据库教程SSD7
💻 JAVA
字号:
/**
 * Class <b>LibraryBooks</b> contains
 * the specification of a JavaBean
 * storing the books held at a library.
 *
 * @author CTE
 * @version 1.0
 */

package library;
import java.sql.*;
import java.io.*;
import java.util.*;

public class LibraryBooks
{
    private static final int MAXBOOKS=100;
    private String [] titles = new String[MAXBOOKS];
    private String [] isbns = new String[MAXBOOKS];
    private int numBooks = 0;
    private Connection conn = null;
    private Statement stmt = null;
    private ResultSet rset = null;
    private String errorString = null;
    private boolean hasError = false;
    private String username = "postgres8";
    private String password = "123";
    
    /**
     * class LibraryBooks constructor.  This constructor
     * opens the JDBC connection to the library database
     * and queries all of the books in the library.
     * The titles and isbns are stored in arrays of string.
     * If an exception occurs, an hasError is set to true,
     * and the error string is set to the exception's
     * string representation.
     */
    public LibraryBooks()
    {
	String SQLQuery;
	//set up the jdbc connections
	try
	    {
		String URL = "jdbc:postgresql:ex4";//指定数据源
		Class.forName("org.postgresql.Driver");//驱动器
		conn = DriverManager.getConnection( URL, username, password );//连接
		stmt = conn.createStatement();//建立实例
		SQLQuery = "select distinct name, isbn from title5";//查询语句
		rset = stmt.executeQuery( SQLQuery );//执行查询
		// Fill up the arrays for isbns and titles.
		while( rset.next() )
		    {
			titles[numBooks] = new String( rset.getString("name") );
			isbns[numBooks] = new String( rset.getString("isbn") );
			numBooks++;
		    }
	    }
	catch( Exception e )
	    {
		// Set the error string.
		errorString = e.toString();
		// Set that an error has occured.
		hasError = true;
	    }
    }
    
    /**
     * Return whether or not an error has occured.
     * @return boolean
     */
    public boolean hasError()
    {
	return hasError;
    }

    /**
     * Reset the errorString and hasError instance variables
     * within the LibraryBooks bean.
     * @return void
     */
    public void resetError()
    {
	hasError=false;
	errorString=null;
    }

    /**
     * Return the string representation of an error.
     * @return String
     */
    public String geterrorString()
    {
	return errorString;
    }

    /*
     * Return the number of distinct books in the library.
     * @return int
     */
    public int getnumBooks()
    {
	return numBooks;
    }

    /*
     * Return an array of strings which are all of the distinct titles in the library.
     * @return String[]
     */
    public String [] getTitles()
    {
	return titles;
    }

    /*
     * Return an array of strings which are all of the distinct isbns in the library.
     * @return String[]
     */
    public String [] getIsbns()
    {
	return isbns;
    }
}
	
		












⌨️ 快捷键说明

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