JDBC增删改查和查唯一的完整代码
第一部分代码(实体类)
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增删改查和查唯一的完整代码的更多相关文章
- JDBC增删改查,PreparedStatement和Statement的区别
此篇是在上一篇的基础上使用PreparedStatement对象来实现JDBC增删改查的 具体工具类JDBCTools和实现类和配置文件在上一篇Statement对象实现的时候有写. 上一篇地址htt ...
- JDBC增删改查
/* db.properties的配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/day14 username=root ...
- jdbc 增删改查以及遇见的 数据库报错Can't get hostname for your address如何解决
最近开始复习以前学过的JDBC今天肝了一晚上 来睡睡回笼觉,长话短说 我们现在开始. 我们先写一个获取数据库连接的jdbc封装类 以后可以用 如果不是maven环境的话在src文件下新建一个db.pr ...
- jdbc增删改查进行封装
jdbc封装 1 dao (代码分层) com.aaa.dao 存放dao相关的类型 例如 StudentDAOImpl 处理 数据库的链接 存取数据 com.aaa.servlet 存放servle ...
- JAVA JDBC 增删改查简单例子
1.数据库配置文件jdbc.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username= ...
- JDBC 增删改查代码 过滤查询语句
package test; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; i ...
- JDBC增删改数据库的操作
JDBC入门及简单增删改数据库的操作 一.JDBC的引入 1.JDBC的概念 JDBC:Java Database Connection,表示数据库连接(任何数据库都支持JDBC的连接),是一个独立于 ...
- JDBC增删改查简单测试
首先编写一个entity以便与数据库表文件相对应 lyTable.java public class LyTable implements java.io.Serializable { private ...
- 商城项目整理(三)JDBC增删改查
商品表的增加,修改,删除,订单表的增加,确认,用户表的查看,日志表的增加,查看 商品表建表语句: create table TEST.GOODS_TABLE ( gid NUMBER not null ...
随机推荐
- python面向对象随笔
传送门:http://www.runoob.com/python/python-object.html
- lua的table表处理 及注意事项
lua,一款很轻量级很nice很强大的脚本语言,做为lua中使用最为频繁的table表,在使用之时还是有颇多的好处与坑的: 下面是大牛 云风的一片关于lua table的blog,可使得对lua ta ...
- Atitit 为什么网络会有延时 电路交换与分组交换的区别
Atitit 为什么网络会有延时 电路交换与分组交换的区别 按道理,网络是电子设备联网,应该达到光速才对.. 本质上因为互联网基于分组交换而不是电路交换. 分组交换相当于队列方式,别人发你的数据包先存 ...
- paip.lucene 4.3 中文语义搜索最佳实践
paip.lucene 4.3 中文语义搜索最佳实践 首先一个问题是要不要使用lucene 自带的分词器...我觉得最好不使用哪自带的分词器.效果还凑火,就是不好控制... 先使用ik,ict,mms ...
- CI Weekly #2 | 如何优化开发流程,实现项目持续集成?
原文首发于 flow.ci Blog >> 链接,转载请联系:) CI Weekly 围绕『 软件工程效率提升』 进行一系列技术内容分享,包括国内外持续集成.持续交付,持续部署.自动化测试 ...
- iOS-MVC模式
提到ios中的mvc不得不提2011秋季斯坦福课程的老头,他的iphone开发公开课是所有描述ios中mvc模式最为准确并且最为浅显易懂的. 模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把 ...
- 读《编写可维护的javascript》笔记
第一章 基本的格式化 缩进层级:推荐 tab:4; 换行:在运算符后面换行,第二行追加两个缩进: // Good: Break after operator, following line inden ...
- css text-align-last设置末尾文本对齐方式
text-align-last:auto | start | end | left | right | center | justify auto: 无特殊对齐方式. left: 内容左对齐. cen ...
- .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法
一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...
- 后端码农谈前端(CSS篇)第五课:CSS样式
一.背景: CSS 允许应用纯色作为背景,也允许使用背景图像创建相当复杂的效果. 1.背景色 可以使用 background-color 属性为元素设置背景色.这个属性接受任何合法的颜色值. 例如: ...