ㄓㄤㄑㄧㄤ

JDBC进阶之PreparedStatement执行SQL语句(MySQL)

一、什么是PreparedStatement
          参阅Java API文档,我们可以知道,PreparedStatement是Statement的子接口(如图所示),表示预编译的 SQL 语句的对象,SQL 语句被预编译并存储在PreparedStatement 对象中。然后可以使用此对象多次高效地执行该语句。
 
 
二、通过PreparedStatement获取在运行命令行中执行的参数,将参数插入到某张数据表中
          相关的实验过程,包括在预先创建程序所需数据库、创建所需数据表格、在开发环境中加载驱动程序包等,可参考前一篇文章《JDBC连接MySQL数据库及示例》(前往该文章
具体代码如下:
[java] view plaincopy
 
  1. package com.serein.jdbc;  
  2.   
  3. import java.sql.*;  
  4.   
  5. public class preparedStatemetTest {  
  6.   
  7.     public static void main(String[] args) {  
  8.           
  9.         //检查命令行中是否够7个参数  
  10.         if(args.length != 7) {  
  11.             System.out.println("Parameter Error! Please Input Again!");  
  12.             System.exit(-1);  
  13.         }  
  14.           
  15.         //程序获取运行栈里的7个参数值  
  16.         String name = args[0];    
  17.         int age = 0;  
  18.         try {  
  19.             age = Integer.parseInt(args[1]);  
  20.         } catch (NumberFormatException e) {  
  21.             System.out.println("Parameter Error! Age should be Number Format!");  
  22.             System.exit(-1);  
  23.         }  
  24.           
  25.         String sex = args[2];  
  26.         String address = args[3];  
  27.         String depart = args[4];  
  28.           
  29.         int worklen = 0;  
  30.         try {  
  31.             worklen = Integer.parseInt(args[5]);  
  32.         } catch (NumberFormatException e) {  
  33.             System.out.println("Parameter Error! Worklen should be Number Format!");  
  34.             System.exit(-1);  
  35.         }  
  36.           
  37.         int wage = 0;  
  38.         try {  
  39.             wage = Integer.parseInt(args[6]);  
  40.         } catch (NumberFormatException e) {  
  41.             System.out.println("Parameter Error! Wage should be Number Format!");  
  42.             System.exit(-1);  
  43.         }  
  44.           
  45.         //创建PreparedStatement对象  
  46.         PreparedStatement pstmt = null;  
  47.         //创建连接对象  
  48.         Connection conn = null;  
  49.           
  50.         //连接数据库,并插入数据  
  51.         try {  
  52.             //加载MySQL驱动实例,提供了两种方法,是等价的  
  53.             Class.forName("com.mysql.jdbc.Driver");  
  54.             //new oracle.jdbc.driver.OracleDriver();  
  55.               
  56.             //建立连接  
  57.             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myuser", "root", "root");  
  58.               
  59.             //使用PreparedStatement对象里来构建并执行SQL语句,7个问号代表7个字段预先要保留的值  
  60.             pstmt = conn.prepareStatement("INSERT INTO staff(name, age, sex,address, depart, worklen,wage) VALUES (?, ?, ?, ?, ?, ?, ?)");  
  61.               
  62.             //通过PreparedStatement对象里的set方法去设置插入的具体数值  
  63.             pstmt.setString(1, name);  
  64.             pstmt.setInt(2, age);  
  65.             pstmt.setString(3, sex);  
  66.             pstmt.setString(4,address );  
  67.             pstmt.setString(5, depart);  
  68.             pstmt.setInt(6, worklen);  
  69.             pstmt.setInt(7, wage);    
  70.             pstmt.executeUpdate();  
  71.               
  72.             //插入成功提示  
  73.             System.out.print("成功插入一条数据记录!");  
  74.               
  75.         //捕获驱动加载失败异常      
  76.         } catch (ClassNotFoundException e) {  
  77.             e.printStackTrace();  
  78.           
  79.         //捕获SQL语句执行失败异常  
  80.         } catch (SQLException e) {  
  81.             e.printStackTrace();  
  82.         //恢复变量初始值     
  83.         } finally {  
  84.             try {  
  85.                 if(pstmt != null) {  
  86.                     pstmt.close();  
  87.                     pstmt = null;  
  88.                 }  
  89.                 if(conn != null) {  
  90.                     conn.close();  
  91.                     conn = null;  
  92.                 }  
  93.             //捕获SQL异常  
  94.             } catch (SQLException e) {  
  95.                 e.printStackTrace();  
  96.             }  
  97.         }  
  98.     }  
  99. }  
 
编写好代码之后,要进行带参数运行程序,方法如下:
 
 
 
其中输入的参数为(可参考进行修改):
SereinChan
25
M
Guangzhou
Engine
3
5000

 

查看Console控制台,"成功插入一条数据记录!":

 

查看MySQL数据库,进行确认:

 

成功完成!

转自:http://blog.csdn.net/cxwen78/article/details/6868941

posted @ 2014-10-20 11:05  ㄓㄤㄑㄧㄤ  阅读(7893)  评论(0编辑  收藏  举报
哈哈,页脚部分。