1. 将数据库、驱动信息存储在配置文件

configure.properties

 url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=GMT&useSSL=false
user=用户名,一般是root
password=数据库密码
driver=com.mysql.cj.jdbc.Driver

2. JDBC工具类

MyDBUtil.java

 import java.io.InputStream;
import java.sql.*;
import java.util.Properties; /**
* 手写实现JDBC工具类
* @author Nemo
* @version 1.0
* @date 2019/4/10
*/
public class MyDBUtil {
private static String url;
private static Properties proterties; // 注册驱动
static {
proterties = new Properties; InputStream configureStream = MyDBUtil.class.getClassLoader.getResourceAsStream("configure.proterties");
try {
properties.load(configureStream);
String driver = properties.getProperty("driver");
class.forName(driver); url = properties.getProperty("url");
} catch (Exception e) {
e.printStackTrace();
}
} // 获取连接
public static Connection getConnection() throw SQLException{
return DriverManager.getConnection(url, properties);
} // 释放资源
public static void release(Statement statement, Connection connection, ResultSet resultSet) {
closeQuietly(statement);
closeQuietly(connection);
closeQuietly(resultSet);
}
// 重载释放资源
public static void release(Statement statement, Connection connection) {
closeQuietly(statement);
closeQuietly(connection);
} private static void closeQuietly(AutoCloseable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

3. 以实现数据库事务为例,演示工具类的使用

TranctionTest.java

 import org.junit.Test;

 import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement; /**
* 以数据库事务为例进行演示
* @author Nemo
* @version 1.0
* @date 2019/4/13
*/
public class TranctionTest {
@Test
public void traction() throws SQLException {
Connection conn = null;
Statement stm = null;
Savepoint savepoint = null;
try {
// 获取数据库连接
conn = MyDBUtil.getConnection(); // 关闭数据库自动commit功能
conn.setAutoCommit(false); // 建立sql语句
stm = conn.cteateStatement(); // zs给lisi转账100
String sql1 = "update user set money=money-100 where name='zs';";
String sql2 = "update user set money=money+100 where name='lisi';"; // wangwu给lisi转账100
String sql3 = "update user set money=money-100 where name='wangwu';";
String sql4 = "update user set money=money+100 where name='lisi';"; // 提交sql语句
stm.executeUpdate(sql1);
stm.executeUpdate(sql2); // 设置savepoint
savepoint = conn.setSavepoint(); int i = 1/0; stm.executeUpdate(sql3);
stm.executeUpdate(sql4); conn.commit();
} catch (Exception e) {
conn.rollback(savepoint);
conn.commit();
} finally {
MyDBUtil.release(statement, connection);
}
}
}

手写JDBC - 数据库、驱动信息存储在配置文件的更多相关文章

  1. MNIST手写数字数据库

    手写数字库很容易建立,但是总会很浪费时间.Google实验室的Corinna Cortes和纽约大学柯朗研究所的Yann LeCun建有一个手写数字数据库,训练库有60,000张手写数字图像,测试库有 ...

  2. SQL纯手写创建数据库到表内内容

    建表啥的只点点鼠标,太外行了,不如来看看我的纯手写,让表从无到有一系列:还有存储过程临时表,不间断的重排序: 一:建数据库 create Database Show on primary ( name ...

  3. 常用JDBC数据库驱动包和类名

    MySQL数据库: 1)驱动包:https://mvnrepository.com/artifact/mysql/mysql-connector-java(下载路径) 2)驱动类名:com.mysql ...

  4. 【机器学习】BP神经网络实现手写数字识别

    最近用python写了一个实现手写数字识别的BP神经网络,BP的推导到处都是,但是一动手才知道,会理论推导跟实现它是两回事.关于BP神经网络的实现网上有一些代码,可惜或多或少都有各种问题,在下手写了一 ...

  5. 利用神经网络算法的C#手写数字识别

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 下载Demo - 2.77 MB (原始地址):handwritten_character_recognition.zip 下载源码 - 70. ...

  6. 深度学习之PyTorch实战(3)——实战手写数字识别

    上一节,我们已经学会了基于PyTorch深度学习框架高效,快捷的搭建一个神经网络,并对模型进行训练和对参数进行优化的方法,接下来让我们牛刀小试,基于PyTorch框架使用神经网络来解决一个关于手写数字 ...

  7. 用python实现的的手写数字识别器

    概述 带GUI界面的,基于python sklearn knn算法的手写数字识别器,可用于识别手写数字,训练数据集为mnist. 详细 代码下载:http://www.demodashi.com/de ...

  8. BP神经网络(手写数字识别)

    1实验环境 实验环境:CPU i7-3770@3.40GHz,内存8G,windows10 64位操作系统 实现语言:python 实验数据:Mnist数据集 程序使用的数据库是mnist手写数字数据 ...

  9. Caffe系列4——基于Caffe的MNIST数据集训练与测试(手把手教你使用Lenet识别手写字体)

    基于Caffe的MNIST数据集训练与测试 原创:转载请注明https://www.cnblogs.com/xiaoboge/p/10688926.html  摘要 在前面的博文中,我详细介绍了Caf ...

随机推荐

  1. git 进阶操作

    1.blame git blame +文件名,可以查看到某个文件每一行最近一次是由谁编辑修改的.-L 22,33 选项可以制定 2.bisect 开始git bisect:   $ git bisec ...

  2. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 12: ordinal not in range(128)问题解决

    今天在验证字符串是否包含的时候报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 12: ordinal n ...

  3. CALayer, CoreGraphics与CABasicAnimation介绍

    今天我们来看一下CALayer.CoreGraphics和CABasicAnimation.这些东西在处理界面绘制.动画效果上非常有用. 本篇博文就讲介绍CALayer的基本概念,使用CoreGrap ...

  4. Short jhat tutorial: diagnosing OutOfMemoryError by example

    转自: http://petermodzelewski.blogspot.com/2013/06/short-jhat-tutorial-diagnosing.html jhat这个工具经过使用, 发 ...

  5. 【小梅哥FPGA进阶教程】第十二章 数字密码锁设计

    十二.数字密码锁设计 本文由山东大学研友袁卓贡献,特此感谢 实验目的 实现数字密码锁设计,要求矩阵按键输出且数码管显示输入密码,密码输入正确与否均会有相应标志信号产生. 实验平台 芯航线FPGA核心板 ...

  6. hibernate缓存机制(转载)

    缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据,在特定的时刻或事 ...

  7. CodeForces 343D water tree(树链剖分)

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...

  8. INDEX--索引相关信息查看

    --============================================== --查看可能缺失的索引 SELECT mig.* ,migs.* ,mid.* FROM sys.dm ...

  9. C#在线运行

    初步完成c#代码的在线编辑.       首先,传回前端的c#在线代码,进行预编译,用CSharpCodeProvider这个方法.设置编译版本3.5 设置编译参数GenerateInMemory:是 ...

  10. 「AH2017/HNOI2017」礼物

    题目链接 戳我 \(Solution\) 应为我们可以将任意一个数列加上一个非负整数,即可以变为将一个数列加上一个整数(可以为负),我们将这个整数设为\(z\).所以要求的式子的变为: \[\sum_ ...