• JDBC


    JDBC简介

       

     JDBC的工作原理

      

     JDBC API

      

    连接数据库

     JDBC驱动

      

     使用JDBC-ODBC桥方式连接数据库

      

      

     使用纯Java方式连接数据库

      

      

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    import org.apache.log4j.Logger;
    
    public class Conn {
        private static Logger logger = Logger.getLogger(Conn.class.getName());
    
        public static void main(String[] args) {
            Connection conn = null;
            // 1.加载驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                logger.error(e);
            }
            // 2.建立连接
            try {
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/epet", "epetadmin", "0000");
                System.out.println("建立连接成功!");
            } catch (SQLException e) {
                logger.error(e);
            } finally {
                // 3.关闭连接
                try {
                    if (conn != null) {
                        conn.close();
                        System.out.println("关闭连接成功!");
                    }
                } catch (SQLException e) {
                    logger.error(e);
                }
            }
        }
    }
    连接数据库

    Statement和ResultSet

      

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.apache.log4j.Logger;
    
    public class Insert {
        private static Logger logger = Logger.getLogger(Insert.class.getName());
    
        public static void main(String[] args) {
            Connection conn = null;
            Statement stmt = null;
            String name = "欧欧";
            int health = 100;
            int love = 60;
            String strain = "酷酷的雪纳瑞";
            // 1.加载驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                logger.error(e);
            }
            // 2.建立连接
            try {
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/epet", "epetadmin", "0000");
                stmt = conn.createStatement();
                // insert into dog(name,health,love,strain)
                // value('欧欧',100,60,'酷酷的雪纳瑞');
                StringBuffer sql = new StringBuffer(
                        "insert into dog(name,health,love,strain) value('");
                sql.append(name + "',");
                sql.append(health + ",");
                sql.append(love + ",'");
                sql.append(strain + "')");
                stmt.execute(sql.toString());
                logger.info("插入狗狗信息成功!");
            } catch (SQLException e) {
                logger.error(e);
            } finally {
                // 3.关闭连接
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    logger.error(e);
                }
            }
        }
    }
    添加数据
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.apache.log4j.Logger;
    
    public class Update {
        private static Logger logger = Logger.getLogger(Update.class.getName());
    
        public static void main(String[] args) {
            Connection conn = null;
            Statement stmt = null;
            // 1.加载驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                logger.error(e);
            }
            // 2.建立连接
            try {
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/epet", "epetadmin", "0000");
                stmt = conn.createStatement();
                stmt.executeUpdate("update dog set health=80,love=15 where id=1");
                logger.info("成功更新了狗狗信息!");
            } catch (SQLException e) {
                logger.error(e);
            } finally {
                // 3.关闭连接
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    logger.error(e);
                }
            }
        }
    }
    修改数据
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.apache.log4j.Logger;
    
    public class Query {
        private static Logger logger = Logger.getLogger(Query.class.getName());
    
        public static void main(String[] args) {
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;
            // 1.加载驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                logger.error(e);
            }
            // 2.建立连接
            try {
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/epet", "epetadmin", "0000");
                stmt = conn.createStatement();
                rs = stmt
                        .executeQuery("select id,name,health,love,strain from dog");
                while (rs.next()) {
                    System.out.println(rs.getInt(1));
                    System.out.println(rs.getString(2));
                    System.out.println(rs.getInt("health"));
                    System.out.println(rs.getInt("love"));
                    System.out.println(rs.getString("strain"));
                }
                logger.info("查询狗狗信息成功!");
            } catch (SQLException e) {
                logger.error(e);
            } finally {
                // 3.关闭连接
                try {
                    if (rs != null) {
                        rs.close();
                    }
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    logger.error(e);
                }
            }
        }
    }
    查询数据

    PreparedStatement接口

      

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    import org.apache.log4j.Logger;
    
    public class Update2 {
        private static Logger logger = Logger.getLogger(Update2.class.getName());
    
        public static void main(String[] args) {
            Connection conn = null;
            PreparedStatement pstmt = null;
            // 1.加载驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                logger.error(e);
            }
            // 2.建立连接
            try {
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/epet", "epetadmin", "0000");
                String sql = "update dog set health=?,love=? where id=?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setInt(1, 80);
                pstmt.setInt(2, 15);
                pstmt.setInt(3, 1);
                pstmt.executeUpdate();
                logger.info("成功更新了狗狗信息!");
            } catch (SQLException e) {
                logger.error(e);
            } finally {
                // 3.关闭连接
                try {
                    if (pstmt != null) {
                        pstmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    logger.error(e);
                }
            }
        }
    }
    PreparedStatement
  • 相关阅读:
    arcgis使用python,做arctoolbox,渔网裁剪gdb里的要素,四等分
    arcgis计算中心点的质心XY,并根据属性Label进行标注。
    arcpy对要素类每个图斑批量出图。
    python安装模块wheel步骤
    arcgis中shp文件的字符串string型字段转换为日期型Date格式。如“20190426”转为“2019/4/26”
    .tar.gz海量遥感影像解压
    python压缩解压文件(天地图切片批量解压缩)
    python读取多层嵌套文件夹中的文件(zip文件嵌套在不同层级的文件夹中)
    使用Binding时关于数据更新的注意事项
    滑动列表底部自动加载下一页。修改旧代码ing
  • 原文地址:https://www.cnblogs.com/xhddbky/p/9244631.html
Copyright © 2020-2023  润新知