当前位置:网站首页>孙老师版本JDBC(2022年6月12日21:34:25)
孙老师版本JDBC(2022年6月12日21:34:25)
2022-06-12 21:47:00 【红目香薰】
数据库自动关闭,方便多了。
package com.item.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCUtil implements AutoCloseable {
private static String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://127.0.0.1:3306/mytest?characterEncoding=utf-8";
// 连接对象
private static Connection conn;
// 结果集
private static ResultSet rs;
// 代码块
static{
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, "root", "12345678");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 全SQL查询
* @param sql
* @return
* @throws SQLException
*/
public static ResultSet query(String sql) throws SQLException {
return conn.createStatement().executeQuery(sql);
}
/**
* @param sql
* @return
* @throws SQLException
*/
public static int update(String sql) throws SQLException {
return conn.prepareStatement(sql).executeUpdate();
}
@Override
public void close() throws Exception {
if (null != rs) {
rs.close();
}
if (null != conn) {
conn.close();
}
}
}数据测试:
public static void main(String[] args) throws SQLException {
ResultSet re = query("select * from users");
while (re.next()) {
System.out.println(re.getObject(1));
System.out.println(re.getObject(2));
System.out.println(re.getObject(3));
System.out.println(re.getObject(4));
System.out.println(re.getObject(5));
}
}
swing
/**
* 转换为Vectors集合
*
* @param userList
* @return
*/
public static Vector<Vector<String>> listToVectors(List<UserVo> userList) {
Vector<Vector<String>> datas = new Vector<Vector<String>>();
for (int i = 0; i < userList.size(); i++) {
UserVo u = userList.get(i);
if (null == u)
continue;
Vector<String> item = new Vector<String>();
item.add(u.getId().toString());
item.add(u.getAccount());
item.add(u.getSex() == 1 ? "男" : "女");
item.add(u.getEducation());
item.add(u.getPhone());
item.add(u.getEmail());
item.add(u.getRegisterDate());
if (null == u.getUpdateDate()) {
item.add("");
}else {
item.add(u.getUpdateDate());
}
datas.add(item);
}
return datas;
}三行实现JTable数据更新。
datas.clear();
datas.addAll(ListDbUtils.listToVectors(userService.list()));
jTable.updateUI();
JTable jTable = new JTable(datas, heads);这个方便快捷一些,但是javaEE的还没有测试。后面我搞个完整的就都知道了。
边栏推荐
- Xingda easy control ModbusRTU to modbustcp gateway
- MySQL introduction and installation (I)
- Okio source code analysis
- Jdbctemplate inserts and returns the primary key
- SQL调优指南笔记6:Explaining and Displaying Execution Plans
- SQL调优指南笔记16:Managing Historical Optimizer Statistics
- SQL调优指南笔记8:Optimizer Access Paths
- Oracle SQL Developer的代码输入框中推荐使用的中文字体
- 如何自己动手写一个vscode插件,实现插件自由!
- Exception encountered by selenium operation element
猜你喜欢

MySQL体系结构及基础管理(二)
![[target detection] |dive detector into box for object detection new training method based on fcos](/img/ac/c54c2733dceffea086b772f35f128a.png)
[target detection] |dive detector into box for object detection new training method based on fcos

Shell script Basics

JVisualVM初步使用

利用ADG Standby克隆PDB

Ansible playbook和Ansible Roles(三)

MySql主从复制

SQL tuning guide notes 18:analyzing statistics using optimizer statistics Advisor

PCB封装下载网站推荐及其详细使用方法

MySQL introduction and installation (I)
随机推荐
SQL调优指南笔记16:Managing Historical Optimizer Statistics
Ansible playbook and ansible roles (III)
Oracle livelabs experiment: introduction to Oracle Spatial
The 2023 campus recruitment officially opened! Oceanbase would like to make an interview with you this spring
Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?
What is the race condition? How do you find and solve the competition?
Limit of advanced mathematics
What is embedded
脱颖而出!OceanBase 入选 2021“科创中国”开源创新榜单
MySQL介绍和安装(一)
【QNX Hypervisor 2.2 用户手册】4.2 支持的构建环境
jsonUtils
#yyds干货盘点# 解决剑指offer:字符流中第一个不重复的字符
Simple understanding of cap and base theory
Common error in script execution: build sh: caller: not found
gzip压缩解压缩
回文链表及链表相交问题(和心怡的人相交)你真的会了吗?
JUC并发工具包使用指南
Cookies and sessions
[qnx hypervisor 2.2 manuel de l'utilisateur] 4.2 environnement de construction pris en charge