课时6:PrepareStatement语句
来源:互联网 发布:php项目进度管理系统 编辑:程序博客网 时间:2024/05/01 10:01
1.PreparedStatement为预定义语句,继承Statement。
2.Statement只能静态操作SQL语句,如果要想动态操作SQL语句又该如何实现呢?例如:注册会员,这里可以使用PreparedStatement来动态操作SQL语句,PreparedStatement通过使用占位符“?”,来预生成SQL语句,从而达到动态操作的功能
3.实例
3.1
面向对象,就是尽量把对象定义成一个java文件,所有属性和操作都定义到里面。在其他函数中直接创建对象和调用对象
package com.geek99.demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test4 {
public static void main(String[] args) {
// insert("tom1","tom1@111111.com");
// Customer c = new Customer();
// c.setName("rose");
// c.setEmail("rose@1111.com");
// insert(c);
// c.setId(1);
// c.setName("dsfasdfa");
// update(c);
// del(2);
Customer c=query(3);
if(c!=null)
System.out.println(c.getName()+":"+c.getEmail());
}
static void insert(String name, String email) {
Connection conn = DBUtil.open();
String sql = "insert into CustomerTbl(name,email)values(?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, email);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void insert(Customer c) {
Connection conn = DBUtil.open();
String sql = "insert into CustomerTbl(name,email)values(?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, c.getName());
pstmt.setString(2, c.getEmail());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void update(Customer c) {
Connection conn = DBUtil.open();
String sql = "update CustomerTbl set name=? where id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, c.getName());
pstmt.setInt(2, c.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void del(int id) {
Connection conn = DBUtil.open();
String sql = "delete from CustomerTbl where id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static Customer query(int id) {
Connection conn = DBUtil.open();
String sql = "select id,name,email from CustomerTbl where id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs=pstmt.executeQuery();
// pstmt.executeUpdate();
if(rs.next()){
String name=rs.getString(2);
String email=rs.getString(3);
Customer c=new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
return c;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
0 0
- 课时6:PrepareStatement语句
- 课时4:Statement语句
- 课时7:CallableStatement语句
- preparestatement
- prepareStatement
- PrepareStatement
- PrepareStatement
- preparestatement
- SQL语句的执行:Statement、PrepareStatement、CallableStatement
- JDBC中的语句对象 statement prepareStatement CallableStatement
- SQL语句的执行:Statement、PrepareStatement、CallableStatement
- H5基础知识第九课时(JS判断语句)
- H5基础知识第九课时(JS循环语句)
- 课时6 gulp命令行
- jsp写调用数据库的sql语句用了prepareStatement
- 得到PrepareStatement最终执行的sql语句的方法
- 得到PrepareStatement最终执行的sql语句的方法
- java用PrepareStatement实现多sql语句执行
- Java代码规范
- jackson java转json hibernate懒加载造成的无限递归问题
- POJ2503Babelfish(MAP+字典树)
- Android Studio使用Robolectric对android程序实现单元测试
- 将一个十六进制字符串转换为十进制数值的问题
- 课时6:PrepareStatement语句
- 气泡
- 批量上传图片时的限制
- [OpenJudge-NOI]词典 stl
- 延迟函数setTimeout()和setInterval()
- 光棍节脱单,单身狗该你上了
- php把.sql文件导入到mysql数据库
- 计算几何 常用算法模版
- Prim’s Minimum Spanning Tree