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 ...
随机推荐
- python基础自学 第四天
break和continue break:某一条件满足,退出循环,不在执行后续重复代码 continue:某一条件满足时,不执行后续重复的代码 注意:在循环中,如果使用continue这个关键字,使用 ...
- Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2)
A: 思路:就是找b,c之前有多个s[i] 代码: #include<stdio.h>#define ll long longusing namespace std;ll a,b,c;in ...
- entity.Database.SqlQuery() 和entity.Database.SqlCommand()
原文地址: http://msdn.microsoft.com/en-us/library/gg715124(v=vs.103) 使用 EF 4.1 或者更新版本, 你可以直接执行任何数据库命令. 在 ...
- Android开发 - 掌握ConstraintLayout(十一)复杂动画!如此简单!
介绍 本系列我们已经介绍了ConstraintLayout的基本用法.学习到这里,相信你已经熟悉ConstraintLayout的基本使用了,如果你对它的用法还不了解,建议您先阅读我之前的文章. 使用 ...
- JavaScript中的日期时间函数
1.Date对象具有多种构造函数,下面简单列举如下 new Date() new Date(milliseconds) new Date(datestring) new Date(year, mont ...
- 读《Linux Shell脚本攻略》(第2版) 一遍、二遍体会
前段时间读完了<Linux Shell脚本攻略>(第2版)这本书,给部分想读这本书的人分享下个人感受. 第一遍体会解读:就像黑夜中的灯塔,指明前进的道路. 推荐指数:强烈推荐. 书中讲解的 ...
- php--include 、require
一.include .require 定义:包含并运行指定文件 问题:查询了这两个语言结构的资料,有人说,什么require 先执行,什么include后执行. 思考:我觉得官方文档已经解释的很清楚 ...
- 页面css样式找不到问题
出现了一个页面没有样式的问题: 问题: 1.路径不对, 可以打开页面f12看样式是否找到 检查路径是否正确. 2.样式没引全或者没引对. 查看引入的样式是否正确或缺少样式. 3.路径明明写对了却404 ...
- Jquery Ajax Realize whether the user is registered
XMLHttpRequest对象可以在不向服务器提交整个页面的情况下,实现局部更新网页.当页面全部加载完毕后,客户端通过该对象向服务器请求数据,服务器端接受数据并处理后,向客户端反馈数据. XMLHt ...
- Java 中的 String 真的是不可变吗?
我们都知道 Java 中的 String 类的设计是不可变的,来看下 String 类的源码. public final class String implements java.io.Seriali ...