/**
* 写一个通用的更新方法 包括 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. SpringBoot 中的使用事务

    转自:https://blog.csdn.net/linzhiqiang0316/article/details/52638039 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数 ...

  2. c# Group类

  3. 常用模块(collections模块,时间模块,random模块,os模块,sys模块,序列化模块,re模块,hashlib模块,configparser模块,logging模块)

    认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...

  4. MySQL事件自动kill运行时间超时的SQL

    delimiter $create event my_long_running_trx_monitoron schedule every 1 minutestarts '2015-09-15 11:0 ...

  5. Pandas进阶笔记 (0)为什么写这个系列

    使用Pandas数年之久了,从最早的0.17版本开始接触Pandas,到现在0.25版本,踩过不少坑,面对各种稀奇古怪的bug抓耳挠腮.每每想要解决bug,或者想要实现一个特定的数据操作需求,首先想到 ...

  6. Redis中如何保证数据库和缓存双写时的数据的一致性?

    简单的场景: 直接使用 1. 使用Cache Aside pattern 读取的时候,先读取缓存中是否有数据,缓存中没有数据,再去数据库中进行查询,查询出来以后,然后再存入到缓存中 更新的时候,先删除 ...

  7. machine learning (5)---learning rate

    degugging:make sure gradient descent is working correctly cost function(J(θ)) of Number of iteration ...

  8. 通过命令行运行java出现"错误: 找不到或无法加载主类 "解决办法

    首先在命令行运行不需要写package路径, 而在ide中一般是有路径的 so举例说明 例如程序名为HelloWorldTest.java,程序中含有package helloWorld语句,而该包位 ...

  9. 2019-2020-1 20199312 《Linux内核原理与分析》 第八周作业

    ELF(Executable and Linkable Format)可执行的和可链接的格式.(对应Windows为PE) 其包含了以下三类: 可重定位文件:保存着代码和适当的数据,用来和其它的目标文 ...

  10. C++类中构造函数调用构造函数问题

    环境:xp+vs2010问题:在初始化类参数的过程中,可能需要多个重载的构造函数,但是有很多变量初始化代码又是一样的.肯定需要写一次,等待其他构造函数来调用即可.经过调试发现,在classA(int ...