JDBC示例程序
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; import org.apache.commons.dbcp.BasicDataSource; public class DBUtils {
private static BasicDataSource dateSource;
static {
//创建属性对象
Properties prop = new Properties();
//得到文件的输入流
InputStream ips = DBUtils2.class.getClassLoader().getResourceAsStream("jdbc.properties");
//把文件加载到属性对象中
try {
prop.load(ips);
//读取数据
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
//创建数据源对象
dateSource = new BasicDataSource();
//设置数据库链接信息
dateSource.setDriverClassName(driver);
dateSource.setUrl(url);
dateSource.setUsername(username);
dateSource.setPassword(password);
//设置连接池参数
dateSource.setInitialSize(3);//初始连接数量
dateSource.setMaxActive(5);//最大连接数量 } catch (IOException e) {
e.printStackTrace();
} }
//1、获取链接
public static Connection getConn() throws Exception { //获取连接池中的连接
Connection conn = dateSource.getConnection();
return conn;
}
//2、关闭资源
public static void close(ResultSet rs,Statement stat,Connection conn) {
try {
if (rs!=null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stat!=null) {
stat.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
//关闭连接
try {
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
DAO
/**
* 依据用户名查询对应的用户信息。 如果找不到,返回null。
*
* @throws SQLException
*/
public User find(String uname) throws SQLException {
User user = null; Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null; try {
conn = DBUtils.getconn();
String sql = "SELECT * FROM t_user " + "WHERE username=?";
ps = conn.prepareStatement(sql);
ps.setString(1, uname);
rs = ps.executeQuery(); if (rs.next()) {
int id = rs.getInt("id");
String pwd = rs.getString("password");
String email = rs.getString("email"); user = new User();
user.setId(id);
user.setUname(uname);
user.setPwd(pwd);
user.setEmail(email); } } catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
DBUtils.close(rs, ps, conn);
} return user;
}
/**
* 删除指定信息
* @param id
* @throws SQLException
*/
public void delete(int id) throws SQLException {
Connection conn = null;
PreparedStatement ps = null; try {
conn = DBUtils.getconn();
String sql = "DELETE FROM t_user " + "WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate(); } catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
DBUtils.close(null, ps, conn);
}
} /**
* 将用户信息插入到t_user表。
*
* @throws SQLException
*
*/
public void save(User user) throws SQLException {
Connection conn = null;
PreparedStatement ps = null; try {
conn = DBUtils.getconn();
String sql = "INSERT INTO t_user " + "VALUES(null,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUname());
ps.setString(2, user.getPwd());
ps.setString(3, user.getEmail());
ps.executeUpdate(); } catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
DBUtils.close(null, ps, conn);
}
} /**
* 从t_user表中查询出所有用户的信息。 注: 一条记录对应一个User对象(即将记录中的数据 存放到User对象里面)。
*
* @throws SQLException
*/
public List<User> findAll() throws SQLException { List<User> users = new ArrayList<User>(); Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null; try {
conn = DBUtils.getconn();
String sql = "SELECT * FROM t_user";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(); while (rs.next()) { int id = rs.getInt("id");
String uname = rs.getString("username");
String pwd = rs.getString("password");
String email = rs.getString("email"); User user = new User();
user.setId(id);
user.setUname(uname);
user.setPwd(pwd);
user.setEmail(email); users.add(user); } } catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
DBUtils.close(rs, ps, conn);
} return users; }
Java中访问数据库的步骤
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3", "root", "");
System.out.println("创建完毕");
//创建Statement
Statement stat = conn.createStatement();
String sql = "delete from jdbc01 where id=1";
//执行sql语句(若SQL语句为查询语句需要处理结果集)
stat.executeUpdate(sql);
System.out.println("删除完毕");
//关闭连接
stat.close();
conn.close();
数据库的基本连接
public static void main(String[] args) throws Exception {
//创建数据源对象
BasicDataSource dateSource = new BasicDataSource();
//设置数据库连接信息
dateSource.setDriverClassName("com.mysql.jdbc.Driver");
dateSource.setUrl("jdbc:mysql://localhost:3306/db3");
dateSource.setUsername("root");
dateSource.setPassword("root");
//设置连接池参数
dateSource.setInitialSize(3);//初始连接数量
dateSource.setMaxActive(5);//最大连接数量
//获取连接池中的连接
Connection conn = dateSource.getConnection();
System.out.println(conn);
}
JDBC示例程序的更多相关文章
- 通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...
- .NET跨平台:在Ubuntu上用自己编译的dnx运行ASP.NET 5示例程序
在 Linux Ubuntu 上成功编译 dnx 之后,会在 artifacts/build/ 文件夹中生成 dnx-coreclr-linux-x64/ 与 dnx-mono/ 这2个文件夹,前者是 ...
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
在之前的博文中我们在 Ubuntu 上成功编译出了 dnx ,并且用它成功运行了 ASP.NET 5 示例程序.在这篇博文中我们将 Ubuntu 换成 CentOS. 目前 dnx 的编译需要用到 m ...
- Salesforce Apex 使用JSON数据的示例程序
本文介绍了一个在Salesforce Apex中使用JSON数据的示例程序, 该示例程序由以下几部分组成: 1) Album.cls, 定了了封装相关字段的数据Model类 2) RestClient ...
- osg 示例程序解析之osgdelaunay
osg 示例程序解析之osgdelaunay 转自:http://lzchenheng.blog.163.com/blog/static/838335362010821103038928/ 本示例程序 ...
- DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版原创发布-带官方示例程序版
关于 DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版------------- ...
- DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版原创发布-带官方示例程序版
关于 DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...
- DotNetBar for Windows Forms 12.2.0.7_冰河之刃重打包版原创发布-带官方示例程序版
关于 DotNetBar for Windows Forms 12.2.0.7_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...
- ACEXML解析XML文件——简单示例程序
掌握了ACMXML库解析XML文件的方法后,下面来实现一个比较完整的程序. 定义基本结构 xml文件格式如下 <?xml version="1.0"?> <roo ...
随机推荐
- ubuntu16 mysql在线安装
输入"sudo apt-get update"-->回车-->"输入root用户的密码"-->回车: 输入"sudo apt-get ...
- css基础回顾
1.css选择器分类: id选择器,类选择器,通用选择器, 包含(后代)选择器——加入空格,用于选择指定标签元素下的后辈元素. 子选择器(大于符号)——用于指定标签元素的第一代子元素. 伪类选择器—— ...
- iOS TouchID & FaceID
import UIKit import LocalAuthentication //指纹识别必须用真机测试,并且在iOS8以上系统,如果是FaceID至少IOS11以上. class Authenti ...
- 像纸质笔记本一样给div,textarea添加行的分割线
想要给textarea添加一个背景图来实现 但是背景图有几个问题, 1.每个div或者textarea的line-height不一样,对于每个不同的line-height都需要一个不同的背景图 2.当 ...
- SpringBoot 通过 Exploded Archives 的方式部署
之前部署 SpringBoot 一直是用可执行 jar 的方式. java -jar codergroup-1.0.0.jar 就可以启动项目,为了能在后台运行,通常我们会使用这行命令 nohup j ...
- eclipse创建Maven web项目的步骤
Maven 是一个项目管理工具,可以对 Java 项目进行构建.依赖管理. Maven 能够帮助开发者完成以下工作: 构建 文档生成 报告 依赖 SCMs 发布 分发 邮件列表 一.环境配置 Mave ...
- MySQL 优化实战记录
阅读本文大概需要 2 分钟. 背景 本次SQL优化是针对javaweb中的表格查询做的. 部分网络架构图 业务简单说明 N个机台将业务数据发送至服务器,服务器程序将数据入库至MySQL数据库.服务器中 ...
- 埃航和737MAX坠毁:软件优先级问题
事件背景: 2019年3月10日,埃塞俄比亚航空公司一架波音737MAX8飞机发生坠机,机上157人全部遇难,包括8名中国公民.这是继去年10月29日印尼狮航空难事故之后,波音737MAX8飞机在五个 ...
- 图片格式PGM缩写
PGM是Portable Gray Map的缩写.它是灰度图像格式中一种最简单的格式标准.另外两种与之相近的图片格式是PBM和PPM.它们分别相应着黑白图像和彩色图像. PGM的数据存放方式相比于JP ...
- [原创]K8Cscan插件之Cisco思科设备扫描(IP、设备型号、主机名、Boot、硬件版本)
[原创]K8 Cscan 大型内网渗透自定义扫描器 https://www.cnblogs.com/k8gege/p/10519321.html Cscan简介:何为自定义扫描器?其实也是插件化,但C ...