Java_jdbc 基础笔记之四 数据库连接 (通用更新方法)
/**
* 写一个通用的更新方法 包括 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 基础笔记之四 数据库连接 (通用更新方法)的更多相关文章
- Java_jdbc 基础笔记之七 数据库连接(方法升级)
之前的更新方法 public static void update(String sql) { Connection conn = null; Statement statement = null; ...
- Java_jdbc 基础笔记之一 数据库连接
方式一: 1.创建一个Driver实现类的对象 2.准备连接数据库的基本信息:url,user,password 3.调用Driver接口的connect(url,info)获取数据库连接 * Dri ...
- Java_jdbc 基础笔记之八 数据库连接(写一个查询Student对象的方法)
public Student getStudent(String sql, Object... args) { // 查询Student对象 Student stu = null; Connectio ...
- Java_jdbc 基础笔记之六 数据库连接 (PreparedStatement)
reparedStatement 是 Statement 的子接口 * ①需要预编译 SQL 语句:PreparedStatement ps = conn.preparedStatement(sql) ...
- Java_jdbc 基础笔记之五 数据库连接 (ResultSet)
/** * ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果. * 1. 调用 Statement 对象的 executeQuery(sql)可以得到结果集. * 2. Resul ...
- Java_jdbc 基础笔记之三 数据库连接 (Statement)
/** * 通过JDBC向之指定的数据表中插入一条记录 1 Statement :用于执行SQL语句的对象 * ==>通过Connection的createStatement()方法来获取 == ...
- MVC LINQ中用封装的TSQL通用更新方法
把TSQL拿出来,做了一个封装,适用的所有表,更新有两种,普通更新和记数更新 看代码:这两个方法是写在DAL里的数据操作基类里的,只有它的子类可以用它,所以用protected做为限制 /// < ...
- Java_jdbc 基础笔记之十一数据库连接 (通用的查询方法)
鉴于之前的查询方法,在这里我们可以写一个通用的方法 /** * 鉴于 student.和customer查询的方法有好多相同之处,在此可以写一个通用的方法 */ public <T> T ...
- Java_jdbc 基础笔记之九 数据库连接 (查询Customer对象的方法)
/** * * 写一个查询Customer对象的方法 * */ public Customer getCustomer(String sql, Object... args) { Customer c ...
随机推荐
- @PropertySources和@ImportReSources注解
修改默认加载的配置文件,加载指定的配置文件. @PropertySources 格式:@PropertySources(value={"classpath:xxx.xxx"}) @ ...
- unity 编译之后签名被改变
原因:在gradle 模式下编译,把development build 勾上 会使签名改变
- SQL SERVER-Extendevent系统视图
--获得扩展事件的事件 select name,description from sys.dm_xe_objects where object_type='event' order by name - ...
- c# BufferedStream 类
- XSLT知识点【一】
XSL 指扩展样式表语言(EXtensible Stylesheet Language). 它起始于 XSL,结束于 XSLT.XPath 以及 XSL-FO. 起始于 XSL------CSS = ...
- mysqlslap压测
mysqlslap 是MySQL自带的压测工具: -P --create-schema=test -S /tmp/mysql_sandbox18601.sock --number-of-queries ...
- SpringCloud2.0 Turbine 断路器集群监控 基础教程(九)
1.启动基础工程 1.1.启动[服务中心]集群,工程名称:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) ...
- 基于ATtiny85微控制器制作一款四通道温度计
本文主要介绍了一款基于ATtiny85微控制器的四通道温度计,该温度计可以同时监测四个温度传感器的温度,并且实时在小型128x32 OLED液晶屏上进行显示. 该温度计可以用于任何需要监控多个温度点的 ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- 28.XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
转自https://www.cnblogs.com/gdjlc/archive/2013/09/08/3308229.html XML Schema 语言也称作 XML Schema 定义(XML S ...