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 ...
随机推荐
- EF架构~为EF DbContext生成的实体添加注释(T5模板应用)
回到目录 相关文章系列 第八回 EF架构~将数据库注释添加导入到模型实体类中 第二十一回 EF架构~为EF DbContext生成的实体添加注释(T4模板应用) 第二十二回EF架构~为EF DbCo ...
- MySQL账户安全设置
一般来说,安装完MySQL后,默认的用户是root,密码123456,外网不能访问. 有时候也希望在外网访问,则可以添加一个账户.建议不要用root. 如下表,存在安全问题: mysql> se ...
- salesforce 零基础学习(二十八)使用ajax方式实现联动
之前的一篇介绍过关于salesforce手动配置关联关系实现PickList的联动效果,但是现实的开发中,很多数据不是定死的,应该通过ajax来动态获取,本篇讲述通过JavaScript Remoti ...
- How Google TestsSoftware - A Break for Q&A
New material for the thisseries is coming more slowly. I am beginning to get into areas where I want ...
- 部署到IIS报错:HTTP错误500.19,错误代码0x800700d
title=部署到IIS报错:HTTP错误500.19,错误代码0x800700d. 用vs直接运行网站没问题,部署到IIS就报错,由此可知应该是IIS中不支持网站相关配置. 查找发现在web.c ...
- SVN更改登录用户
如果装了TortoiseSVN: Settings -> Saved Data -> Authentication Data -> clear.即可清除保存的上个用户登录信息:当再次 ...
- AIX下如何根据端口号查找相应的进程
1. $ netstat -Aan |grep 8080 f1000e0002321bb8 tcp 0 0 *.8080 *.* LISTEN 2. $ rmsock f1000e0002321bb8 ...
- JavaScript作用域原理(二)——预编译
JavaScript是一种脚本语言, 它的执行过程, 是一种翻译执行的过程.并且JavaScript是有预编译过程的,在执行每一段脚本代码之前, 都会首先处理var关键字和function定义式(函数 ...
- Pure – 赞!轻量的、响应式的 CSS 模块集
Pure 是一组轻量的,响应式的 CSS 模块,您可以使用在任何的 Web 项目中.充分考虑了移动设备中的使用,保持文件体积尽量小,每行 CSS 都进行了仔细的考虑. Pure 基于 Normaliz ...
- 纯JS实现可拖拽表单
转载注明出处!!! 转载注明出处!!! 转载注明出处!!! 因为要用到可拖拽表单,个人要比较喜欢自己动手,不怎么喜欢在不懂实现或者原理的情况下用插件,所以查找资料实现了一个. 思路:放入:用mouse ...