第一部分代码(实体类)

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("失败!");
}

}

最新文章

  1. POJ2104 K-th Number[主席树]【学习笔记】
  2. entity-model-first
  3. Oracle 遇到的问题 (1)
  4. 区间K 大数查询
  5. java的HashMap与ConcurrentHashMap
  6. sdut 2153 Clockwise (2010年山东省第一届ACM大学生程序设计竞赛)
  7. Mysql 5.6 Cmake 编译安装
  8. java 静态文件使用注解
  9. thinkphp ,进行关联模型的时候出现的问题,版本是3.2
  10. Hibernate @Embeddable注释
  11. eclipse导入android studio时一些异常的处理
  12. Bzoj1176:Mokia&amp;Cogs1752:[BOI2007]摩基亚Mokia
  13. Python简单语音识别并响应
  14. 4 playlook-Jinja2 filter
  15. Java多线程中wait语句的具体使用技巧
  16. GOROOT、GOPATH、GOBIN
  17. pickle file in matlab
  18. JS图片Switchable切换大集合
  19. 【字符串算法2】浅谈Manacher算法
  20. std::string begin end

热门文章

  1. MySQL的Grant命令[转]
  2. iOS---NSAutoreleasePool自动释放原理及详解
  3. poi 输出Excel显示内容
  4. cordova添加plugin
  5. VMware虚拟机Mac OS X无法调整扩展硬盘大小,更新xcode时出现磁盘空间不足
  6. iReport 下载地址
  7. input---checked小问题
  8. 深入理解PHP内核(八)变量及数据类型-预定义变量
  9. 编译原理 LL1文法First集算法实现
  10. 自制jQuery标签插件