如何通过使用JDBC的statement进行数据操作
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,小编给大家分享一下如何通过使用JDBC的statement进行数据操作,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!使用J
千家信息网最后更新 2024年09月22日如何通过使用JDBC的statement进行数据操作
小编给大家分享一下如何通过使用JDBC的statement进行数据操作,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
使用JDBC的statement进行数据的查询,基本步骤如下:
1. 初始化simpleDbSource对象
2. 获得getconnection
3. createStatement 获得查询语句
4. executeUpdate, 执行更新语句
5. 关闭使用的statement, connection, 注意次序不要弄错
注意:更新语句,执行过一次后,column需要递增,否则报错
Java代码
/** * */ package db; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; /** * @author sean * * 1. 初始化simpleDbSource对象 * 2. 获得getconnection * 3. createStatement 获得查询语句 * 4. executeUpdate, 执行更新语句 * 5. 关闭使用的statement, connection, 注意次序不要弄错 * * 注意:更新语句,执行过一次后,column需要递增,否则报错 */ public class StatementDemo { private static String insertSql="insert into user values('7','sean','sean@mail.com','hellofromsean')"; private static String querySql ="select * from user"; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DBSource dbSource; Connection conn = null; java.sql.Statement stmt = null; try { dbSource = new SimpleDBSource(); conn = dbSource.getConnect(); stmt = conn.createStatement(); //数据库更新工作,包括create, drop, update, insert etc. stmt.executeUpdate(insertSql); System.out.println("执行成功"+ insertSql); //进行数据库查询 ResultSet rs = stmt.executeQuery(querySql); //进行遍历 while(rs.next()){ System.out.println(rs.getInt(1)+ "\t"); System.out.println(rs.getString(2)+ "\t"); System.out.println(rs.getString(3)+ "\t"); System.out.println(rs.getString(4)+ "\t"); System.out.println("**********************"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //依次关闭statement和conn数据库连接对象,清空资源 finally{ if(stmt!= null){ try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt= null; } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn= null; } } } } /** * */ package db; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; /** * @author sean * * 1. 初始化simpleDbSource对象 * 2. 获得getconnection * 3. createStatement 获得查询语句 * 4. executeUpdate, 执行更新语句 * 5. 关闭使用的statement, connection, 注意次序不要弄错 * * 注意:更新语句,执行过一次后,column需要递增,否则报错 */ public class StatementDemo { private static String insertSql="insert into user values('7','sean','sean@mail.com','hellofromsean')"; private static String querySql ="select * from user"; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DBSource dbSource; Connection conn = null; java.sql.Statement stmt = null; try { dbSource = new SimpleDBSource(); conn = dbSource.getConnect(); stmt = conn.createStatement(); //数据库更新工作,包括create, drop, update, insert etc. stmt.executeUpdate(insertSql); System.out.println("执行成功"+ insertSql); //进行数据库查询 ResultSet rs = stmt.executeQuery(querySql); //进行遍历 while(rs.next()){ System.out.println(rs.getInt(1)+ "\t"); System.out.println(rs.getString(2)+ "\t"); System.out.println(rs.getString(3)+ "\t"); System.out.println(rs.getString(4)+ "\t"); System.out.println("**********************"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //依次关闭statement和conn数据库连接对象,清空资源 finally{ if(stmt!= null){ try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt= null; } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn= null; } } } } /** * */ package db; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * @author sean * * 1. 初始化simpleDbSource对象 * 2. 获得getconnection * 3. createPreparedStatement 获得查询语句 * 4. 设置具体更新内容,setInt(colIndex, value), setString(colIndex,value) * 4. executeUpdate, 执行更新语句 * 5. 关闭使用的PreparedStatementstatement, connection, 注意次序不要弄错 * * 注意:更新语句,执行过一次后,column需要递增,否则报错 */ public class PreparedStatementDemo { private static String querySql ="select * from user"; private static String pstmtSql = "insert into user values(?,?,?,?)"; Connection conn1; static Statement stmt; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DBSource dbSource; Connection conn = null; java.sql.PreparedStatement pstmt = null; try { dbSource = new SimpleDBSource(); conn = dbSource.getConnect(); pstmt = conn.prepareStatement(pstmtSql); pstmt.setInt(1, 9); pstmt.setString(2, "sean"); pstmt.setString(3, "my@hotmail.com"); pstmt.setString(4, "add some comments"); //数据库更新工作,包括create, drop, update, insert etc. pstmt.executeUpdate(); //清空设置的参数,为后续更新准备 pstmt.clearParameters(); System.out.println("执行成功"+ pstmtSql); //进行数据库查询 Connection conn1 = dbSource.getConnect(); Statement stmt = conn1.createStatement(); ResultSet rs = stmt.executeQuery(querySql); //进行遍历 while(rs.next()){ System.out.println(rs.getInt(1)+ "\t"); System.out.println(rs.getString(2)+ "\t"); System.out.println(rs.getString(3)+ "\t"); System.out.println(rs.getString(4)+ "\t"); System.out.println("**********************"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //依次关闭jdbc的statement和conn数据库连接对象,清空资源 finally{ if(stmt!= null){ try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt= null; } if(pstmt!= null){ try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } pstmt= null; } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn= null; } } } }
以上是"如何通过使用JDBC的statement进行数据操作"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
更新
数据
语句
数据库
查询
对象
次序
成功
内容
篇文章
资源
工作
不怎么
代码
参数
大部分
更多
步骤
知识
行业
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
ug许可证服务器装不了
云网络技术打开
数据库机房布线图解
数据库统一接口组件
计算机软件开发合同纠纷二审判决
中央监控服务器
银河麒麟服务器版操作系统全指南
廊坊c 软件开发论坛
美国agis软件开发公司
每个学校的知网数据库是一样的吗
鼎盛创美互联网科技
用服务器下载
网络技术的培养目标
靠谱的生产管理软件开发定制
万方数据库的功能
与电气专业相关的数据库设计
合肥做分布式存储服务器
如何用数据库创建事务
超微服务器外部管理器
山东信威网络技术有限公司
郑州坐标软件开发有限公司
贵州时代网络技术分类市场价
赣州兆鑫软件开发
层次分析法数据库
河北周宁网络安全宣传
村居开展网络安全宣传活动
怎么向数据库添加数据
金仓数据库端口多少
开展国家网络安全学
江苏正规软件开发材料