吴裕雄--天生自然JAVA数据库编程:PrepareStatement
2024-10-07 23:18:14
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.PreparedStatement ;
import java.text.SimpleDateFormat ;
public class PreparedStatementDemo01 {
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
// 定义MySQL数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
// MySQL数据库的连接用户名
public static final String DBUSER = "root" ;
// MySQL数据库的连接密码
public static final String DBPASS = "mysqladmin" ;
public static void main(String args[]) throws Exception{ // 所有异常抛出
Connection conn = null ; // 数据库连接
PreparedStatement pstmt = null ; // 数据库操作
String name = "李兴华" ; // 姓名
String password = "www.mldnjava.cn" ; // 密码
int age = 30 ; // 年龄
String sex = "男" ; // 性别
String birthday = "2007-08-27" ; // 生日
java.util.Date temp = null ;
temp = new SimpleDateFormat("yyyy-MM-dd").parse(birthday) ;
java.sql.Date bir = new java.sql.Date(temp.getTime()) ;
String sql = "INSERT INTO user(name,password,age,sex,birthday) VALUES (?,?,?,?,?) " ;
Class.forName(DBDRIVER) ; // 加载驱动程序
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
pstmt = conn.prepareStatement(sql) ; // 实例化PreapredStatement对象
pstmt.setString(1,name) ;
pstmt.setString(2,password) ;
pstmt.setInt(3,age) ;
pstmt.setString(4,sex) ;
pstmt.setDate(5,bir) ;
int t = pstmt.executeUpdate() ; // 执行更新
System.out.println(t);
pstmt.close() ;
conn.close() ; // 数据库关闭
}
};
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.ResultSet ;
import java.sql.PreparedStatement ;
import java.text.SimpleDateFormat ;
public class PreparedStatementDemo02 {
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
// 定义MySQL数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
// MySQL数据库的连接用户名
public static final String DBUSER = "root" ;
// MySQL数据库的连接密码
public static final String DBPASS = "mysqladmin" ;
public static void main(String args[]) throws Exception{ // 所有异常抛出
Connection conn = null ; // 数据库连接
PreparedStatement pstmt = null ; // 数据库操作
String keyWord = "李" ; // 设置查询关键字
ResultSet rs = null ; // 接收查询结果
String sql = "SELECT id,name,password,age,sex,birthday " +
" FROM user WHERE name LIKE ? OR password LIKE ? OR sex LIKE ?" ;
Class.forName(DBDRIVER) ; // 加载驱动程序
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
pstmt = conn.prepareStatement(sql) ; // 实例化PreapredStatement对象
pstmt.setString(1,"%"+keyWord+"%") ;
pstmt.setString(2,"%"+keyWord+"%") ;
pstmt.setString(3,"%"+keyWord+"%") ;
rs = pstmt.executeQuery() ; // 执行查询
while(rs.next()){
int id = rs.getInt(1) ;
String name = rs.getString(2) ;
String pass = rs.getString(3) ;
int age = rs.getInt(4) ;
String sex = rs.getString(5) ;
java.util.Date d = rs.getDate(6) ;
System.out.print("编号:" + id + ";") ;
System.out.print("姓名:" + name + ";") ;
System.out.print("密码:" + pass + ";") ;
System.out.print("年龄:" + age + ";") ;
System.out.print("性别:" + sex + ";") ;
System.out.println("生日:" + d + ";") ;
System.out.println("-------------------------") ;
}
rs.close() ;
pstmt.close() ;
conn.close() ; // 数据库关闭
}
};
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.ResultSet ;
import java.sql.PreparedStatement ;
import java.text.SimpleDateFormat ;
public class PreparedStatementDemo03 {
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
// 定义MySQL数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
// MySQL数据库的连接用户名
public static final String DBUSER = "root" ;
// MySQL数据库的连接密码
public static final String DBPASS = "mysqladmin" ;
public static void main(String args[]) throws Exception{ // 所有异常抛出
Connection conn = null ; // 数据库连接
PreparedStatement pstmt = null ; // 数据库操作
String keyWord = "李" ; // 设置查询关键字
ResultSet rs = null ; // 接收查询结果
String sql = "SELECT id,name,password,age,sex,birthday " +
" FROM user" ;
Class.forName(DBDRIVER) ; // 加载驱动程序
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
pstmt = conn.prepareStatement(sql) ; // 实例化PreapredStatement对象
rs = pstmt.executeQuery() ; // 执行查询
while(rs.next()){
int id = rs.getInt(1) ;
String name = rs.getString(2) ;
String pass = rs.getString(3) ;
int age = rs.getInt(4) ;
String sex = rs.getString(5) ;
java.util.Date d = rs.getDate(6) ;
System.out.print("编号:" + id + ";") ;
System.out.print("姓名:" + name + ";") ;
System.out.print("密码:" + pass + ";") ;
System.out.print("年龄:" + age + ";") ;
System.out.print("性别:" + sex + ";") ;
System.out.println("生日:" + d + ";") ;
System.out.println("-------------------------") ;
}
rs.close() ;
pstmt.close() ;
conn.close() ; // 数据库关闭
}
};
最新文章
- [C1] C1FlexGrid 排除非绑定列的验证效果
- VS工具如何新建筛选器
- 微信 {";errcode";:40029,";errmsg";:";invalid code, hints: [ req_id: Cf.y.a0389s108 ]";}
- Spring MVC配置
- Delphi XE5 android 获取网络状态
- Oracle bbed 实用示例-----修改Data内容、恢复delete的rows
- golang实现tcp接入服务器
- easyui 翻译
- html <;select>; 用JS控制默认选中项
- MD5摘要算法简析
- oneNote总结
- ISLR系列:(4.3)模型选择 PCR &; PLS
- hyper发送表单数据
- angular ViewChild ContentChild 系列的查询参数
- 关于oracle 索引,收藏
- 从word得到表格数据插入数据库(6位行业代码)
- POJ-1679 The Unique MST (判断最小生成树的唯一性)
- Python设计模式 - UML - 组件图(Component Diagram)
- [No0000115]打开Excel2016提示内存或磁盘空间不足的解决方法
- linux 系统 目录,以部分及相关命令
热门文章
- 【PAT甲级】1055 The World&#39;s Richest (25 分)
- C++11常用特性介绍——Lambda表达式
- 安装mysql常见错误解决方法
- redhat7.6 httpd配置php模块
- 浅谈CVE-2018-12613文件包含/buuojHCTF2018签到题Writeup
- 【网摘自CSDN网站】当文本内容过多,展开更多 (css控制,)
- 学习 Python,怎能不懂点PEP 呢?
- springboot devtool热部署的一个大坑
- 【转】python创建和删除文件
- c++刷算法的好处