zoukankan      html  css  js  c++  java
  • JDBC

    持久化

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

    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
  • 相关阅读:
    Python3常用学习网站总结(随时更新)
    Python自学笔记-生成器(来自廖雪峰的官网Python3)
    Python自学笔记-lambda函数(来自廖雪峰的官网Python3)
    Python自学笔记-sorted()函数(来自廖雪峰的官网Python3)
    Python自学笔记-filter()函数(来自廖雪峰的官网Python3)
    Python自学笔记-map和reduce函数(来自廖雪峰的官网Python3)
    Python自学笔记-列表生成式(来自廖雪峰的官网Python3)
    Python自学笔记-关于切片(来自廖雪峰的官网Python3)
    Python自学笔记-递归函数(来自廖雪峰的官网Python3)
    Jmeter测试JDBC
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11349706.html
Copyright © 2011-2022 走看看