第一部分代码(实体类)

package com.wf.entity;

public class Hehe{

private int hehe_id;

private String hehe_name;

private String hehe_gender;

public int getHehe_id(){

return hehe_id;

}

public void setHehe_id(int heheId){

hehe_id=heheId;

}

public String getHehe_name() {
return hehe_name;
}
public void setHehe_name(String heheName) {
hehe_name = heheName;
}
public String getHehe_gender() {
return hehe_gender;
}
public void setHehe_gender(String heheGender) {
hehe_gender = heheGender;
}

}

第二部分 BaseDao(增删改查和查唯一)

package com.wf.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao{

protected static final String DRIVER="com.mysql.jdbc.Driver";

protected static final String URL="mysql://localhost:3306/mysql";

protected static final String USER="root";

protected static final String PASSWORD="******";

protected Connection connection=null;

protected PreparedStatement preparedStatement=null;

protected ResultSet resultSet =null;

protected void getconnection() throws ClassNotFoundException, SQLException{

Class.forName(DRIVER);

this.connection =DriverManager.getconnection (URL,USER,PASSWORD);

}

/**
* 通用的增删改方法
* @param sql SQL语句
* @param params 参数数组
* @return 受影响的行数
*/

protected int executeUpdate(String sql ,String[]params){
int result=-1;
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {

this.preparedStatement.setString(i+1, params[i]);


}

}
result= this.preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {

e.printStackTrace();
} catch (SQLException e) {

e.printStackTrace();
}finally{
this.close();
}
return result;
}

/**
* 查询结果集的方法
* @param sql
* @param params
*/
protected void executeQuery(String sql,String[]params){

try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
}

}
this.resultSet=this.preparedStatement.executeQuery();
} catch (ClassNotFoundException e) {

e.printStackTrace();
} catch (SQLException e) {

e.printStackTrace();
}

}

/**
* 查询唯一的结果
* @return Object
*/
protected Object executeQueryUnique(String sql,String[]params){
Object object=null;
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
}

}
this.resultSet=this.preparedStatement.executeQuery();
if(this.resultSet.next())

object=this.resultSet.getObject(1);
} catch (ClassNotFoundException e) {

e.printStackTrace();
} catch (SQLException e) {

e.printStackTrace();
}

return object;
}

protected void close(){
try {
if(null!=this.resultSet)
this.resultSet.close();
if(null!=this.preparedStatement)
this.preparedStatement.close();
if(null!=this.connection)
this.connection.close();
} catch (SQLException e) {

e.printStackTrace();
}

}

第三部分 HeheDao

package com.wf.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.wf.entity.Hehe;

public class HeheDao extends BaseDao{
public boolean insert(Hehe hehe){


String sql="insert into hehe(hehe_name,hehe_gender) values(?,?)" ;
String []params=new String[]{hehe.getHehe_name(),hehe.getHehe_gender()};
return this.executeUpdate(sql, params)>0? true:false;

}

第四部分 测试Test_BaseDao_Insert

package com.wf.test;

import com.wf.dao.HeheDao;
import com.wf.entity.Hehe;

public class Test_BaseDao_Insert {
public static void main(String[] args) {
Hehe hehe=new Hehe();

hehe.setHehe_name("个");
hehe.setHehe_gender("b");

HeheDao _hd=new HeheDao();
if(_hd.insert(hehe))
System.out.println("成功!");
else
System.out.println("失败!");
}

}

JDBC增删改查和查唯一的完整代码的更多相关文章

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

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

  2. JDBC增删改查

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

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

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

  4. jdbc增删改查进行封装

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

  5. JAVA JDBC 增删改查简单例子

    1.数据库配置文件jdbc.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username= ...

  6. JDBC 增删改查代码 过滤查询语句

    package test; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; i ...

  7. JDBC增删改数据库的操作

    JDBC入门及简单增删改数据库的操作 一.JDBC的引入 1.JDBC的概念 JDBC:Java Database Connection,表示数据库连接(任何数据库都支持JDBC的连接),是一个独立于 ...

  8. JDBC增删改查简单测试

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

  9. 商城项目整理(三)JDBC增删改查

    商品表的增加,修改,删除,订单表的增加,确认,用户表的查看,日志表的增加,查看 商品表建表语句: create table TEST.GOODS_TABLE ( gid NUMBER not null ...

随机推荐

  1. AngularJS 源码分析1

    AngularJS简介 angularjs 是google出品的一款MVVM前端框架,包含一个精简的类jquery库,创新的开发了以指令的方式来组件化前端开发,可以去它的官网看看,请戳这里 再贴上一个 ...

  2. [Redis]Redis 概述及基本使用规范.

    1 nosql的简介 1.1 nosql简介 随着互联网Web2.0网站的兴起,传统的关系数据库在应付Web2.0网站,特别是超大规模和高并发的SNS类型的Web2.0纯动态网站已经显得力不从心,暴露 ...

  3. [数据库连接池] Java数据库连接池--DBCP浅析.

    前言对于数据库连接池, 想必大家都已经不再陌生, 这里仅仅设计Java中的两个常用数据库连接池: DBCP和C3P0(后续会更新). 一. 为何要使用数据库连接池假设网站一天有很大的访问量,数据库服务 ...

  4. OutputCache属性详解(三)— VaryByHeader,VaryByCustom

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  5. Redis 监控

    redis 监控有 redis-live 和 redis-stat Redis-Live是一个用来监控redis实例,分析查询语句并且有web界面的监控工具,使用python编写. redis-sta ...

  6. iOS----CocoaPods的安装、使用和,原理+参考流程+常见问题

    一.什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理.开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和第 ...

  7. html5 响应式布局

    一.什么是响应式布局?       响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简而言之,就是一个网站能够兼容多个终端——而不是为每个终端做一个特定的版本. 这个概念是为 ...

  8. JavaScript实现DDoS攻击原理,以及保护措施。

    DDos介绍 最普遍的攻击是对网站进行分布式拒绝服务(DDoS)攻击.在一个典型的DDoS攻击中,攻击者通过发送大量的数据到服务器,占用服务资源.从而达到阻止其他用户的访问. 如果黑客使用JavaSc ...

  9. 深入理解CSS浮动

    × 目录 [1]定义 [2]特性 [3]表现 [4]重叠 前面的话 浮动最早的使用是出自<img src="#" align="right">,用于 ...

  10. Cocos2d-x 3.2 学习笔记(四)学习打包Android平台APK!

    从cocos2dx 3.2项目打包成apk安卓应用文件,搭建安卓环境的步骤有点繁琐,但搭建一次之后,以后就会非常快捷! (涉及到3.1.1版本的,请自动对应3.2版本,3.x版本的环境搭建都是一样的) ...