课时6:PrepareStatement语句
2015-11-05 11:11
337 查看
1.PreparedStatement为预定义语句,继承Statement。
2.Statement只能静态操作SQL语句,如果要想动态操作SQL语句又该如何实现呢?例如:注册会员,这里可以使用PreparedStatement来动态操作SQL语句,PreparedStatement通过使用占位符“?”,来预生成SQL语句,从而达到动态操作的功能
3.实例
3.1
面向对象,就是尽量把对象定义成一个java文件,所有属性和操作都定义到里面。在其他函数中直接创建对象和调用对象
![](file:///C:/Users/bill/AppData/Local/YNote/data/bin890607@126.com/a9cccdf3795449528fda40fc085b7135/attachment.png)
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;
}
}
2.Statement只能静态操作SQL语句,如果要想动态操作SQL语句又该如何实现呢?例如:注册会员,这里可以使用PreparedStatement来动态操作SQL语句,PreparedStatement通过使用占位符“?”,来预生成SQL语句,从而达到动态操作的功能
3.实例
3.1
面向对象,就是尽量把对象定义成一个java文件,所有属性和操作都定义到里面。在其他函数中直接创建对象和调用对象
![](file:///C:/Users/bill/AppData/Local/YNote/data/bin890607@126.com/a9cccdf3795449528fda40fc085b7135/attachment.png)
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;
}
}
相关文章推荐
- HTML学习 <10>
- [OpenJudge-NOI]词典 stl
- Android Studio使用Robolectric对android程序实现单元测试
- 对NSURLConnection的简单封装
- 批量上传图片时的限制
- Microhard P900 900MHz跳频电台核心模块
- FileInfo-文件类
- java学习4-继承
- Elasticsearch随笔 01
- 剑指offer(二)
- Android学习笔记:实现上拉加载更多数据的ListView
- 2015年大二上-数据结构-链表(6)-循环双链表应用
- OC初期-08 属性,点语法,KVC
- JAVA的可变类与不可变类
- 网上流传的内核开发面试题答案--欢迎指正
- java中两种类型变量
- 浅谈HTML中的块级元素和内联元素
- 复制歌曲文件
- 音频重采样造成音质损失的原理
- git命令