预处理语句
来源:互联网 发布:菠萝饭官方软件 编辑:程序博客网 时间:2024/04/30 22:27
7.2.2 预处理语句
从JDBC API可以看到,PreparedStatement扩展自Statement,PreparedStatement是用于执行预编译的SQL语句,在提高性能方面有很多优点。下面以PreparedStatement举个例子,保存为TestPrepStmt.java。
例7-5 PreparedStatement预处理语句。
- import java.sql.*;
- public class TestPrepStmt
- {
- public static void main(String[] args)
- {
- if(args.length!=3)
- {
- System.out.println("参数错误!,请重新输入!");
- System.exit(-1);
- }
- int id=0;
- try
- {
- id=Integer.parseInt(args[0]);
- }
- catch(NumberFormatException e)
- {
- System.out.print("请输入整数");
- System.exit(-1);
- }
- String name=args[1];
- int age=0;
- try
- {
- age=Integer.parseInt(args[2]);
- }
- catch(NumberFormatException e)
- {
- System.out.print("请输入整数");
- System.exit(-1);
- }
- PreparedStatement pstmt=null;
- Connection conn=null;
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- String url="jdbc:mysql://127.0.0.1:3306/mysql";
- String user="root";
- String password="admin";
- conn=DriverManager.getConnection(url,user,password);
- pstmt=conn.prepareStatement("insert into dbtest values(?,?,?)");
- pstmt.setInt(1, id);
- pstmt.setString(2,name);
- pstmt.setInt(3,age);
- pstmt.executeUpdate();
- }
- catch(ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if(pstmt!=null)
- {
- pstmt.close();
- }
- if(conn!=null)
- {
- conn.close();
- }
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
这个程序在运行时,输入参数示例如下:
- java TestPrepStmt 6 wpm 25
0 0
- 预处理语句
- 预处理语句
- 预处理语句
- 使用语句预处理
- gcc:预处理语句
- 预处理语句对象PreparedStatement
- MYSQL预处理语句
- MySQL预处理语句
- 预处理程序--#define 语句
- IOS 预处理语句
- MySQL预处理语句
- SASS-预处理语句
- PHP MySQL 预处理语句
- mysql prepare 预处理语句
- IOS 预处理语句
- iOS 预处理语句
- 预处理sql语句
- PHP MySQL 预处理语句
- Chapter06-Monkey and Banana(EOJ 1093)
- 自定义ios7标题栏和状态栏
- 黑马程序员_交通灯管理系统
- 在腾讯微博发布的长微博如何被百度收录
- Problem - 244B - Codeforces STL 中set的用法
- 预处理语句
- struts2中ServletActionContext.getRequest() 不是请求原来的request
- lcd接口
- C 语言中 typeof 关键字简介
- Subresource for target class has no jax-rs annotations
- NSStringg常用用法
- C语言~和>>运算符
- 段位结构体与补码、大小端
- 明星粉丝经济“叫好不叫座”?变现需找准“窍门儿”