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

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