zoukankan      html  css  js  c++  java
  • jdbc连接mysql数据库

    一、建立名叫jdbc连接mysql数据库的工程(Java和javaweb工程都行)

    我用javaweb工程

    二、建立MysqlDemo类调用数据库ConnectSql类的各种方法

    1、MysqlDemo类以调用select(sql)查询数据库为列

    public class MysqlDemo {
    
        public static void main(String[] args) {
            /**
             * 查询数据库
             */
            {
                String sql = "select * from dept";
                ConnectSql c = new ConnectSql();
                try {
                    ResultSet rs = c.select(sql);
                    while (rs.next()) {
                        System.out.print(rs.getString("deptno")+"  ");
                        System.out.print(rs.getString("deptname")+"  ");
                        System.out.println(rs.getString("age"));
                    }
                } catch (Exception e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
            }
        }
    }

    2、登录mysql数据库

    private static final String DBDRIVER = "com.mysql.jdbc.Driver";// MySql数据库驱动
        private static final String databasename = "zxg";//数据库名称
        private static final String DBURL = "jdbc:mysql://localhost:3306/"+databasename;// 连接数据库
        private static final String DBUSER = "root";// 数据库用户登录名称
        private static final String DBPASSWORD = "123";// 数据库用户登录密码
        private static Connection conn = null;
        private static Statement ps = null;
        private static ResultSet rs = null;
    
        public static Connection getConnection() {
            try {
                Class.forName(DBDRIVER);// 加载MySql数据库驱动
                conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);// 连接数据库
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }

    3、插入数据

    // 插入数据
        public int insert(String sql) throws Exception {
            int num = 0;// 计数器
            try {
                conn = getConnection();
                ps = conn.createStatement();
                ps.executeUpdate(sql);
            } catch (SQLException sqle) {
                throw new SQLException("insert data Exception: " + sqle.getMessage());
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                        num++;
                    }
                } catch (Exception e) {
                    throw new Exception("ps close exception: " + e.getMessage());
                }
                try {
                    if (conn != null) {
                        conn.close();
                        num++;
                    }
                } catch (Exception e) {
                    throw new Exception("conn close exception: " + e.getMessage());
                }
            }
            return num;
        }

    4、修改数据

    // 更新 修改数据
        public int update(String sql) throws Exception {
            int num = 0;
            try {
                conn = getConnection();
                ps = conn.createStatement();
                ps.executeUpdate(sql);
            } catch (SQLException sqle) {
                throw new SQLException("update data Exception: " + sqle.getMessage());
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } catch (Exception e) {
                    throw new Exception("ps close Exception: " + e.getMessage());
                }
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (Exception e) {
                    throw new Exception("conn close Excepiton: " + e.getMessage());
                }
            }
            return num;
        }

    5、查询数据

    // 查询数据
        public ResultSet select(String sql) throws Exception {
            try {
                conn = getConnection();
                ps = conn.createStatement();
                rs = ps.executeQuery(sql);
                return rs;
            } catch (SQLException sqle) {
                throw new SQLException("select data Exception: " + sqle.getMessage());
            } catch (Exception e) {
                throw new Exception("System error: " + e.getMessage());
            }
        }

    6、删除数据

    // 删除数据
        public int delete(String sql) throws Exception {
            int num = 0;
            try {
                conn = getConnection();
                ps = conn.createStatement();
                ps.executeUpdate(sql);
            } catch (SQLException sqle) {
                throw new SQLException("delete data Exception: " + sqle.getMessage());
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } catch (Exception e) {
                    throw new Exception("ps close Exception: " + e.getMessage());
                }
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (Exception e) {
                    throw new Exception("conn close Exception: " + e.getMessage());
                }
            }
            return num;
        }
  • 相关阅读:
    js模板
    block,inline和inline-block概念和区别
    jquery parent和parents,children和find
    (CVE-2015-5254)ActiveMQ 反序列化漏洞复现
    办公系统致远OA漏洞
    CVE-2018-16509 GhostScript 沙箱绕过(命令执行)漏洞
    phpmyadmin-相关漏洞详解
    XXL-JOB(任务调度中心)-反弹getshell
    组策略(域和域服务的搭建)
    解决在NAT模式下物理机无法ping通虚拟机的问题
  • 原文地址:https://www.cnblogs.com/zhangxiangguo/p/5401057.html
Copyright © 2011-2022 走看看