第一部分代码(实体类)

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. C#学习系列-类与结构的区别

    参考:http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx?et=9851&m=9830&ct=31038 如 ...

  2. 领会CSS,实际中的研究

    虽懂却不会,真是可怕,自认懂却了无. 善用CSS属性选择器 在用于区别和唯一的情况下完全可以使用属性选择器,而没有必要使用class或id p[city="http://www.css.co ...

  3. 你的 mixin 兼容 ECMAScript 5 吗

    原文:Are your mixins ECMAScript 5 compatible? 作者:Nicholas C. Zakas 我最近在与客户合作的项目中,需要充分利用的 ECMAScript 5, ...

  4. android NDK 生成so 文件流程-ecplice

    1:生成jni目录 首先说一句网上,大部分博客这么写的:打开控制台,进入项目目录,运行javah -classpath bin/classes -d jni com.example.hellojni. ...

  5. jQuery插件之ajaxFileUpload

    原文:http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html ajaxFileUpload是一个异步上传文件的jQuery插件 ...

  6. javase基础复习攻略《六》

    学习JAVA的同学都知道,sun为我们封装了很多常用类,本篇就为大家总结一下我们经常使用的类.上一篇博客一位朋友留言问我String是不是引用数据类型?我通过查找资料发现String属于应用数据类型, ...

  7. Java多线程系列--“JUC集合”09之 LinkedBlockingDeque

    概要 本章介绍JUC包中的LinkedBlockingDeque.内容包括:LinkedBlockingDeque介绍LinkedBlockingDeque原理和数据结构LinkedBlockingD ...

  8. 转载--tomcat整合apr

    原文地址: http://zhaosheng.wolf.blog.163.com/blog/static/115304589201212845341723/ APR(Apache Portable R ...

  9. Android监听来电和去电

    要监听android打电话和接电话,只需下面2步骤1.第一步,写一个Receiver继承自BroadcastReceiver import android.app.Service; import an ...

  10. 在jfinal中使用druid,并配置查看权限

    首先导入druid包,然后配置configPlugin @Override public void configPlugin(Plugins me) { /**配置druid数据连接池插件**/ Dr ...