使用JAVA jdbc编写一段快速连接Mysql数据库

时间:2019-03-25
本文章向大家介绍使用JAVA jdbc编写一段快速连接Mysql数据库,主要包括使用JAVA jdbc编写一段快速连接Mysql数据库使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

使用JAVA jdbc编写一段快速连接Mysql数据库

第一次写文档,看的人轻喷。做一个简单记录

话不多说,下面直接上代码。

public static void main(String[] args) {
        Connection conn = null;
        String sql;
//        String url = "jdbc:mysql://localhost:3306/test?" +
//                "user=root&password=root&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT";
        String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT";
        String user = "root";
        String password = "root";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            log.info("成功加载MySql驱动程序");
            conn = DriverManager.getConnection(url, user, password);
            Statement stmt = conn.createStatement();
            sql = "create table student(No char(30), name varchar(30), primary key(No))";
            int result = stmt.executeUpdate(sql);
            if (result != -1) {
                log.info("创建数据表成功");
                sql = "insert into student(no, name) values ('201901', 'sui123')";
                result = stmt.executeUpdate(sql);
                sql = "insert into student(no, name) values ('201902', 'ya123')";
                result = stmt.executeUpdate(sql);
                sql = "select * from student";
                ResultSet rs = stmt.executeQuery(sql);
                while (rs.next()) {
                    log.info(rs.getString(1) + "\t" + rs.getString(2));
                }
            }
        } catch (ClassNotFoundException e) {
            log.error("没有找到驱动类");
            throw new RuntimeException("没有找到加载类");
        } catch (SQLException e) {
            log.error("sql不正确", e.getMessage(), e);
            throw new RuntimeException("sql不正确");
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                log.error("关闭连接失败");
                throw new RuntimeException("关闭连接失败");
            }
        }
    }

这样