在Eclipse上实现简单的JDBC增删查改操作
在Javaweb的学习里,学到了如何完成简单的增删查改操作,在这里撰写一篇文章以便自己整理回忆。
- 首先要建立一些包和导入一些文件、建一些类。具体框架如图


- 编写Product类
public class Product {
private long id;
private String productName;
private long dir_id;
private double salePrice;
private String supplier;
private String brand;
private double cutoff;
private double costPrice;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public long getDir_id() {
return dir_id;
}
public void setDir_id(long dir_id) {
this.dir_id = dir_id;
}
public double getSalePrice() {
return salePrice;
}
public void setSalePrice(double salePrice) {
this.salePrice = salePrice;
}
public String getSupplier() {
return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getCutoff() {
return cutoff;
}
public void setCutoff(double cutoff) {
this.cutoff = cutoff;
}
public double getCostPrice() {
return costPrice;
}
public void setCostPrice(double costPrice) {
this.costPrice = costPrice;
}
@Override
public String toString() {
return "Product [id=" + id + ", productName=" + productName + ", dir_id=" + dir_id + ", salePrice=" + salePrice
+ ", supplier=" + supplier + ", brand=" + brand + ", cutoff=" + cutoff + ", costPrice=" + costPrice
+ "]";
}
public Product(long id, String productName, long dir_id, double salePrice, String supplier, String brand,
double cutoff, double costPrice) {
super();
this.id = id;
this.productName = productName;
this.dir_id = dir_id;
this.salePrice = salePrice;
this.supplier = supplier;
this.brand = brand;
this.cutoff = cutoff;
this.costPrice = costPrice;
}
public Product() {
super();
}
}
- 编写IProductDao类
import java.util.List;
import github.domain.Product;
public interface IProductDao {
/*
* 根据id删除产品
*/
public void deleteProductById(long id);
/*
* 更新数据的操作
*/
public void updateProduct(Product product);
/*
* 查询数据的操作,根据id
*/
public Product queryProductById(long id);
/*
* 查询所有的产品
*/
public List<Product> queryAllProduct();
/*
* 新增数据
*/
public void addProduct(Product product);
}
- 编写ProductDaoImpl类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import github.dao.IProductDao;
import github.domain.Product;
import github.util.JDBCUtil; public class ProductDaoImpl implements IProductDao { JDBCUtil jdbc = JDBCUtil.getInstance(); @Override
public void deleteProductById(long id) {
Connection connection = null;
PreparedStatement pst = null;
try {
connection = jdbc.getConnection();
pst = connection.prepareStatement("delete from product where id = ?");
pst.setLong(1, id);
pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
jdbc.close(null, pst, connection);
}
} @Override
public void updateProduct(Product product) {
Connection connection = null;
PreparedStatement pst = null;
try {
connection = jdbc.getConnection();
pst = connection.prepareStatement("update product set productName = ? where id = ?");
pst.setString(1, product.getProductName());
pst.setLong(2, product.getId());
pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
jdbc.close(null, pst, connection);
}
} @Override
public Product queryProductById(long id) {
Product p1 = new Product();
Connection connection = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
connection = jdbc.getConnection();
pst = connection.prepareStatement("select * from product where id = ?");
pst.setLong(1, id);
rs = pst.executeQuery();
while(rs.next()){
String productName = rs.getString("productName");
p1.setProductName(productName);
} } catch (SQLException e) {
e.printStackTrace();
}finally{
jdbc.close(rs, pst, connection);
}
return p1;
} @Override
public List<Product> queryAllProduct() {
List<Product> list = new ArrayList<Product>();
try {
Connection connection = jdbc.getConnection();
PreparedStatement pst = connection.prepareStatement("select * from product");
ResultSet rs = pst.executeQuery();
while(rs.next()){
long id = rs.getLong("id");
String productName = rs.getString("productName");
long dir_id = rs.getLong("dir_id");
double salePrice = rs.getDouble("salePrice");
String supplier = rs.getString("supplier");
String brand = rs.getString("brand");
double cutoff = rs.getDouble("cutoff");
double costPrice = rs.getDouble("costPrice");
Product p = new Product(id, productName, dir_id, salePrice, supplier, brand, cutoff, costPrice);
list.add(p);
} } catch (SQLException e) {
e.printStackTrace();
} return list;
} @Override
public void addProduct(Product product) {
String sql="insert into product (id,productName,dir_id,salePrice,supplier,brand,cutoff,costPrice) values(?,?,?,?,?,?,?,?)";
Connection connection = null;
PreparedStatement pst = null;
try {
connection = jdbc.getConnection(); pst = connection.prepareStatement(sql);
pst.setLong(1, product.getId());
pst.setString(2, product.getProductName());
pst.setLong(3, product.getDir_id());
pst.setDouble(4, product.getSalePrice());
pst.setString(5,product.getSupplier());
pst.setString(6, product.getBrand());
pst.setDouble(7, product.getCutoff());
pst.setDouble(8, product.getCostPrice()); pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
jdbc.close(null, pst, connection);
}
}
}
- 封装工具JDBCUtil类
/*
* 操作JDBC的工具类
*/ import java.io.IOException;
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 JDBCUtil { private static JDBCUtil instace = null;
private static Properties pro = null;
static{
try {
pro = new Properties();
//读取配置文件
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("git.properties"));
Class.forName(pro.getProperty("jdbc.driver"));
instace = new JDBCUtil();//创建对象
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /*
* 获取jdbcutil的对象
*/
public static JDBCUtil getInstance(){
return instace;
} //2.获取连接
public Connection getConnection() throws SQLException{
return DriverManager.getConnection(pro.getProperty("jdbc.url"),pro.getProperty("jdbc.username"),pro.getProperty("jdbc.password"));
} //3.关闭
public void close(ResultSet rs,Statement st,Connection connection){
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(st!=null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} }
- 编写git.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:///test
jdbc.username = root
jdbc.password = root
- 编写JDBCTest类
import java.util.List; import org.junit.Test; import github.dao.IProductDao;
import github.dao.impl.ProductDaoImpl;
import github.domain.Product; public class JDBCTest { /*
* 删除数据
*/
IProductDao productDao = new ProductDaoImpl();
@Test
public void test() {
productDao.deleteProductById(2);
} /*
* 更改数据
*/
@Test
public void testUpdate() {
Product p1 = new Product(); p1.setProductName("荧光闪烁");
p1.setId(14); productDao.updateProduct(p1);
} /*
* 单查询
*/
@Test
public void testQuery() {
Product p = productDao.queryProductById(22L);
System.out.println(p);
} /*
* 多查询
*/
@Test
public void testAllQuery() {
List<Product> queryAllProduct = productDao.queryAllProduct();
for(Product p : queryAllProduct){
System.out.println(p);
}
} /*
* 增加数据
*/
@Test
public void addProductTest() {
Product p1 = new Product(); p1.setId(5);
p1.setProductName("荧光闪烁");
p1.setDir_id(5);
p1.setSalePrice(186.32);
p1.setSupplier("可乐");
p1.setBrand("可乐");
p1.setCutoff(0.72);
p1.setCostPrice(143.52); productDao.addProduct(p1);
}
}
- 运行程序

在Eclipse上实现简单的JDBC增删查改操作的更多相关文章
- Java连接MySQL数据库及简单的增删查改操作
主要摘自 https://www.cnblogs.com/town123/p/8336244.html https://www.runoob.com/java/java-mysql-connect.h ...
- 利用dbutils工具实现数据的增删查改操作(dbutis入门)
一.前期准备 1.安装数据库(如:mysql5.5) 2.安装Eclipse(如:3.4) 3.下载数据库驱动包 4.下载dbutis工具包 5.在Eclipse创建名为 dbutils 的工程并在工 ...
- PHP与MYSQL结合操作——文章发布系统小项目(实现基本增删查改操作)
php和mysql在一起几十年了,也是一对老夫老妻了,最近正在对他们的爱情故事进行探讨,并做了一个很简单的小东西——文章发布系统,目的是为了实现mysql对文章的基本增删查改操作 前台展示系统有:文章 ...
- Mybatis基础配置及增删查改操作
一.简介 平时我们都用JDBC访问数据库,除了需要自己写SQL之外,还必须操作Connection, Statement, ResultSet 这些其实只是手段的辅助类. 不仅如此,访问不同的表,还会 ...
- JDBC增删查改(使用配置文件)
JDBCDemo2.java package com.zhangbz.jdbc; import java.sql.Connection; import java.sql.ResultSet; impo ...
- JDBC 增删查改
public class MemberDaoImpl implements MemberDao { private Connection conn = null; public MemberDaoIm ...
- myBatis 实现用户表增删查改操作<方法2 加入接口>(最终版)
这2种方法的区别:1.添加接口 2.运用接口来实现 其他的都一样 添加接口 //接口的名字和xml的名字一样,这样xml中的namespace就不用改 public interface UserMap ...
- myBatis 实现用户表增删查改操作<方法1 没有使用接口的>(最终版)
在UserMapper.xml中添加增删改查 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...
- java操作ElasticSearch(es)进行增删查改操作
有时间是要了解一下ES这个东西的~ ---------------------------------------------------------------------------------- ...
随机推荐
- iOS 缩小 ipa 大小
一.爱奇艺 爱奇艺移动应用优化之路:如何让崩溃率小于千分之二 iOS8 对于 App 的 text 段有 60MB 的限制: 超过 200MB 的 App 需要连接 WIFI 下载(之前是 150MB ...
- [vijos1725&bzoj2875]随机数生成器<矩阵乘法&快速幂&快速乘>
题目链接:https://vijos.org/p/1725 http://www.lydsy.com/JudgeOnline/problem.php?id=2875 这题是前几年的noi的题,时间比较 ...
- Java多线程工具类之循环栅栏计数器
Java多线程下循环计数器 本文主要内容:CyclicBarrier(下文中凯哥就用cycBar来代替)定义介绍:举例说明:代码演示:从源码来看原理及总结:CyclicBarrier与CountDow ...
- 华为五年自动化测试工程详细解说:unittest单元测试框架
一.单元测试框架说明 单元测试是指在编程中,针对程序模块的最小单元(类中的方法)进行正确性检验的测试工作.python+selenium自动化测试中通常使用unittest或者pytest作为单元 ...
- Appium自动化 - 设置unicodeKeyboard: True运行脚本后,手机输入时无法调出软键盘
问题背景 做appium自动化的时候,使用了UiAutomator1驱动,然后设置了UnicodeKeyboard 执行自动化脚本之后,玩手机的时候发现平时用的输入法键盘没法调出来了 'automat ...
- 除了chrome、Firefox之外其他浏览器全都连不上网
在调试jsp时,总是会遇到eclipse打开jsp网页失败,没有网络,浏览器也除了chrome.Firefox之外其他浏览器全都连不上网,这里我也不清楚是什么问题,但是解决方法是: 打开Interne ...
- postfix基础邮件服务
postfix基础邮件服务 案例1:postfix基础邮件服务 1.1 问题 本例 ...
- scratch 如何改变变量的作用域
在新建变量的时候,有个选项是“适用于所有角色”还是“仅适用于当前角色”.通常称前者为全局变量,所有角色都可以访问到这个变量:后者,称为局部变量,只能在当前角色里访问到这个变量.例如,在使用克隆功能时, ...
- Java第十三天,内部类
内部类 一.①成员内部类.②局部内部类(包含③匿名内部类) 1.内部类用外部类属性和方法的时候,可以随意进行访问. 2.外部类用内部类属性和方法的时候,需要通过内部类对象访问. 3.在编译成class ...
- matplotlib中的基本概念
有外语基础的朋友看这里: matplotlib官方文档 Figure(图像): 组成部分