1:获取数据库连接

Connection connection=getConnection();

2:准备SQL语句

3:调用Connection的creatStatement()方法获取Statement对象运行SQL语句

(注:Statement对象处理的SQL语句仅仅能是INSERT,UPDATE或DELETE)
statement=connection.createStatement();

statement.execute(SQL);

4:关闭Statement对象

5:关闭数据库连接


将数据库连接等操作进行封装:

附:JDBCTools.java
/**
* JDBC 的工具类
*
* 当中包括: 获取数据库连接, 关闭数据库资源等方法.
*/
public class JDBCTools { //处理数据库事务的
//提交事务
public static void commit(Connection connection){
if(connection != null){
try {
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
} //回滚事务
public static void rollback(Connection connection){
if(connection != null){
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
} //開始事务
public static void beginTx(Connection connection){
if(connection != null){
try {
connection.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
} private static DataSource dataSource = null; //数据库连接池应仅仅被初始化一次.
static{
dataSource = new ComboPooledDataSource("helloc3p0");
} public static Connection getConnection() throws Exception {
return dataSource.getConnection();
} public static void releaseDB(ResultSet resultSet, Statement statement,
Connection connection) { if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if (connection != null) {
try {
//数据库连接池的 Connection 对象进行 close 时
//并非真的进行关闭, 而是把该数据库连接会归还到数据库连接池中.
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }

附:MySqlTools.java,对数据库进行操作
	public static void update(String SQL){
Connection connection = null;
Statement statement=null;
try {
connection = JDBCTools.getConnection();
statement=connection.createStatement();
statement.execute(SQL); } catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.releaseDB(null, null, connection);
}
}


最新文章

  1. 盘点国内11家已经获得融资的移动CRM平台
  2. Lucene.net站内搜索—3、最简单搜索引擎代码
  3. Bootstrap左侧下拉三级菜单
  4. 在uwp中复活常用的vb库函数
  5. PRML读书会第三章 Linear Models for Regression(线性基函数模型、正则化方法、贝叶斯线性回归等)
  6. ios应运程序的五种状态
  7. IOC主要接口
  8. H TML5 之 (7) 俄罗斯方块效果
  9. Android5.1源码Xposed框架编译
  10. python之路: 线程、进程和协程
  11. javascript中DOM集锦(二)
  12. Boredom
  13. javascript数组的内置对象Array
  14. 接之前的文章,VS2017中使用Spring.NET配置以及使用方法(framework4.6.1超详细)
  15. es的mapping设置
  16. ajax请求二进制流图片并渲染到html中img标签
  17. (二)windows上使用docker
  18. hadoop上线和下线节点
  19. Qt Q_DECLARE_METATYPE说明——让自定义的类型设置到QVariant
  20. 配置nginx虚拟目录配置文件支持tp的pathinfo

热门文章

  1. SpringBoot中打包设置,将配置文件打包在外部
  2. java源码之LinkedHashMap
  3. HDU 4828
  4. Springmvc Get请求Tomcat、WebLogic中文乱码问题
  5. 纯粹的K12精髓 - 名师指导整理《20以内加法口诀表》
  6. PHP的curl库代码使用
  7. hdu 2604 Queuing (矩阵高速幂)
  8. xBIM 基础08 WeXplorer 简介
  9. vue 组件来回切换时 记住上一个组件滚动位置(keep-alive)
  10. autocomplete="off" 不起作用解决方案