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 ...
随机推荐
- 常用的Linux 系统备份、恢复命令
公众号关注 「开源Linux」 回复「学习」,有我为您特别筛选的学习资料~ 删库跑路的事常常听说,不过,这只能是个调侃的话题,真正的工作中可不能这么干,否则,库是删了,路怕是跑不了了. 所以,备份很重 ...
- django的下载安装,目录结构的介绍,简单的django应用
1.django的下载安装 pip3 install django==1.11.9 2.django的创建 1.在windows的cmd窗口下 1.1创建django项目 django-admin s ...
- linux下的mysql数据库以及mysql主从复制
参考博客 1.mysql数据库的安装 centos7如何安装mysql 1.yum安装 1.1首先配置yum源,然后再用yum进行安装 2. 源代码编译安装 3.rpm包安装 yum安装的前提条件,是 ...
- R可视化:plot函数基础操作,小白教程
最近迷恋上了画图,一方面是觉得挺有意思的,另一方面是觉得自己确实画图方面比较弱,所以决定比较系统地回顾反思一下,同时顺带记录下来分享给大家.也确实是好久好久没更新文章了,真的是杂事太多太忙太牵扯精力没 ...
- 手写vue路由
目录 一.简易demo 二.Vue-Router传参方式 三.进阶-路由导航 一.简易demo // routes注册 import Vue from "vue"; // impo ...
- 关于Linux添加字体
安装字体命令 yum -y install fontconfig 查看已经安装的字体 fc-list # 查看已经已经安装的中文字体 fc-list :lang=zh 在字体目录下创建新的目录或者使用 ...
- Fast-Rcnn学习笔记
Fast-Rcnn学习笔记 paper code Fast-RCNN总览 step1:图片先放进卷积层 step2:再卷积层的特征图谱上回映射出对应的感兴趣区域 step3:集过一层ROI Pooli ...
- SmartIDE v0.1.18 已经发布 - 助力阿里国产IDE OpenSumi 插件安装提速10倍、Dapr和Jupyter支持、CLI k8s支持
SmartIDE v0.1.18 (cli build 3538) 已经发布,在过去的Sprint 18中,我们集中精力推进对 k8s 远程工作区 的支持,同时继续扩展SmartIDE对不同技术栈的支 ...
- Python数据分析--Numpy常用函数介绍(6)--Numpy中矩阵和通用函数
在NumPy中,矩阵是 ndarray 的子类,与数学概念中的矩阵一样,NumPy中的矩阵也是二维的,可以使用 mat . matrix 以及 bmat 函数来创建矩阵. 一.创建矩阵 mat 函数创 ...
- ConfigurationManager姿势快闪
C# ConfigurationManager使用记录 最近一个祖传代码是使用.NET Fx写就的,我在使用控制台程序获取配置时有些折腾. 下面记录一些管理配置文件的姿势: Configuration ...