first.java~4~

来自「学生信息管理系统」· JAVA~4~ 代码 · 共 78 行

JAVA~4~
78
字号
package sfms;import java.sql.*;public class first{  public static void main(String args[]){    //declare Connection and Statement objects    Connection conn=null;    Statement state=null;    try{      //register the Oracle JDBC drivers      Class.forName("oracle.jdbc.OracleDriver").newInstance();      //create a Connection object,and connect to the database      //as df using the Oracle JDBC thin driver      conn=DriverManager.getConnection(          "jdbc:oracle:thin:@localhost:1521:SFMS",          "df","1234"          );      //create a Statement object      state=conn.createStatement();      //create a ResultSet object,and populate it with the      //result of SELECT statement that retrieves the user      //and sysdate variables from the database via the dual      //table -the executeQuery() method of the Statement object      //is used to perform the SELECT      ResultSet result=state.executeQuery(          "SELECT user,sysdate from dual"          );      //      //the next() method      result.next();      //retrieve the user from the row in the Resultset using      //the getString() method      String user=result.getString("user");      //retrieve the sysdate from the row in the ResultSet using      //the getTimestamp() method      Timestamp dateTime=          result.getTimestamp("sysdate");      System.out.print("Hello  "+user+                       ", the current date and time is "+dateTime);      //close this ResultSet object using the close() method      result.close();    }    catch(SQLException e){      System.out.println("Error Code ="+e.getErrorCode());      System.out.println("Error message="+e.getMessage());    }    catch(Exception e){        e.printStackTrace();      }    finally{      try{        //close the Statement object using the close() method        if(state!=null){         state.close();        }        //close the Connection object using the close() method        if(conn!=null){          conn.close();        }      }      catch(SQLException e){        System.out.println("Error Code ="+e.getErrorCode());       System.out.println("Error message="+e.getMessage());      }    }  }//end of main()}

⌨️ 快捷键说明

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