以下是示例,该示例使用PreparedStatement以及打开和关闭语句:
该示例代码是根据前几章中的环境和数据库设置编写的。
复制并粘贴以下示例到 JDBCExample.java 中,如下编译并运行:
//步骤1. 导入所需的软件包 import java.sql.*; public class JDBCExample { //JDBC驱动程序名称和数据库URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // 数据库凭据 static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; PreparedStatement stmt = null; try{ //步骤2: 注册 JDBC 驱动程序 Class.forName("com.mysql.jdbc.Driver"); //步骤3:打开连接 System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //步骤4:执行查询 System.out.println("Creating statement..."); String sql = "UPDATE Employees set age=? WHERE id=?"; stmt = conn.prepareStatement(sql); //将值绑定到参数中。 stmt.setInt(1, 35); // set age stmt.setInt(2, 102); // set ID //让我们用ID=102更新记录的年龄; int rows = stmt.executeUpdate(); System.out.println("Rows impacted : " + rows ); //让我们选择所有的记录并显示它们。 sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); //步骤5:从结果集中提取数据 while(rs.next()){ //按列名检索 int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } //第六步:清理环境 rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //处理JDBC的错误 se.printStackTrace(); }catch(Exception e){ //处理 Class.forName 的错误 e.printStackTrace(); }finally{ //用于关闭资源的finally块 try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// nothing we can do try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try System.out.println("Goodbye!"); }//end main }//end JDBCExample
现在让我们编译上面的示例,如下所示:
C:\>javac FirstExample.java C:\>
当您运行FirstExample时,它将产生以下结果-
C:\>java FirstExample Connecting to database... Creating statement... ID: 100, Age: 18, First: Zara, Last: Ali ID: 101, Age: 25, First: Mahnaz, Last: Fatma ID: 102, Age: 30, First: Zaid, Last: Khan ID: 103, Age: 28, First: Sumit, Last: Mittal C:\>