Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之06.JDBC PreparedStatement
–为占位符“?”赋值
–使用PreparedStatement动态执行SQL语句
–Statement只能静态操作SQL语句,如果要想动态操作SQL语句又该如何实现呢?例如:注册会员
–这里可以使用PreparedStatement来动态操作SQL语句
–PreparedStatement通过使用占位符“?”,来预生成SQL语句,从而达到动态操作的功能
–根据当前SQL的数据类型
–insert
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class ConnectionUtil {
//第一种方法
public Connection getConnection(){
Connection conn = null;
try {
//Class.forName加载驱动
Class.forName("com.mysql.jdbc.Driver");
//DriverManager获得连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin");
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//第二种方法
public Connection getConnection(String driver,String url,String user,String password){
Connection conn = null;
try {
//Class.forName加载驱动
Class.forName(driver);
//DriverManager获得连接
conn = DriverManager.getConnection(url,user,password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//第三种方法
public Connection openConnection(){
String driver = "";
String url = "";
String user = "";
String password = "";
Properties prop = new Properties();
Connection conn = null;
try {
//加载属性文件
prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
//Class.forName加载驱动
Class.forName(driver);
//DriverManager获得连接
conn = DriverManager.getConnection(url,user,password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
public class Customer {
private int id;
private String name;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestPrepareStatement {
public static void add(Customer c){
Connection conn = new ConnectionUtil().openConnection();
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) {
e.printStackTrace();
}
}
}
import com.michael.jdbc.Customer;
import com.michael.jdbc.TestPrepareStatement;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Customer c = new Customer();
c.setName("51blog");
c.setEmail("51blog@51blog.com");
TestPrepareStatement.add(c);
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestPrepareStatement {
public static void delete(int id){
Connection conn = new ConnectionUtil().openConnection();
String sql = "delete from CustomerTbl where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import com.michael.jdbc.TestPrepareStatement;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
/*
Customer c = new Customer();
c.setName("51blog");
c.setEmail("51blog@51blog.com");
TestPrepareStatement.add(c);
*/
//删除ID为2的用户
TestPrepareStatement.delete(2);
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestPrepareStatement {
public static void update(String name){
Connection conn = new ConnectionUtil().openConnection();
String sql = "update CustomerTbl set name = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Main.java
package com.michael.main;
import com.michael.jdbc.TestPrepareStatement;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
/*
Customer c = new Customer();
c.setName("51blog");
c.setEmail("51blog@51blog.com");
TestPrepareStatement.add(c);
*/
//删除ID为2的用户
//TestPrepareStatement.delete(2);
TestPrepareStatement.update("Michael");
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestPrepareStatement {
public static void update(String name1,String name2){
Connection conn = new ConnectionUtil().openConnection();
String sql = "update CustomerTbl set name = ? where name = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name1);
pstmt.setString(2, name2);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Main.java
package com.michael.main;
import com.michael.jdbc.TestPrepareStatement;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
/*
Customer c = new Customer();
c.setName("51blog");
c.setEmail("51blog@51blog.com");
TestPrepareStatement.add(c);
*/
//删除ID为2的用户
//TestPrepareStatement.delete(2);
//TestPrepareStatement.update("Michael");
TestPrepareStatement.update("Michael","51cto");
}
}
–query
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class TestPrepareStatement {
public static List query(String name){
Connection conn = new ConnectionUtil().openConnection();
String sql = "select * from CustomerTbl where name = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
List list = new ArrayList();
while(rs.next()){
int id = rs.getInt(1);
String email = rs.getString(3);
Customer c = new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
list.add(c);
}
System.out.println(list.size());
return list;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
import com.michael.jdbc.TestPrepareStatement;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
/*
Customer c = new Customer();
c.setName("51blog");
c.setEmail("51blog@51blog.com");
TestPrepareStatement.add(c);
*/
//删除ID为2的用户
//TestPrepareStatement.delete(2);
//TestPrepareStatement.update("Michael");
//TestPrepareStatement.update("Michael","51cto");
TestPrepareStatement.query("Michael");
}
}
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之06.JDBC PreparedStatement相关推荐
- Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之14.Servlet请求头信息
–典型的请求头信息 –读取HTTP请求头 –使用表格显示所有请求头信息 –理解各种请求头的含义 –区分不同的浏览器类型 ##############Michael分割线################ ...
- JDBC+Servlet+JSP整合开发之25.JSP动作元素
–jsp:useBean –jsp:setProperty –jsp:getProperty –jsp:forward –jsp:include –jsp:param –实例 ?计算器 ------- ...
- JDBC+Servlet+JSP整合开发之22.JSP简介
–对JSP的需求 –JSP的结构 –JSP的好处 –JSP实例 ?创建一个简单的JSP页面 ########################################### ? JSP –JSP ...
- JDBC+Servlet+JSP整合开发之26.JSP内建对象
–使用内建对象的目的 –内建对象 –out 内建对象 –request 内建对象 –response 对象 –session 内建对象 –pageContext 内建对象 –applic ...
- Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之12.Servlet基础(2)
–提交表单的方法 • get • post –Servlet 生命周期 –使用Servlet 输出HTML页面 –获得Servlet初始化参数 –页面导航 • 请求重定向 –response.send ...
- Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之13.Form表单处理(1)
–Form 表单简介 –创建并提交表单 –使用Servlet处理表单 • 读取单个请求参数 • 读取多个表单 • 读取所有参数名称 –实例 • 注册会员 ###############Michael分 ...
- Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之10.Web_工程结构
–简介 –Web应用程序的思想 –Web应用程序的目的 –Web工程结构 –web.xml 文件 –实例 • 创建一个简单的web应用程序 • 部署到tomcat中来运行 ############## ...
- JDBC+Servlet+JSP整合开发之30-JDBC、Servlet、JSP的MVC
–Servlet 的优势与弊端 –JSP 的优势与弊端 –MVC 设计模式 –实例 ?使用MVC实现学生信息的添加.显示 -----------------------------START----- ...
- JDBC+Servlet+JSP整合开发之29-JSP表达式语言(EL)
–EL 简介 –EL的应用场合 –EL 的基本语法 –EL中的算术运算符 –EL中的关系运算符 –EL中的逻辑运算符 ------------------------------START- ...
最新文章
- 其他算法-PCA主成分分析
- JAVA-JSP Scriptlets(JSP小脚本)
- ReentrantLock与公平锁、非公平锁实现
- 【Protocol Buffer】Protocol Buffer入门教程(一):简介和安装
- Flex更改Image
- 如何使用粒子氩气进行位置跟踪
- 大数据学习笔记33:解决CentOS7上MySQL启动失败问题
- 7-12 方阵循环右移 (20 分)
- 阿里巴巴编码规范(java)考核
- 每周分享第 24 期
- Windows Server 2012 AD域管理创建
- jt808终端鉴权_JT/T808协议文档-道路运输车辆卫星定位系统北斗兼容车载终端通讯协议技术规范.pdf...
- IntelliJ IDEA启动tomcat报错:Disconnected from server
- 深度增强学习射击类游戏(vizdoom)
- 初始C语言——字符串,转义字符,循环语句
- AboutServer
- C语言练习(一球从M米高度自由下落,每次落地后返回原高度的一半,再落下,它在第N次落地时共经过多少米?反弹多高?)
- 数据库第十次作业-视图【带源码】
- SAP FI系统配置-资产相关
- Android音乐App桌面图标制作以及启动页面开发(简易音乐 一)