|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
而学习JAVA我觉得最应该避免的就是:只学习,不思考,只记忆,不实践!
publicvoidListStudents()throwsSQLException{
inti,NoofColumns;
StringStNo,StFName,StLName;
//初始化并加载JDBC-ODBC驱动程序
Class.forName("jdbc.odbc.JdbcOdbcDriver");
//创立毗连对象
ConnectionEx1Con=DriverManager.getConnection("jdbc:odbc:StudentDB";uid="admin";pw="sa");
//创立一个复杂的Statement对象
StatementEx1Stmt=Ex1Con.createStatement();
//创立SQL串,传送到DBMS并实行SQL语句
ResultSetEx1rs=Ex1Stmt.executeQuery("SELECTStudentID,FirstName,LastNameFROMStudents");
//处置每个数据行,直到不再无数据行
System.out.println("StudentNumber FirstName LastName");
while(Ex1rs.next()){
//将列值保留到java变量中
StNo=Ex1rs.getString(1);
StFName=Ex1rs.getString(2);
StLName=Ex1rs.getString(3);
System.out.println(StNo,StFName,StLName);
}
}
publicvoidUpdateStudentName(StringStFName,StringStLName,StringStNo)throwsSQLException,ClassNotFoundException
{
intRetValue;
//初始化并加载JDBC-ODBC驱动程序
Class.forName("jdbc.odbc.JdbcOdbcDriver");
//创立毗连对象
ConnectionEx1Con=DriverManager.getConnection("jdbc:odbc:StudentDB";uid="admin";pw="sa");
//创立一个复杂的Statement对象
StatementEx1Stmt=Ex1Con.createStatement();
//创立SQL串,传送到DBMS并实行该SQL语句
StringSQLBuffer="UPDATEStudentsSETFirstName="+
StFName+",LastName="+StLName+
"WHEREStudentNumber="+StNo;
RetValue=Ex1Stmt.executeUpdate(SQLBuffer);
System.out.println("Updated"+RetValue+"rowsintheDatabase.");
}
//利用PreparedStatement改善实例
//Declareclassvariables
ConnectionCon;
PreparedStatementPrepStmt;
booleanInitialized=false;
publicvoidInitConnection()throwsSQLException,ClassNotFoundException{
//InitializeandloadtheJDBC-ODBCdriver.
Class.forName("jdbc.odbc.JdbcOdbcDriver");
//Maketheconnectionobject.
Con=DriverManager.getConnection("jdbc:odbc:StudentDB";uid="admin";pw="sa");
//CreateapreparedStatementobject.
PrepStmt=Con.prepareStatement("SELECTClassName,Location,DaysAndTimesFROMClassesWHEREClassName=?");
Initialized=true;
}
publicvoidListOneClass(StringListClassName)throwsSQLException,ClassNotFoundException{
inti,NoOfColumns;
StringClassName,ClassLocation,ClassSchedule;
if(!Initialized){
InitConnection();
}
//SettheSQLparametertotheonepassedintothismethod
PrepStmt.setString(1,ListClassName);
ResultSetEx1rs=PrepStmt.executeQuery();
//Processeachrowuntiltherearenomorerowsanddisplaytheresultsontheconsole.
System.out.println("ClassLocationSchedule");
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)
publicvoidDisplayGrade(StringStudentID,StringClassID)throwsSQLException
{
intGrade;
//InitializeandloadtheJDBC-ODBCdirver.
Class.forName("jdbc.odbc.JdbcOdbcDriver");
//Maketheconnectionobject;
ConnectionCon=DriverManager.getConnection("jdbc:odbc:studentDB";uid="admin";pw="sa");
//CreateaCallableStatementobject;
CallableStatementCStmt=Con.prepareCall({?=callgetStudentGrade[?,?]});
//Nowtietheplaceholderswithactualparameters.
//Registerthereturnvaluefromthestoredprocedure
//asanintegertypesothatthedriverknowshowtohandleit.
//Notethetypeisdefinedinthejava.sql.Types.
CStmt.registerOutParameter(1,java.sql.Types.INTEGER);
//SettheInparameters(whichareinheritedfromthePreparedStatementclass)
CStmt.setString(1,StudentID);
CStmt.setString(2,ClassID);
//Nowwearereadytocallthestoredprocedure
intRetVal=CStmt.excuteUpdate();
//GettheOUTParameterfromtheregisteredparameter
//NotethatwegettheresultfromtheCallableStatementobject
Grade=CStmt.getInt(1);
//Anddisplaytheresultsontheconsole;
System.out.println("TheGradeis:"+Grade);
}
学习JAVA的目的更多的是培养自身的工作能力,我觉得工作能力的一个核心就是:独立思考能力,因为只有独立思考后,才会有自己的见解 |
|