/**
* 写一个通用的更新方法 包括 INSERT、 DELETE、UPDATE
* 使用工具类
* @param sql
*/
  public void update(String sql){
Connection conn=null;
Statement statement=null;
try {
conn=JDBCTools.getConnection();
statement=conn.createStatement();
statement.executeUpdate(sql); } catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools.close(statement, conn);
} }

//jdbc工具类

package jdbc;

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; public class JDBCTools { /**
* 操作JDBC的工具类,其中封装了一些工具方法 版本一;
*
* @author 杨波波
*/
/**
* 关闭Statement 和Connection
*/
public static void close(ResultSet rs, Statement statement, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} } } public static void close(Statement statement, Connection conn) { if (statement != null) {
try {
statement.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} } } /**
* 1 获取连接的方法 通过读取配置文件从数据库服务器获取一个连接
*
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception {
// 1 准备连接数据库的4个字符串
String driverClass = null;
String urljdbc = null;
String user = null;
String password = null;
// 2 读取路径下的配置文件
InputStream is = JDBCTools.class.getClassLoader().getResourceAsStream(
"jdbc.properties");
Properties p = new Properties();
p.load(is);// 加载
driverClass = p.getProperty("driver");
urljdbc = p.getProperty("urljdbc");
user = p.getProperty("user");
password = p.getProperty("password"); // // 3 通过反射--->Driver
// Driver driver = (Driver) Class.forName(driverClass).newInstance();//
// 反射!!
// Properties info=new Properties();
// info.put("user",user);
// info.put("password", password);
// // 通过Driver的connect方法获取数据库的连接
// Connection cc=driver.connect(urljdbc, info);
// return cc; // 3 加载数据库的驱动程序(对应的Driver 实现类中有注册驱动的静态代码块)
Class.forName(driverClass);// 是的方法灵活
// 4 通过DriverManager的getConnection()方法获取数据库的连接。 Connection cc = DriverManager.getConnection(urljdbc, user, password);
return cc; } }

转:  https://blog.csdn.net/YL1214012127/article/details/48214093

Java_jdbc 基础笔记之四 数据库连接 (通用更新方法)的更多相关文章

  1. Java_jdbc 基础笔记之七 数据库连接(方法升级)

    之前的更新方法 public static void update(String sql) { Connection conn = null; Statement statement = null; ...

  2. Java_jdbc 基础笔记之一 数据库连接

    方式一: 1.创建一个Driver实现类的对象 2.准备连接数据库的基本信息:url,user,password 3.调用Driver接口的connect(url,info)获取数据库连接 * Dri ...

  3. Java_jdbc 基础笔记之八 数据库连接(写一个查询Student对象的方法)

    public Student getStudent(String sql, Object... args) { // 查询Student对象 Student stu = null; Connectio ...

  4. Java_jdbc 基础笔记之六 数据库连接 (PreparedStatement)

    reparedStatement 是 Statement 的子接口 * ①需要预编译 SQL 语句:PreparedStatement ps = conn.preparedStatement(sql) ...

  5. Java_jdbc 基础笔记之五 数据库连接 (ResultSet)

    /** * ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果. * 1. 调用 Statement 对象的 executeQuery(sql)可以得到结果集. * 2. Resul ...

  6. Java_jdbc 基础笔记之三 数据库连接 (Statement)

    /** * 通过JDBC向之指定的数据表中插入一条记录 1 Statement :用于执行SQL语句的对象 * ==>通过Connection的createStatement()方法来获取 == ...

  7. MVC LINQ中用封装的TSQL通用更新方法

    把TSQL拿出来,做了一个封装,适用的所有表,更新有两种,普通更新和记数更新 看代码:这两个方法是写在DAL里的数据操作基类里的,只有它的子类可以用它,所以用protected做为限制 /// < ...

  8. Java_jdbc 基础笔记之十一数据库连接 (通用的查询方法)

    鉴于之前的查询方法,在这里我们可以写一个通用的方法 /** * 鉴于 student.和customer查询的方法有好多相同之处,在此可以写一个通用的方法 */ public <T> T ...

  9. Java_jdbc 基础笔记之九 数据库连接 (查询Customer对象的方法)

    /** * * 写一个查询Customer对象的方法 * */ public Customer getCustomer(String sql, Object... args) { Customer c ...

随机推荐

  1. 【大数据技术能力提升_4】logistic学习

    logistic学习 标签(空格分隔): logistic sigmod函数 逻辑回归 分类 前言:   整体逻辑回归比线性回归难理解点,其还需要<概率论与数理统计>中"二项分布 ...

  2. Python 依赖版本控制 (requirements.txt 文件生成和使用)

    requirements.txt 最好配合虚拟空间使用, 虚拟空间的使用请参考 Python 虚拟空间的使用 - 难以想象的晴朗. requirements.txt 可以保证项目依赖包版本的确定性, ...

  3. Linux命令cut

    原文 cut 文件内容查看 显示行中的指定部分,删除文件中指定字段 显示文件的内容,类似于下的type命令. 说明 该命令有两项功能,其一是用来显示文件的内容,它依次读取由参数file所指明的文件,将 ...

  4. error 106: Can't Access ASP.NET\ClientFiles\

    Error 1606 Can’t access ASP.NET\ClientFiles\ when installing Crystal Reports Support Pack 10     Sea ...

  5. [nginx][tls] nginx配置https与ssl/tls的sni的方法

    一 https的sni配置方法 http {       }       server {               listen 443 ssl;               server_nam ...

  6. Windows通过SSH远程登录Linux主机

    准备工作:1.Windows系统下装有VMware虚拟机且是Linux系统2.终端连接工具Xshell 63.本次实验系统IP如下 系统 IP Windows10 192.168.37.111 Cen ...

  7. Debug与Release版本的区别

    Debug 和 Release 并没有本质的区别,他们只是VC预定义提供的两组编译选项的集合,编译器只是按照预定的选项行动.如果我们愿意,我们完全可以把Debug和Release的行为完全颠倒过来.当 ...

  8. vue-cli3.0 脚手架搭建项目的过程详解

    1.安装vue-cli 3.0 ? 1 2 3 npm install -g @vue/cli # or yarn global add @vue/cli 安装成功后查看版本:vue -V(大写的V) ...

  9. Windows下通过SSH无密码连接Linux服务器

    一.配置环境 1.本机系统:Windows 10 Pro(64位) 2.服务器:CentOS 6.10(64位) 3.SSH连接软件:SecureCRT 二.配置SSH无密码登录步骤 1.在个人PC机 ...

  10. 让 Python 代码更易维护的七种武器——代码风格(pylint、Flake8、Isort、Autopep8、Yapf、Black)测试覆盖率(Coverage)CI(JK)

    让 Python 代码更易维护的七种武器 2018/09/29 · 基础知识 · 武器 原文出处: Jeff Triplett   译文出处:linux中国-Hank Chow    检查你的代码的质 ...