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


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

    }

  • 相关阅读:
    【lwip】04内存管理
    微信小程序之获取当前位置经纬度以及地图显示
    Apache与Tomcat的区别和联系
    Jfinal中Db类的的使用
    利用GROUP_CONCAT和GROUP BY实现字段拼接
    实现easyui combobox中textField字段的拼接
    Jfinal数据库操作语句中占位符的使用
    Django项目部署到Apache服务器
    函数编程概念
    腾讯官网 样式初始化
  • 原文地址:https://www.cnblogs.com/llguanli/p/8458905.html
Copyright © 2020-2023  润新知