jdbc 08: statement应用场景
jdbc连接mysql,statement的应用场景
package com.examples.jdbc.o8_statement应用场景;
import java.sql.*;
import java.util.ResourceBundle;
import java.util.Scanner;
/*
普通数据库操作对象的应用场景:
根据用户输入的:desc或者asc将查询结果集按照指定顺序输出
*/
public class Test {
//静态代码块完成资源绑定器对配置文件属性的绑定
public static void main(String[] args) {
//commonStatement();
preparedStatement();
}
/**
* 预处理数据库操作对象
*/
private static void preparedStatement(){
//获取用户输入的desc 或者 asc
Scanner scanner = new Scanner(System.in);
System.out.println("请输入desc 或者 asc,desc代表降序排列,asc代表升序排列");
System.out.println("请输入:");
String orderType = scanner.nextLine();
//资源绑定器绑定配置属性文件
ResourceBundle resourceBundle = ResourceBundle.getBundle("config/jdbc");
String driver = resourceBundle.getString("driver");
String url = resourceBundle.getString("url");
String userName = resourceBundle.getString("userName");
String passWord = resourceBundle.getString("passWord");
//3个资源对象
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//jdbc数据库连接6步骤
try {
//1.
Class.forName(driver);
//2.
connection = DriverManager.getConnection(url, userName, passWord);
//3.
//String sql = "select * from tb_user order by uname ?";
//preparedStatement = connection.prepareStatement(sql);
//preparedStatement.setString(1, orderType);
//4.
resultSet = preparedStatement.executeQuery();
//5.
while(resultSet.next()){
int id = resultSet.getInt("id");
String uname = resultSet.getString("uname");
String upasswd = resultSet.getString("upasswd");
System.out.println("id: " + id + " uname: " + uname + " upasswd: " + upasswd);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//6.
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement != null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* 普通数据库操作对象的应用场景
*/
private static void commonStatement() {
//获取用户输入的desc 或者 asc
Scanner scanner = new Scanner(System.in);
System.out.println("请输入desc 或者 asc,desc代表降序排列,asc代表升序排列");
System.out.println("请输入:");
String orderType = scanner.nextLine();
//资源绑定器绑定配置属性文件
ResourceBundle resourceBundle = ResourceBundle.getBundle("config/jdbc");
String driver = resourceBundle.getString("driver");
String url = resourceBundle.getString("url");
String userName = resourceBundle.getString("userName");
String passWord = resourceBundle.getString("passWord");
//3个资源对象
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
//jdbc数据库连接6步骤
try {
//1.
Class.forName(driver);
//2.
connection = DriverManager.getConnection(url, userName, passWord);
//3.
statement = connection.createStatement();
//4.
String sql = "select * from tb_user order by upasswd " + orderType;
resultSet = statement.executeQuery(sql);
//5.
while(resultSet.next()){
int id = resultSet.getInt("id");
String uname = resultSet.getString("uname");
String upasswd = resultSet.getString("upasswd");
System.out.println("id: " + id + " uname: " + uname + " upasswd: " + upasswd);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//6.
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
jdbc 08: statement应用场景的更多相关文章
- jdbc执行Statement接口的步骤
jdbc执行Statement接口的步骤如下: 1)驱动注册程序: Class.forName(com.mysql.jdbc.Driver); 2)获取连接对象: Connection conn = ...
- JDBC的Statement对象
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/statements.html: 一旦获得了数据库的连接,就可以和数据库进行交互.JDBC的Statem ...
- JDBC PreparedStatement Statement
参考:预编译语句(Prepared Statements)介绍,以MySQL为例 1. 背景 本文重点讲述MySQL中的预编译语句并从MySQL的Connector/J源码出发讲述其在Java语言中相 ...
- jdbc之Statement和Preparement
Jdbc DML 操作 Statement:静态SQL操作 每次操作都会将sql语句提交到数据库执行一次,性能比较低 // 1.加载驱动程序 Class.forName(driverName); // ...
- jdbc中Statement和PreparedStatement有什么区别?哪个性能更好?
Statement和PreparedStatement的功能主要是对sql语句的执行 区别 (1)Statement每执行一条sql语句就需要生成一条执行计划,执行100条就需要100条执行计划Pre ...
- JDBC与Statement和PreparedStatement的区别
一.先来说说,什么是java中的Statement:Statement是java执行数据库操作的一个重要方法,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句.具体步骤: 1.首先导入 ...
- JDBC之Statement 接口的测试(存在sql注入风险)
实现简单的登录功能 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; impo ...
- JDBC之Statement、PreparedStatement和CallableStatement
JDBC提供了Statement.PreparedStatement和CallableStatement三种方式来执行查询语句,其中Statement用于通用查询,PreparedStatement用 ...
- JDBC:Statement问题
1.Statement问题 2.解决办法:通过PreparedStatement代替 实践: package com.dgd.test; import java.io.FileInputStrea ...
随机推荐
- 按照 Promise/A+ 规范逐行注释并实现 Promise
0. 前言 面试官:「你写个 Promise 吧.」 我:「对不起,打扰了,再见!」 现在前端越来越卷,不会手写 Promise 都不好意思面试了(手动狗头.jpg).虽然没多少人会在业务中用自己实现 ...
- AC自动机:Tire树+KMP
简介 AC自动机是一个多模式匹配算法,在模式匹配领域被广泛应用,举一个经典的例子,违禁词查找并替换为***.AC自动机其实是Trie树和KMP 算法的结合,首先将多模式串建立一个Tire树,然后结合K ...
- grafana展示zabbix统计
1.安装grafana 参照官网文档:https://grafana.com/grafana/download 我这边是centos系统,执行这两个命令 wget https://dl.grafa ...
- Nginx禁止使用IP访问
在nginx的访问日志中,会出现只显示IP,而不出现域名的情况,在经过尝试之后,是因为没有设置禁止IP访问导致的. 下面就是在配置文件中设置禁止IP访问,来实现日志文件中$host显示域名. vim ...
- git提交时写message的规范
message规范 angular示例 commit message(提交说明) git commit -m "写一行提交说明" # 跳出文本编辑器,写多行 git commit ...
- 2020级C++实验课-期末机考模拟考题解
做这个题解的理由很简单,有很多同学想写但是不会写,凑巧我写了,所以搞个题解. 顺序就是题单里的顺序(界面左上角菜单切换文章,右上角目录方便查看) 1:黑马白马 题意: 随机得到一个数字,如果是偶数,则 ...
- python面向对象(封装、多态、反射)
目录 面向对象之封装 @property 面向对象之多态 面向对象之反射 面向对象之封装 含义 将类中的某些名字按照特殊的书写方式"隐藏"起来,不让外界直接调用,目的是为了不然外界 ...
- 《Mybatis 手撸专栏》第10章:使用策略模式,调用参数处理器
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 你这代码写的,咋这么轴呢! 说到轴,让我想起初中上学时老师说的话:"你那脑 ...
- java-去除html中的标签或者元素属性(正则表达式/jsoup)
业务场景: 如一篇使用富文本编辑器编辑的新闻稿,需要在列表页面截取前200字作为摘要,此时需要去除html标签,截取真正的文本部分. /** * 删除Html标签 */public static St ...
- Java_循环结构
目录 while循环 do...while循环 for循环 for循环嵌套 增强for循环 打印三角形 Debug 视频 while循环 while(布尔表达式){ //循环内容 } //死循环 wh ...