• 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;
        }
  • 相关阅读:
    TTTTTTTTTTTTTTTTTTT UVA 2045 Richness of words
    hdu 5723 Abandoned country 最小生成树+子节点统计
    hdu 5792 World is Exploding 树状数组+离散化+容斥
    MySQL字符集详解
    MySQL的sql语言分类DML、DQL、DDL、DCL、
    MySQL5.6的4个自带库详解
    Win10下安装MySQL5.6
    {MySQL数据库初识}一 数据库概述 二 MySQL介绍 三 MySQL的下载安装、简单应用及目录介绍 四 root用户密码设置及忘记密码的解决方案 五 修改字符集编码 六 初识sql语句
    {python--GIL锁}一 介绍 二 GIL介绍 三 GIL与Lock 四 GIL与多线程 五 多线程性能测试
    Navicat安装及简单使用
  • 原文地址:https://www.cnblogs.com/zhangxiangguo/p/5401057.html
Copyright © 2020-2023  润新知