码迷,mamicode.com
首页 > 数据库 > 详细

向数据库添加记录(若有则不添加)

时间:2020-02-18 15:04:41      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:mysq   code   run   public   pre   finish   读取内容   state   file   

功能:从文件读取内容经过两次分割最后将值传入bean中封装,再将bean添加到数据库中,如果数据库中有相关记录,则不添加。
package com.lq.dao;

import com.lq.bean.CiTiao;
import com.lq.file.LocalFile;
import com.lq.file.StringHandle;

import java.sql.*;
import java.util.List;

public class AddToMySql
{
    private static String DriverName="com.mysql.jdbc.Driver";
    private static String UName="root";
    private static String Upwd="root";
    private static String Url="jdbc:mysql://192.168.25.1:3306/xinjian?useUnicode=true&characterEncoding=UTF-8";
    public boolean exit(String name)   //根据词名称查询是否存在 如果存在返回true,不存在返回false
    {
        //System.out.println(query(id));
        return query(name)==1?true:false;
    }
    public int query(String name)   //查询是否存在;存在return 1;否存在return 0;
    {
        CiTiao CiTiao=null;
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet result=null;
        int count=-1;
        try {
            Class.forName(DriverName);
            conn=DriverManager.getConnection(Url, UName, Upwd);
            String sql="select count(*) from citiao where name=?";
            pstmt= conn.prepareStatement(sql);
            pstmt.setString(1,name);
            result=pstmt.executeQuery();
            if(result.next()){
                count=result.getInt(1);
                System.out.println("已存在");
            }
            if(count>0)
            return count;
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
            return count;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            return count;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return count;
        }
        finally {
            try {
                if(result!=null) result.close();
                if(pstmt!=null) pstmt.close();
                if(conn!=null) conn.close();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return count;
    }
    public boolean Add(CiTiao CiTiao)   //添加信息
    {
        Connection conn=null;
        PreparedStatement pstmt=null;
        try {
            AddToMySql addToMySql=new AddToMySql();
            //System.out.println(addToMySql.exit(CiTiao.getId()));
            if(addToMySql.exit(CiTiao.getName())!=true) {
                //System.out.println("2");
                Class.forName(DriverName);
                conn = DriverManager.getConnection(Url, UName, Upwd);
                String sql = "insert into citiao values(NULL,?,?,?)";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, CiTiao.getName());
                pstmt.setString(2, CiTiao.getDes());
                pstmt.setString(3, CiTiao.getUrl());

                int connt = pstmt.executeUpdate();
                if (connt > 0) {
                    System.out.println("保存成功");
                    return true;

                }
                else {
                    System.out.println("已存在");
                    return false;
                }
            }
            else return false;
        }
        catch(ClassNotFoundException  e)
        {
            e.printStackTrace();
            return false;  //系统异常
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            return false;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return false;
        }
        finally
        {
            try
            {
                if(pstmt!=null){
                    pstmt.close();
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }

    }
    public void Run(){                                                 //从文件读取内容经过两次分割最后将值传入bean中封装,再将bean添加到数据库中
        try {
            LocalFile localFile=new LocalFile();
            AddToMySql addToMySql=new AddToMySql();
            String []  strs = localFile.read_CiTiao_local("citiao.txt");
            String[] str=null;
            System.out.println(strs.length);
            for(int i=0;i<strs.length;i++){
                str=strs[i].split("&&");
                StringHandle stringHandle=new StringHandle();
                CiTiao CiTiao=null;
                CiTiao=new CiTiao(str[0],str[1],str[2]);
                addToMySql.Add(CiTiao);
                System.out.println(CiTiao.toString());
            }
            //System.out.println(str.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        AddToMySql addToMySql=new AddToMySql();
        addToMySql.Run();
        System.out.println("finish");
    }

}

 

向数据库添加记录(若有则不添加)

标签:mysq   code   run   public   pre   finish   读取内容   state   file   

原文地址:https://www.cnblogs.com/lq13035130506/p/12326050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!