对数据库进行增删改查操作
2015-06-24 08:09
351 查看
本篇文章接上一篇 使用的连接是上一章的JDBC驱动链接的SQLServer驱动
package addressUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.text.DefaultEditorKit.InsertBreakAction;
import bookConnUtil.DBUtil;
public class addressUtil {
private address address = new address();
public static boolean addressUpdate(String name,String email,String phone,String company,String address) throws SQLException{
String updateWithName ="update address set email=?,phone=?,company=?,address=?where name=?";
boolean flag =false;
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=DBUtil.getConnection(DBUtil.CONNECTION_SQL);
stmt=conn.prepareStatement(updateWithName);
int count=stmt.executeUpdate();
stmt.setString(1, email);
stmt.setString(2, phone);
stmt.setString(3, company);
stmt.setString(4, address);
stmt.setString(5, name);
if (count>0){
flag=true;
}else {
flag=false;
}
}catch(Exception e){
e.printStackTrace();
//没有对其进行具体的异常处理只是进行了简单的捕获如有需要可自行修改
}finally{
conn.close();
stmt.close();
}
return flag ;
}
public static boolean addressSelect(String name) throws SQLException{
String selectAddress ="select email,phone,company,address,from address where name=?";
boolean flag =false;
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=DBUtil.getConnection(DBUtil.CONNECTION_SQL);
stmt=conn.prepareStatement(selectAddress);
int count=stmt.executeUpdate();
stmt.setString(1, name);
if (count>0){
flag=true;
}else {
flag=false;
}
}catch(Exception e){
e.printStackTrace();
//没有对其进行具体的异常处理只是进行了简单的捕获如有需要可自行修改
}finally{
conn.close();
stmt.close();
}
return flag ;
}
public static boolean addressDelete(String name) throws SQLException{
String deleteAddress ="delete * from address where name=?";
boolean flag =false;
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=DBUtil.getConnection(DBUtil.CONNECTION_SQL);
stmt=conn.prepareStatement(deleteAddress);
int count=stmt.executeUpdate();
stmt.setString(1, name);
if (count>0){
flag=true;
}else {
flag=false;
}
}catch(Exception e){
e.printStackTrace();
//没有对其进行具体的异常处理只是进行了简单的捕获如有需要可自行修改
}finally{
conn.close();
stmt.close();
}
return flag ;
}
public static boolean addressInsert(String name,String email,String phone,String company,String address) throws SQLException{
boolean flag =false ;
try
{
Connection con=null;
con=DBUtil.getConnection(1);
String Insert="insert into address values(?,?,?,?,?)";
PreparedStatement stmt = null;
stmt =con.prepareStatement(Insert);
stmt.setString(1, name);
stmt.setString(2, email);
stmt.setString(3, phone);
stmt.setString(4, company);
stmt.setString(5, address);
//System.out.println("��ѯ�ɹ�");
int count=stmt.executeUpdate();
System.out.println("11111111111111111111111111111");
if (count>0)
{
flag = true;
}else{
flag=false;
}
} catch(Exception e){
e.printStackTrace();
System.out.println("出岔子了");
}
return flag;
}
}
package addressUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.text.DefaultEditorKit.InsertBreakAction;
import bookConnUtil.DBUtil;
public class addressUtil {
private address address = new address();
public static boolean addressUpdate(String name,String email,String phone,String company,String address) throws SQLException{
String updateWithName ="update address set email=?,phone=?,company=?,address=?where name=?";
boolean flag =false;
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=DBUtil.getConnection(DBUtil.CONNECTION_SQL);
stmt=conn.prepareStatement(updateWithName);
int count=stmt.executeUpdate();
stmt.setString(1, email);
stmt.setString(2, phone);
stmt.setString(3, company);
stmt.setString(4, address);
stmt.setString(5, name);
if (count>0){
flag=true;
}else {
flag=false;
}
}catch(Exception e){
e.printStackTrace();
//没有对其进行具体的异常处理只是进行了简单的捕获如有需要可自行修改
}finally{
conn.close();
stmt.close();
}
return flag ;
}
public static boolean addressSelect(String name) throws SQLException{
String selectAddress ="select email,phone,company,address,from address where name=?";
boolean flag =false;
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=DBUtil.getConnection(DBUtil.CONNECTION_SQL);
stmt=conn.prepareStatement(selectAddress);
int count=stmt.executeUpdate();
stmt.setString(1, name);
if (count>0){
flag=true;
}else {
flag=false;
}
}catch(Exception e){
e.printStackTrace();
//没有对其进行具体的异常处理只是进行了简单的捕获如有需要可自行修改
}finally{
conn.close();
stmt.close();
}
return flag ;
}
public static boolean addressDelete(String name) throws SQLException{
String deleteAddress ="delete * from address where name=?";
boolean flag =false;
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=DBUtil.getConnection(DBUtil.CONNECTION_SQL);
stmt=conn.prepareStatement(deleteAddress);
int count=stmt.executeUpdate();
stmt.setString(1, name);
if (count>0){
flag=true;
}else {
flag=false;
}
}catch(Exception e){
e.printStackTrace();
//没有对其进行具体的异常处理只是进行了简单的捕获如有需要可自行修改
}finally{
conn.close();
stmt.close();
}
return flag ;
}
public static boolean addressInsert(String name,String email,String phone,String company,String address) throws SQLException{
boolean flag =false ;
try
{
Connection con=null;
con=DBUtil.getConnection(1);
String Insert="insert into address values(?,?,?,?,?)";
PreparedStatement stmt = null;
stmt =con.prepareStatement(Insert);
stmt.setString(1, name);
stmt.setString(2, email);
stmt.setString(3, phone);
stmt.setString(4, company);
stmt.setString(5, address);
//System.out.println("��ѯ�ɹ�");
int count=stmt.executeUpdate();
System.out.println("11111111111111111111111111111");
if (count>0)
{
flag = true;
}else{
flag=false;
}
} catch(Exception e){
e.printStackTrace();
System.out.println("出岔子了");
}
return flag;
}
}
相关文章推荐
- mysql、JDBC、JDBC驱动连接数据库
- Androiod开发历程(SQLite)
- 笔记-Microsoft SQL Server 2008技术内幕:T-SQL语言基础-10 可编程对象
- nodeJS链接mysql数据库(express框架)
- 笔记-Microsoft SQL Server 2008技术内幕:T-SQL语言基础-09 事务和并发
- redis client protocol 解析
- MySQL 数据类型
- MySQL优化之——自定义存储过程和函数
- web.py在SAE中的Session问题解决方法(使用mysql存储)
- 在Node.js应用中使用Redis的方法简介
- MySQL的事件调度器使用介绍
- SQLAlchemy的session
- Mysql中的count()与sum()区别详细介绍
- Oracle11g expdp impdp命令详解
- oracle增加列,删除列
- SQL三表联合查询
- SQL中常用的比较复杂的增删改查
- pdo 数据库操作抽象类
- zabbix监控mysql
- mysql的安装