ruijiege

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

持久化

把内存中的数据写入磁盘,达到持久化,使数据永久的存储在磁盘上

JDBC

Java中的数据写入数据库达到持久化,因为数据库的内容是存入磁盘的,因为Java代码不可能使用每个数据库厂商写一个类,所以由数据库厂商写接口实现连接所以要导包

Mysql-connectior-java-5.1.2实现使用驱动

三种实现但是常用为Class.forName(“完全限定名”);

贾琏欲执事

DirverManger.getConnection(url,root,mima)

Conn.createstatement();

执行sql语句

工具类

因为当每次都要使用连接Driver所以每次连接数据都要进行这样的操作,和释放资源而我们希望每次把方法写在一个类中这样就能实现调用工具操作,而在工具我们需要每次都Class.forName可以写入静态代码块这样就能提高加载速率

 

package cn.jiedada.dao;

import cn.jiedada.domin.User;

public interface Basic {
    void add(User u);
    void del(User u);
    User select(User u);
    void update(User u);
}

 

package cn.jiedada.dao;

import cn.jiedada.domin.User;

public interface IUserDao extends Basic{
    
}
View Code
package cn.jiedada.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.jiedada.dao.IUserDao;
import cn.jiedada.domin.User;
import cn.jiedada.jdbcutil.JDBCUtil;

public class UserDaoImpl implements IUserDao{

    @Override
    public void add(User u) {
        Connection conn = JDBCUtil.getinstance().getConnetion();
        try {
            Statement statement = conn.createStatement();
            String sql="insert into user(username,password) values('"+u.getUsername()+"','"+u.getPassword()+"')";
            statement.execute(sql);
            JDBCUtil.getinstance().del(null, statement, conn);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

    @Override
    public void del(User u) {
        Connection conn = JDBCUtil.getinstance().getConnetion();
        try {
            Statement statement = conn.createStatement();
            String sql="delete from user where id='"+u.getId()+"'";
            statement.execute(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
    }

    @Override
    public User select(User u) {
        Connection conn = JDBCUtil.getinstance().getConnetion();
        User user=null;
        try {
            Statement statement = conn.createStatement();
            String sql="select * from user where username='"+u.getUsername()+"' and password='"+u.getPassword()+"'";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                user=new User();
                user.setId(rs.getLong("id"));
                user.setUsername("username");
                user.setPassword("password");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return user;
        
    }

    @Override
    public void update(User u) {
        Connection conn = JDBCUtil.getinstance().getConnetion();
        try {
            Statement statement = conn.createStatement();
            String sql="update user set username='"+u.getUsername()+"',password='"+u.getPassword()+"' where id='"+u.getId()+"'";
            statement.execute(sql);
            JDBCUtil.getinstance().del(null, statement, conn);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}
View Code
package cn.jiedada.jdbcutil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtil {
    //单列模式
    private JDBCUtil(){}
    static private JDBCUtil instance;
    static Properties prop;
    public static JDBCUtil getinstance() {
        //获得对象
        return instance;
    }
    static{
        //提高加载效率
        prop = new Properties();
        instance=new JDBCUtil();
        try {
            //使用load获得文件中的键值对
            prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
            //获得加载器
            Class.forName(prop.getProperty("classname"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public  Connection getConnetion() {    
        Connection conn=null;
        try {
            //获得连接
             conn = DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("username"), prop.getProperty("password"));
            return conn;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }
    public  void del(ResultSet rs,Statement st,Connection conn) {
            try {
                //判断关闭资源
                if(rs!=null)rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                try {
                    if(st!=null)
                    st.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally {
                    try {
                        if(conn!=null)
                        conn.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
    }
}
View Code

 

posted on 2019-08-14 08:45  哦哟这个怎么搞  阅读(164)  评论(0编辑  收藏  举报