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

本篇文章接上一篇 使用的连接是上一章的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;
}

}

转载于:https://www.cnblogs.com/llguanli/p/8458905.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值