todatabase.java

来自「一个很好的JAVA图书馆查询代码,它有学生查询和老师查询等」· Java 代码 · 共 133 行

JAVA
133
字号
//import the packages for using the classes in them into the program
   import java.sql.*;

   public class toDatabase {
   /***************************************************************************
    ***      declaration of the private variables used in the program       ***
    ***************************************************************************/
   
   //for setting the connection and statement
      private Connection connection = null;
      private Statement statement   = null;
      private ResultSet resultset   = null;
   
      public boolean isDouble(String query) {
         try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         }
            catch(ClassNotFoundException ea) {
               System.out.println(ea.toString());
            }
            catch(Exception e) {
               System.out.println(e.toString());
            }
      /***************************************************************
       * for making the connection,creating the statement and update *
       * the table in the database. After that,closing the statmenet *
       * and connection. There is catch block SQLException for error *
       ***************************************************************/
         try {
            connection = DriverManager.getConnection("jdbc:odbc:JLibrary");
            statement  = connection.createStatement();
            resultset  = statement.executeQuery(query);
            while(resultset.next()) {
               return true;
            }
            resultset.close();
            statement.close();
            connection.close();
         }
            catch(SQLException SQLe) {
               System.out.println(SQLe.toString());
            }
         return false;
      }
      public int numberOfBooks(String query) {
         try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         }
            catch(ClassNotFoundException ea) {
               System.out.println(ea.toString());
            }
            catch(Exception e) {
               System.out.println(e.toString());
            }
      /***************************************************************
       * for making the connection,creating the statement and update *
       * the table in the database. After that,closing the statmenet *
       * and connection. There is catch block SQLException for error *
       ***************************************************************/
         try {
            connection = DriverManager.getConnection("jdbc:odbc:JLibrary");
            statement  = connection.createStatement();
            resultset  = statement.executeQuery(query);
            while(resultset.next()) {
               return resultset.getInt("NumberOfBooks");
            }
            resultset.close();
            statement.close();
            connection.close();
         }
            catch(SQLException SQLe) {
               System.out.println(SQLe.toString());
            }
         return -1;
      }
      public int memberID(String query) {
         try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         }
            catch(ClassNotFoundException ea) {
               System.out.println(ea.toString());
            }
            catch(Exception e) {
               System.out.println(e.toString());
            }
      /***************************************************************
       * for making the connection,creating the statement and update *
       * the table in the database. After that,closing the statmenet *
       * and connection. There is catch block SQLException for error *
       ***************************************************************/
         try {
            connection = DriverManager.getConnection("jdbc:odbc:JLibrary");
            statement  = connection.createStatement();
            resultset  = statement.executeQuery(query);
            while(resultset.next()) {
               return resultset.getInt("MemberID");
            }
            resultset.close();
            statement.close();
            connection.close();
         }
            catch(SQLException SQLe) {
               System.out.println(SQLe.toString());
            }
         return -1;
      }
      public void update(String query) {
         try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         }
            catch(ClassNotFoundException ea) {
               System.out.println(ea.toString());
            }
            catch(Exception e) {
               System.out.println(e.toString());
            }
      /***************************************************************
       * for making the connection,creating the statement and update *
       * the table in the database. After that,closing the statmenet *
       * and connection. There is catch block SQLException for error *
       ***************************************************************/
         try {
            connection = DriverManager.getConnection("jdbc:odbc:JLibrary");
            statement  = connection.createStatement();
            statement.executeUpdate(query);
            statement.close();
            connection.close();
         }
            catch(SQLException SQLe) {
               System.out.println(SQLe.toString());
            }
      }
   }

⌨️ 快捷键说明

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