1、数据库配置文件jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=

2、数据库数据类JdbcUtils

package com.test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties; public class JdbcUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
// 加载驱动,获取数据库连接信息
static {
try {
// 加载配置文件
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("DB.properties");
Properties properties = new Properties();
properties.load(in);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
// 加载驱动
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} /**
* 获取数据库连接
* @throws SQLException
*/
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
} /**
* 释放资源
* @param connection
* @param preparedStatement
* @param resultSet
*/
public static void releaseDB(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

3、增删改查

package com.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class JdbcTest {
public void insert() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
// 获取连接
connection = JdbcUtils.getConnection();
// 准备sql语句
String sql = "INSERT INTO user(id,name,age) VALUES(?,?,?)";
// 获取PrepareStatement对象
preparedStatement = connection.prepareStatement(sql);
// 填充占位符
preparedStatement.setInt(1, 6);
preparedStatement.setString(2, "小A");
preparedStatement.setInt(3, 25);
// 执行sql
int num = preparedStatement.executeUpdate();// 返回影响到的行数 System.out.println("一共影响到" + num + "行");
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseDB(connection, preparedStatement, null);
}
}
public void update() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
// 获取连接
connection = JdbcUtils.getConnection();
// 准备sql语句
String sql = "update user set age = ? where name = ?";
// 获取PrepareStatement对象
preparedStatement = connection.prepareStatement(sql);
// 填充占位符
preparedStatement.setInt(1, 30);
preparedStatement.setString(2, "小A");
// 执行sql
int num = preparedStatement.executeUpdate();// 返回影响到的行数 System.out.println("一共影响到" + num + "行");
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseDB(connection, preparedStatement, null);
}
}
public void query() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConnection();
String sql = "SELECT name,age FROM user";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery(); // 遍历结果集
while (resultSet.next()) {
String name = resultSet.getString(1);
Integer age = resultSet.getInt(2); System.out.println("name:" + name + ", age:" + age);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseDB(connection, preparedStatement, resultSet);
}
}
public void delete() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtils.getConnection();
String sql = "DELETE FROM user WHERE id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 6);
int num = preparedStatement.executeUpdate(); System.out.println("一共影响到" + num + "行");
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseDB(connection, preparedStatement, null);
}
} }

  

JAVA JDBC 增删改查简单例子的更多相关文章

  1. JDBC增删改查简单测试

    首先编写一个entity以便与数据库表文件相对应 lyTable.java public class LyTable implements java.io.Serializable { private ...

  2. JDBC增删改查,PreparedStatement和Statement的区别

    此篇是在上一篇的基础上使用PreparedStatement对象来实现JDBC增删改查的 具体工具类JDBCTools和实现类和配置文件在上一篇Statement对象实现的时候有写. 上一篇地址htt ...

  3. WPF MVVM+EF增删改查 简单示例(二) 1对1 映射

    WPF MVVM+EF增删改查 简单示例(一)实现了对学生信息的管理. 现在需求发生变更,在录入学生资料的时候同时需要录入学生的图片信息,并且一名学生只能有一张图片资料.并可对学生的图片资料进行更新. ...

  4. JDBC增删改查和查唯一的完整代码

    第一部分代码(实体类) package com.wf.entity; public class Hehe{ private int hehe_id; private String hehe_name; ...

  5. JDBC增删改查

    /* db.properties的配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/day14 username=root ...

  6. jdbc 增删改查以及遇见的 数据库报错Can't get hostname for your address如何解决

    最近开始复习以前学过的JDBC今天肝了一晚上 来睡睡回笼觉,长话短说 我们现在开始. 我们先写一个获取数据库连接的jdbc封装类 以后可以用 如果不是maven环境的话在src文件下新建一个db.pr ...

  7. jdbc增删改查进行封装

    jdbc封装 1 dao (代码分层) com.aaa.dao 存放dao相关的类型 例如 StudentDAOImpl 处理 数据库的链接 存取数据 com.aaa.servlet 存放servle ...

  8. 一个在ASP.NET中利用服务器控件GridView实现数据增删改查的例子

    备注:这是我辅导的一个项目开发组的一个例子,用文章的方式分享出来,给更多的朋友参考.其实我们这几年的项目中,都不怎么使用服务器控件的形式了,而是更多的采用MVC这种开发模式.但是,如果项目的历史背景是 ...

  9. iOS sqlite 增删改查 简单封装(基于 FMDB)

    /** *  对 sqlite 的使用进行简单封装,仅涉及简单的单表 增删改查 * *  基于 FMDB * *  操作基于 model ,数据库表字段与 model 属性一一对应,对 model 整 ...

随机推荐

  1. C# 实现生产者消费者队列

    开发过程中经常会碰到这样的场景:需要从一个地方获取一些数据,然后处理数据并将其保存在数据库中. 1 2 3 4 5 6 7 8 9 10 private void FetchData() {} pri ...

  2. Python - 1. Built-in Atomic Data Types

    From:http://interactivepython.org/courselib/static/pythonds/Introduction/GettingStartedwithData.html ...

  3. Rower Bo (高数 + 物理)

    #include<bits/stdc++.h> #define esp (1e-5) using namespace std; int main(){ int a; double v1, ...

  4. eclipse maven install没反应解决办法

    .打开eclipse的Window菜单-->java-->Installed JREs .点击用的jdk,edit,在Default VM arguments里面填入-Dmaven.mul ...

  5. 如何用新安装的jdk替换掉Linux系统默认jdk

    版主写的非常棒: http://blog.csdn.net/u011364306/article/details/48375653

  6. 20165305 Linux安装及学习

    一.虚拟机的安装 在根据老师所给的<基于VirtualBox虚拟机安装Ubuntu图文教程>的时候,我发现虚拟化处于被禁用状态,于是我在网上查找了一下解决办法,在我将bios中虚拟化设置为 ...

  7. Linux基础命令---查找进程id

    pidof pidof可以查找指定名称的进程的pid,将结果送到标准输出.pidof有两种返回值:0,找到至少一个进程:1,没有找到进程.pidof实际上与killall5相同:程序根据调用它的名称进 ...

  8. 转:【专题八】P2P编程

    引言: 前面的介绍专题中有朋友向我留言说介绍下关于P2P相关的内容的,首先本人对于C#网络编程也不是什么大牛,因为能力的关系,也只能把自己的一些学习过程和自己的一些学习过程中的理解和大家分享下的,下面 ...

  9. 四轴飞行器1.7 NRF24L01P无线通讯和改进型环形缓冲(转)

    源: 四轴飞行器1.7 NRF24L01P无线通讯和改进型环形缓冲

  10. Git命令cherry-pick,选择把一部分代码提交到另一个分支

    由于对git的使用还不是很熟悉,只是会基本的添加.提交.分支相关简单操作,在开发新需求的时候,需要涉及两个项目,一部分在新项目的新分支上开发的,另一部分是在老项目的老分支上开发的(这里忘了创建新分支) ...