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();
}
}
}
}
}

最新文章

  1. Unity 序列化 总结
  2. .NET Framework 基础知识总结
  3. SQL Server 格式化时间format
  4. HINSTANCE数据类型
  5. [原创作品]Javascript内存管理机制
  6. Qt直接使用OpenSSL里的函数
  7. Linux常用C函数---字符测试篇
  8. Mybatis 的Log4j日志输出问题 - 以及有关日志的所有问题
  9. windows下python的安装
  10. .elf格式内容
  11. JDBC 程序实例小练习
  12. CoordinatorLayout
  13. JavaScript DOM 高级程序设计读书笔记二
  14. Ocelot简易教程(七)之配置文件数据库存储插件源码解析
  15. Python——lambda函数
  16. netty源码解解析(4.0)-8 ChannelPipeline的设计
  17. XQN number format
  18. webstorm版本2017.2开发stylus报错
  19. Codeforces821C Okabe and Boxes 2017-06-28 15:24 35人阅读 评论(0) 收藏
  20. [na]pc加入域认证细节

热门文章

  1. Spring Boot 启动源码解析结合Spring Bean生命周期分析
  2. 漫谈 HTTP 连接
  3. JuiceFS 在数据湖存储架构上的探索
  4. AliIAC 智能音频编解码器:在有限带宽条件下带来更高质量的音频通话体验
  5. zipper题解
  6. kNN-画图
  7. 最新版2022年任我行管家婆工贸版ERP M7 V22.0进销存财务生产管理软件网络版——云上的集团化制造管理系统
  8. js--promise、async 和 await 相关知识总结
  9. 使用RoslynSyntaxTool工具互相转换C#代码与语法树代码
  10. unity---世界坐标和本地坐标的转换