连接数据库的四个必要条件:driverclass、url、username、password。

package cn.itcast.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
//使用配置文件 public class JdbcUtils {
private static final String DRIVERCLASS;
private static final String URL;
private static final String USERNAME;
private static final String PASSWORD;
static{
DRIVERCLASS=ResourceBundle.getBundle("jdbc").getString("driverClass");
URL=ResourceBundle.getBundle("jdbc").getString("url");
USERNAME=ResourceBundle.getBundle("jdbc").getString("username");
PASSWORD=ResourceBundle.getBundle("jdbc").getString("password");
}
static{//静态块只执行一次驱动就加载了
try {
//将加载驱动操作,放置在静态代码块中,这样就保证了只加载一次。
Class.forName(DRIVERCLASS);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnectin() throws SQLException{//连接就抛SQLException最靠谱 //Class.forName("com.mysql.jdbc.Driver");//在开发中用哪个Statement人家有选择的权利,你不能给它抽取 //2.获取连接
//Connection con = DriverManager.getConnection("jdbc:mysql:///day17", "root", "");
Connection con = DriverManager.getConnection(URL,USERNAME, PASSWORD); return con;
} //如果再完善一点,可以写关闭操作
public static void closeConnection(Connection con) throws SQLException{
if(con!=null){
con.close();
}
}
public static void closeStatement(Statement st) throws SQLException{
if(st!=null){
st.close();
}
}
public static void closeResultSet(ResultSet rs) throws SQLException{
if(rs!=null){
rs.close();
}
} }
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:///day17
username=root
password= #driverClass=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:MFC
#username=scott
#password=scott
package cn.itcast.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import org.junit.Test; import cn.itcast.utils.JdbcUtils;
import cn.itcast.utils.JdbcUtils1; //jdbc的crud操作
public class JdbcDemo6 { @Test
public void findByIdTest(){
//1.定义sql
String sql = "select * from user where id= 1";
Connection con = null;
Statement st = null;
ResultSet rs = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取连接
try {
con = DriverManager.getConnection("jdbc:mysql:///day17", "root", ""); //3.获取操作sql语句对象Statement
st = con.createStatement(); //4.执行sql
rs = st.executeQuery(sql); //5.遍历结果集
while(rs.next()){
int id = rs.getInt("id");
//String id = rs.getString("id");//虽然用getString()行,但是用getInt()比较合适
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
System.out.println(id+" "+username+" "+password+" "+email);
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//6.释放资源
try {
if(rs !=null ){
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(st!=null){
st.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//添加操作
@Test
public void addTest(){
//定义sql
String sql = "insert into user values(null,'张三','123','zs@163.com')";
Connection con = null;
Statement st = null;
ResultSet rs = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取连接
try {
con = DriverManager.getConnection("jdbc:mysql:///day17", "root", ""); //3.获取操作sql语句对象Statement
st = con.createStatement(); //4.执行sql
int row = st.executeUpdate(sql);
System.out.println(row);
if(row!=0){
System.out.println("添加成功");
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//6.释放资源
try {
if(rs !=null ){
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(st!=null){
st.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} //update操作
@Test
public void updateTest(){
//将id=3的人的password修改为456
String password = "456";
String sql = "update user set password='"+password+"' where id=3"; //1.得到Connection
Connection con = null;
Statement st = null;
try {
con = JdbcUtils1.getConnectin(); //3.获取操作sql语句对象Statement
st = con.createStatement(); //4.执行sql
int row = st.executeUpdate(sql);
System.out.println(row);
if(row!=0){
System.out.println("添加成功");
} }catch(ClassNotFoundException e){
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭资源
try {
if(st!=null){
st.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } //delete测试
@Test
public void deleteTest(){
//将id=3的人删除
//String sql = "delete from user where id=3";
//将id=2的人删除
String sql = "delete from user where id=2";
//1.得到Connection
Connection con = null;
Statement st = null;
try {
con = JdbcUtils.getConnectin(); //3.获取操作sql语句对象Statement
st = con.createStatement(); //4.执行sql
int row = st.executeUpdate(sql);
System.out.println(row);
if(row!=0){
System.out.println("删除成功");
} }/*catch(ClassNotFoundException e){
e.printStackTrace();
}*/
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭资源
/* try {
if(st!=null){
st.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
/* try {
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
try {
JdbcUtils.closeStatement(st);
JdbcUtils.closeConnection(con);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }

day17 11.JdbcUtils工具抽取的更多相关文章

  1. day17(JDBC入门&jdbcUtils工具介绍)

    day17 JDBC整体思维导图 JDBC入门 导jar包:驱动! 加载驱动类:Class.forName("类名"); 给出url.username.password,其中url ...

  2. Android快速开发不可或缺的11个工具类

     Android快速开发不可或缺的11个工具类  :http://www.devst ore.cn/code/info/363.html

  3. JavaWeb基础之JdbcUtils工具类final

    JdbcUtils工具类3.0最终版,添加了事务相关功能和释放链接.最终版本可以直接打成jar包,在后面的基本项目都会使用该工具类 1. JdbcUtils代码 /** * 最终版 * @author ...

  4. JavaWeb基础之JdbcUtils工具类2.0

    使用c3p0连接池来改版JdbcUtils工具 1. 使用c3p0连接池获取连接,使代码更加简单 /** * 使用c3p0连接池做小工具 * JdbcUtils v2.0 * @author hui. ...

  5. JavaWeb基础之JdbcUtils工具类1.0

    2016年12月20日,第一次学习JDBC.看的是传智播客崔希凡老师的视频,东北口音很是风趣幽默,技术之牛让人膜拜.2017年9月21日,再次重温web知识,分享JdbcUtils工具类,用以接下来的 ...

  6. Druid 连接池 JDBCUtils 工具类的使用

    Druid工具介绍 它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser. 支持所有JDBC兼容的数据库,包括Oracle.MySQL. ...

  7. 创建JDBCUtils工具类

    JDBCUtils工具类 私有化构造函数,外界无法直接创建对象 提供公共的,静态的,getConnection 方法,用来给外界提供数据库连接 提供公共的,静态的,close方法,用来释放资源 pac ...

  8. 记一次关于JDBCUtils工具类的编写

    jdbc.properties数据库配置的属性文件内容如下 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost/xxxx ...

  9. 【JDBC】学习路径5-提取JDBCUtils工具类

    回顾我们上面几节的内容,我们发现重复代码非常多,比如注册驱动.连接.关闭close()等代码,非常繁杂. 于是我们将这些重复的大段代码进行包装.提取成JDBCUtils工具类. 第一章:提取注册连接模 ...

随机推荐

  1. Memcache笔记(1)

    缓存主要分为:页面缓存和数据缓存 Memcache .redis.mongodb都是做数据缓存的 Memcache是什么? 是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的ha ...

  2. 解析Ceph: RBDCache 背后的世界

    转自:https://www.ustack.com/blog/ceph-internal-rbdcache/ RBDCache 是Ceph的块存储接口实现库 Librbd 的用来在客户端侧缓存数据的目 ...

  3. JavaScript如何处理解析JSON数据详解

    JSON (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧. JSON 是 JavaScript 原生格式,这意味着在 JavaScript 中处理 JSON ...

  4. 【SQL查询】查询的值为空时,给出默认值_NVL函数

    格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值. 引申一下,此NVL的作 ...

  5. VI编辑器、ipython、jupyter及进程

    VI编辑器.ipython.jupyter及进程知识总结 https://www.cnblogs.com/thoughtful-actors/p/9650959.html VI编辑器.ipython. ...

  6. AtCoder Petrozavodsk Contest 001 B - Two Arrays

    Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You are given two inte ...

  7. [Luogu3727]曼哈顿计划E

    luogu 题意(简化版) 给你一棵树,每个点上有一个\(SG\)值,问你是否存在一条路径使得\(SG\)异或和为\(0\). sol 可以当做每个点的稳定值就是这个点上的石子数量. 很显然我们只需要 ...

  8. mysql 多表查询 左联 去重方法

    1.数据库中的两张表: 2.传统左联查询数据结果如下: 3.替换查询语句可得到去重数据结果:

  9. 仿百度下拉关键词,和关键词的小demo

    自己做项目时用到的仿百度下拉关键词 代码: $(function(){ var oTxt = document.getElementById('search_text'); oTxt.onkeyup ...

  10. 首次db查询时延迟明显高于后面几次。

    1.如果排查到时db相关的问题的话,一般都是连接池的配置问题. 在配置好连接池时一定要注意对连接也进行初始化配置,否则可能出现连接池初始化了,但是连接并没有初始化,这样在第一次查询的时候可能会出现较大 ...