测试JDBC

    A:加载驱动
        1:将相关数据库的驱动包拷到对应的应用程序中。并加载到编译路径中。

        2:使用Class.forName来加载相应的驱动程序。

    B:使用驱动管理器获取连接
        String url = "
jdbc:mysql://localhost:3306/jdbcstudy";
        String username = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, username,
                password);        

    C:构造SQL,并通过Statement对象执行SQL
        Statement stmt = conn.createStatement();
        System.out.println(stmt);
        String sql = "insert into t_mc_type(nid,sname,npid) values(15,'JDBC插入的数据','1')";
        int i = stmt.executeUpdate(sql);        
    D:关闭资源
        finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    
原文地址:https://www.cnblogs.com/xiaoqiqistudy/p/11177985.html