首页 > 数据库技术 > 详细

测试JDBC

时间:2019-07-12 19:39:20      阅读:128      评论:0      收藏:0      [点我收藏+]
    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();
            }
        }    

 

测试JDBC

原文:https://www.cnblogs.com/xiaoqiqistudy/p/11177985.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!