程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JDBC中Statement,PreparedStatement,CallableStatement三個方法的實例

JDBC中Statement,PreparedStatement,CallableStatement三個方法的實例

編輯:關於JAVA

public void ListStudents() throws SQLException{
 int i, NoofColumns;
 String StNo, StFName, StLName;
 //初始化並加載JDBC-ODBC驅動程序
 Class.forName("jdbc.odbc.JdbcOdbcDriver");
 //創建連接對象
 Connection Ex1Con = DriverManager.getConnection("jdbc:odbc:StudentDB";uid="admin";pw="sa");
 //創建一個簡單的Statement對象
 Statement Ex1Stmt = Ex1Con.createStatement();
 //創建SQL串,傳送到DBMS並執行SQL語句
 ResultSet Ex1rs = Ex1Stmt.executeQuery("SELECT StudentID, FirstName, LastName FROM Students");
 //處理每一個數據行,直到不再有數據行
 System.out.println("Student Number   First Name   Last Name");
 while(Ex1rs.next()){
 //將列值保存到java變量中
 StNo = Ex1rs.getString(1);
 StFName = Ex1rs.getString(2);
 StLName = Ex1rs.getString(3);
 System.out.println(StNo, StFName, StLName);
 }
}
public void UpdateStudentName(String StFName, String StLName, String StNo) throws SQLException, ClassNotFoundException
{
 int RetValue;
 //初始化並加載JDBC-ODBC驅動程序
 Class.forName("jdbc.odbc.JdbcOdbcDriver");
 //創建連接對象
 Connection Ex1Con = DriverManager.getConnection("jdbc:odbc:StudentDB";uid="admin";pw="sa");
 //創建一個簡單的Statement對象
 Statement Ex1Stmt = Ex1Con.createStatement();
 //創建SQL串,傳送到DBMS並執行該SQL語句
 String SQLBuffer = "UPDATE Students SET FirstName = " +
 StFName + ",LastName = " + StLName +
 "WHERE StudentNumber = " + StNo;
 RetValue = Ex1Stmt.executeUpdate(SQLBuffer);
 System.out.println("Updated" + RetValue + "rows in the Database.");
}
//使用PreparedStatement改進實例
//Declare class variables
Connection Con;
PreparedStatement PrepStmt;
boolean Initialized = false;
public void InitConnection() throws SQLException, ClassNotFoundException{
 //Initialize and load the JDBC-ODBC driver.
 Class.forName("jdbc.odbc.JdbcOdbcDriver");
 //Make the connection object.
 Con = DriverManager.getConnection("jdbc:odbc:StudentDB";uid="admin";pw="sa");
 //Create a prepared Statement object.
 PrepStmt = Con.prepareStatement("SELECT ClassName, Location, DaysAndTimes FROM Classes WHERE ClassName = ?");
 Initialized = true;
}
public void ListOneClass(String ListClassName) throws SQLException, ClassNotFoundException{
 int i, NoOfColumns;
 String ClassName, ClassLocation, ClassSchedule;
 if(!Initialized){
 InitConnection();
 }

 //Set the SQL parameter to the one passed into this method
 PrepStmt.setString(1, ListClassName);
 ResultSet Ex1rs = PrepStmt.executeQuery();
 //Process each row until there are no more rows and display the results on the console.
 System.out.println("Class Location Schedule");
 while(Ex1rs.next()){
 ClassName = Ex1rs.getString(1);
 ClassLocation = Ex1rs.getString(2);
 ClassSchedule = Ex1rs.getString(3);
 System.out.println(ClassName,ClassLocation,ClassSchedule);
 }
}
//使用CallableStatement顯示成績
//預先定義好的存儲過程的調用形式為:studentGrade = getStudentGrade(StudentID, ClassID)
public void DisplayGrade(String StudentID, String ClassID) throws SQLException
{
 int Grade;
 //Initialize and load the JDBC-ODBC dirver.
 Class.forName("jdbc.odbc.JdbcOdbcDriver");
 //Make the connection object;
 Connection Con = DriverManager.getConnection("jdbc:odbc:studentDB";uid="admin";pw="sa");
 //Create a Callable Statement object;
 CallableStatement CStmt = Con.prepareCall({? = call getStudentGrade[?,?]});

 //Now tie the placeholders with actual parameters.
 //Register the return value from the stored procedure
 //as an integer type so that the driver knows how to handle it.
 //Note the type is defined in the java.sql.Types.
 CStmt.registerOutParameter(1,java.sql.Types.INTEGER);

 //Set the In parameters (which are inherited from the PreparedStatement class)
 CStmt.setString(1,StudentID);
 CStmt.setString(2,ClassID);

 //Now we are ready to call the stored procedure

 int RetVal = CStmt.excuteUpdate();

 //Get the OUT Parameter from the registered parameter
 //Note that we get the result from the CallableStatement object
 Grade = CStmt.getInt(1);

 //And display the results on the console;
 System.out.println("The Grade is:" + Grade);
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved