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);
}
分享到:
相关推荐
JDBC实例教程 JDBC(Java Database Connectivity)是一种Java API,用于连接和操作关系数据库。以下是JDBC实例教程的详细知识点: JDBC 代码手册 JDBC 代码手册是 JDBC 的核心组件之一,由 Eric 于 2008 年 1 月 ...
在Java的JDBC(Java Database Connectivity)中,`Statement`和`CallableStatement`对象是用于执行SQL语句的关键组件。这两个接口提供了与数据库交互的方法,允许程序员执行查询、更新和其他数据库操作。 首先,...
**JDBC连接MySQL实例详解** Java Database Connectivity (JDBC) 是Java编程语言中用于与数据库交互的一种接口标准,由Sun Microsystems公司(现为Oracle公司)开发。它为Java程序员提供了标准化的方法来创建、执行...
使用`Statement`或`PreparedStatement`对象的`executeQuery()`或`executeUpdate()`方法执行SQL。前者用于查询,返回`ResultSet`;后者用于增删改操作,返回受影响的行数。 4. **处理结果集** `ResultSet`对象用于...
- 通过`Connection`对象的`prepareStatement()`方法创建一个预编译的`PreparedStatement`对象。 - 预编译的好处在于,对于那些重复执行的SQL语句,预编译可以提高效率。 - 可以设置参数值,适合动态构建SQL语句。...
本实例是一个专门为新手设计的学习教程,帮助初学者理解和掌握JDBC的基本操作。 在JDBC中,主要涉及以下几个核心概念: 1. **驱动管理**:JDBC驱动是Java代码与数据库通信的桥梁。在使用JDBC前,需要加载并注册...
有三种Statement类型:Statement(默认),PreparedStatement(预编译的SQL,更安全),CallableStatement(用于调用存储过程)。 4. **执行SQL语句**:通过Statement对象的executeQuery()或executeUpdate()方法...
JDBC 连接数据库方法...JDBC 连接数据库的方法可以分为四个步骤:加载 JDBC 驱动程序、提供 JDBC 连接的 URL、创建数据库的连接、创建一个 Statement。通过这四个步骤,开发者可以轻松地连接数据库并执行 SQL 语句。
1. **CallableStatement**:CallableStatement是JDBC接口,用于执行存储过程。在学员管理系统中,如果数据库中包含存储过程,CallableStatement允许我们调用它们,并传递参数及接收返回值。与PreparedStatement类似...
本章的实例"JDBC-5-实例(电子词典)"可能涉及创建一个简单的电子词典应用,该应用利用JDBC与数据库进行交互,实现单词的增删查改等功能。通过这个实例,读者可以学习到如何在实际项目中运用上述知识点,理解JDBC...
有三种类型:简单`Statement`,预编译的`PreparedStatement`,以及用于调用存储过程的`CallableStatement`。`PreparedStatement`可以提高性能,因为它允许预先编译SQL语句,从而减少解析时间。 4. **结果集...
当PreparedStatement实例化时,会预先指定一个SQL语句,这个语句会被直接发送到数据库进行编译。因此,当同一个PreparedStatement多次执行时,由于编译只需进行一次,性能提升显著,尤其适合于频繁执行相同查询或...
Connection对象创建Statement或PreparedStatement实例,而这些实例用于执行SQL语句。执行后返回ResultSet对象,用于检索查询结果。CallableStatement用于处理存储过程的调用,同样返回ResultSet对象。 **六、JDBC的...
- Statement/PreparedStatement/CallableStatement:用于执行SQL语句的接口,其中PreparedStatement可以预编译SQL,提高执行效率,CallableStatement用于调用存储过程。 - ResultSet:查询结果集,用于遍历查询...
它提供了创建Statement、PreparedStatement和CallableStatement对象的方法,以及关闭数据库连接的方法。例如,`createStatement()`用于创建Statement对象,`prepareStatement()`用于创建PreparedStatement对象,它们...
例如,`Connection.createStatement()`可以创建一个Statement实例。 4. **PreparedStatement对象** `PreparedStatement`是预编译的SQL语句,它比`Statement`更高效且安全。`PreparedStatement`可以防止SQL注入攻击...
在实际项目中,为了提高代码的健壮性和可维护性,我们通常会使用PreparedStatement和CallableStatement,它们可以防止SQL注入,并允许预编译SQL语句。此外,还可以考虑使用连接池(如C3P0、HikariCP)来管理数据库...
通过调用 `Connection` 对象的 `getMetaData()` 方法获取元数据实例,然后通过这个实例可以查询到数据库的详细结构。例如,获取所有表名可以通过 `getTables()` 方法实现。 2. **可滚动的结果集**: - JDBC2.0 ...
- **作用**:提供了与数据库交互的基本手段,如创建`Statement`、`PreparedStatement`或`CallableStatement`等对象。 - **生命周期管理**: - **获取连接**:通常通过`DataSource`或直接使用`DriverManager`来获取...