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应用场景的更多相关文章

  1. jdbc执行Statement接口的步骤

    jdbc执行Statement接口的步骤如下: 1)驱动注册程序: Class.forName(com.mysql.jdbc.Driver); 2)获取连接对象: Connection conn = ...

  2. JDBC的Statement对象

    以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/statements.html: 一旦获得了数据库的连接,就可以和数据库进行交互.JDBC的Statem ...

  3. JDBC PreparedStatement Statement

    参考:预编译语句(Prepared Statements)介绍,以MySQL为例 1. 背景 本文重点讲述MySQL中的预编译语句并从MySQL的Connector/J源码出发讲述其在Java语言中相 ...

  4. jdbc之Statement和Preparement

    Jdbc DML 操作 Statement:静态SQL操作 每次操作都会将sql语句提交到数据库执行一次,性能比较低 // 1.加载驱动程序 Class.forName(driverName); // ...

  5. jdbc中Statement和PreparedStatement有什么区别?哪个性能更好?

    Statement和PreparedStatement的功能主要是对sql语句的执行 区别 (1)Statement每执行一条sql语句就需要生成一条执行计划,执行100条就需要100条执行计划Pre ...

  6. JDBC与Statement和PreparedStatement的区别

    一.先来说说,什么是java中的Statement:Statement是java执行数据库操作的一个重要方法,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句.具体步骤: 1.首先导入 ...

  7. JDBC之Statement 接口的测试(存在sql注入风险)

    实现简单的登录功能 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; impo ...

  8. JDBC之Statement、PreparedStatement和CallableStatement

    JDBC提供了Statement.PreparedStatement和CallableStatement三种方式来执行查询语句,其中Statement用于通用查询,PreparedStatement用 ...

  9. JDBC:Statement问题

    1.Statement问题  2.解决办法:通过PreparedStatement代替  实践: package com.dgd.test; import java.io.FileInputStrea ...

随机推荐

  1. 干货 | 手把手教你搭建一套OpenStack云平台

    1 前言 今天我们为一位朋友搭建一套OpenStack云平台. 我们使用Kolla部署stein版本的OpenStack云平台. kolla是用于自动化部署OpenStack的一个项目,它基于dock ...

  2. Mysql数据库基础_复习思维导图

    Mysql复习的一个小总结,用xmind写的.(字数没有都不给我发博客) 下面是一些备注 子查询 MySQL子查询称为内部查询,而包含子查询的查询称为外部查询. 子查询可以在使用表达式的任何地方使用, ...

  3. 百万数据 mysql count(*)优化

    一.故事背景有一张 500w 左右的表做 select count(*) 速度特别慢. 二.原 SQL 分析Server version: 5.7.24-log MySQL Community Ser ...

  4. c# 读取所有磁盘的剩余空间

    介绍: 有一个控制台命令是创建指定大小的空文件,因此我想制作一个一键填充剩余磁盘空间的坑人小程序. 想要填充剩余容量,就要先获取所有本地磁盘的剩余空间,这个程序就是用来做这个的. 项目类型为c#控制台 ...

  5. 【深入理解计算机系统CSAPP】第六章 存储器层次结构

    6 存储器层次结构 存储器系统(memory system)是一个具有不同容量.成本和访问时间的存储设备的层次结构.CPU 寄存器保存着最常用的数据.靠近 CPU 的小的.快速的高速缓存存储器(cac ...

  6. 【Java8新特性】Lambda表达式

    一.Lambda 表达式 是什么? Lambda读音:拉姆达. Lambda是一个匿名函数,匿名函数就是一个没有名字的函数. Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中). ...

  7. linux篇-xshell连接突然报Connection closed by foreign host.

    1问题描述报错 Connection closed by foreign host. Disconnected from remote host(yaoGS) at 155513. 2登入虚拟机 在l ...

  8. stm32f103ve+BH1750使用教程+oled(HAL库)

    1.硬件:BH1750模块+oled 2.代码:BH1750是标准的iic协议的外设,我这里单独有iic的文件,之后想要实现多个设备共用一个iic. BH1750.c 1 #include " ...

  9. 一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue

    摘要: J.U.C是Java并发编程中非常重要的工具包,今天,我们就来着重讲讲J.U.C里面的FutureTask.Fork/Join框架和BlockingQueue. 本文分享自华为云社区<[ ...

  10. VmWare安装Centos8注意事项

    VmWare安装Centos8注意事项 1.需选择稍后安装操作系统 2.选择操作系统版本 3.修改虚拟机配置 4.配置完成点击开启虚拟机(注意要将鼠标放在屏幕中央,点击一下后才能使用上下键进行选择) ...