您的位置:首页 > 数据库

对数据库进行增删改查操作

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;

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: