实验代码Javaweb

复制代码
package test;

import java.sql.*;
import java.util.regex.Pattern;

public class Data {
//getter and setter
    private String hubie,housetype,houseS,home,name,id,sex,minzu,edu;
    public String gethubie() {
        return hubie;
    }
    public void sethubie(String hubie) {
        this.hubie = hubie;
    }
    public String gethousetype() {
        return housetype;
    }
    public void sethousetype(String housetype) {
        this.housetype = housetype;
    }
    public String gethouseS() {
        return houseS;
    }
    public void sethouseS(String houseS) {
        this.houseS = houseS;
    }
    public String gethome() {
        return home;
    }
    public void sethome(String home) {
        this.home =home ;
    }
    public String getname() {
        return name;
    }
    public void setname(String name) {
        this.name =name ;
    }
    public String getid() {
        return id;
    }
    public void setid(String id) {
        this.id = id;
    }
    public String getsex() {
        return sex;
    }
    public void setssex(String sex) {
        this.sex = sex;
    }
    public String getminzu() {
        return minzu;
    }
    public void setminzu(String minzu) {
        this.minzu = minzu;
    }
    public String getedu() {
        return edu;
    }
    public void setedu(String edu) {
        this.edu = edu;
    }

    //***********************************************************************
    //数据库连接
    public Connection getConnection()//连接数据库
    {
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            //System.out.println("加载驱动成功");
        }catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        String user="root";
        String password="123456";
        String url = "jdbc:mysql://localhost:3306/ztest01?useSSL=false&serverTimezone=GMT&characterEncoding=utf-8&autoReconnect=true";
        Connection con=null;
        try{
            con=DriverManager.getConnection(url,user,password);
            //System.out.println("数据库连接成功");
        }catch(SQLException e)
        {
            e.printStackTrace();
        }

        return con;
    }
    //**********************************************************************
    //关闭方法
    public  void close (Connection con)
    {
        try{
            if(con!=null)
            {
                con.close();
            }
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    public  void close (PreparedStatement preparedStatement)
    {
        try{
            if(preparedStatement!=null)
            {
                preparedStatement.close();
            }
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    public  void close(ResultSet resultSet)
    {
        try{
            if(resultSet!=null)
            {
                resultSet.close();
            }
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    //******************************************************************
    //
    public void adddata(String hubie,String housetype,String houseS,String home,String name,String id,String sex,String minzu,String edu)
    {
        Connection connection = getConnection();
        PreparedStatement preparedStatement=null;
        try {
            //hubie,housetype,houseS,home,name,id,sex,minzu,edu;
            String sql = "insert into t0 (户别,住房类型,本户现住房面积,本户住房间数,户主姓名,身份证号,性别,民族,受教育程度) values (?,?,?,?,?,?,?,?,?)";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,hubie);
            preparedStatement.setString(2,housetype);
            preparedStatement.setString(3,houseS);
            preparedStatement.setString(4,home);
            preparedStatement.setString(5,name);
            preparedStatement.setString(6,id);
            preparedStatement.setString(7,sex);
            preparedStatement.setString(8,minzu);
            preparedStatement.setString(9,edu);
            preparedStatement.executeUpdate();
            //System.out.println("添加成功");

        } catch (SQLException  e) {
            e.printStackTrace();
        }finally{
            close(preparedStatement);
            close(connection);
        }

    }
    //
    public void deletedata(String id)
    {
        Connection connection = getConnection();
        PreparedStatement preparedStatement=null;
        try {
            String sql = "delete from t0 where 身份证号 = ?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,id);
            preparedStatement.executeUpdate();
            //System.out.println("删除成功");

        } catch (SQLException  e) {
            e.printStackTrace();
        }finally{
            close(preparedStatement);
            close(connection);
        }
    }
    //
    public void revisedata(String id0, String id, String sex, String minzu , String edu)
    {
        Connection connection = getConnection();
        PreparedStatement preparedStatement=null;
        try {
            //身份证号码、性别、民族、受教育程度
            String sql = "update t0 set 身份证号=?, 性别=?, 民族=?, 受教育程度=? where 身份证号=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,id);
            preparedStatement.setString(2,sex);
            preparedStatement.setString(3,minzu);
            preparedStatement.setString(4,edu);
            preparedStatement.setString(5,id0);
            preparedStatement.executeUpdate();

        } catch (SQLException  e) {
            e.printStackTrace();
        }finally{
            close(preparedStatement);
            close(connection);
        }
    }

    //判断方法****************************************************************
    //判空
    public boolean isEmpty(String hubie,String housetype,String houseS,String home,String name,String id,String sex,String minzu,String edu)
    {
        if(hubie==null||housetype==null||houseS==""||home==""||name==""||id==""||sex==null||minzu==""||edu=="")
            return true;
        else return false;
    }
    //判整数-面积-房间数
    public boolean isNumber(String str) {
//        使用正则表达式对定义字符串的模式
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");//^[0-9]+[0-9]*$
        return pattern.matcher(str).matches();
    }
    //判断身份证号
    public boolean isIdRight(String id)
    {
        if(id.length()==18)
        {
            for(int i=0;i<17;i++)//前17位
            {
                char c=id.charAt(i);
                if(c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9')
                {continue;}
                else {return false;}
            }
            char c=id.charAt(17);//第18位
            if(c!='0'&&c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='5'&&c!='6'&&c!='7'&&c!='8'&&c!='9'&&c!='X') {
                //System.out.println("不是数字或者X");
                return false;
            }
            else {
                //System.out.println("身份证号正确");
                return true;
            }
        }
        else //System.out.println("不是18位");
        return false;
    }
    //判重/判存在
    public boolean isSame(String s)
    {
        Connection connection = getConnection();
        PreparedStatement preparedStatement=null;
        ResultSet rs=null;
        try {
            String sql = "select * from t0";
            preparedStatement=connection.prepareStatement(sql);
            rs=preparedStatement.executeQuery();
            while(rs.next()){
                if( s.equals(rs.getObject(6))||s.equals(rs.getObject(5)) )
                    return true;
            }
            //preparedStatement.executeUpdate();

        } catch (SQLException  e) {
            e.printStackTrace();
        }finally{
            close(rs);
            close(preparedStatement);
            close(connection);
        }
        return false;
    }


    //*****************************************************************
    public static void main(String[] args)
    {
        //test.Data a=new test.Data();
    }

}
复制代码

 

本文作者:TranquilTimber

本文链接:https://www.cnblogs.com/gbrr/p/16811552.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   喝着农药吐泡泡o  阅读(33)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
🔑