zoukankan      html  css  js  c++  java
  • jdbc 回顾

    JDBC实现基本的CRUD示例
    private static void insertTest() throws SQLException
    {
        String dbURL = "jdbc:mysql://localhost/test";
        Connection con = DriverManager.getConnection(dbURL, "root", "123");
        Statement st = con.createStatement();
        st.execute("insert into user(ID,NAME) values(1, 'Zhang San')");
        st.execute("insert into user(ID,NAME) values(2, 'Li Si')");
        st.execute("insert into user(ID,NAME) values(3, 'Wang Wu')");
        System.out.println("=====insert test=====");
        showUser(st);
        st.close();
        con.close();
    }
    
    private static void deleteTest() throws SQLException
    {
        String dbURL = "jdbc:mysql://localhost/test";
        Connection con = DriverManager.getConnection(dbURL, "root", "123");
        Statement st = con.createStatement();
        st.execute("delete from user where ID=3");
        System.out.println("=====delete test=====");
        showUser(st);
        st.close();
        con.close();
    }
    
    private static void updateTest() throws SQLException
    {
        String dbURL = "jdbc:mysql://localhost/test";
        Connection con = DriverManager.getConnection(dbURL, "root", "123");
        Statement st = con.createStatement();
        st.executeUpdate("update user set NAME='TEST' where ID=2");
        System.out.println("=====update test=====");
        showUser(st);
        st.close();
        con.close();
    }
    
    private static void showUser(Statement st) throws SQLException
    {
        ResultSet rs = st.executeQuery("select ID, NAME from user");
        while(rs.next())
        {
            int id = rs.getInt("ID");
            String name = rs.getString("NAME");
            System.out.println("ID:" + id + "; NAME=" + name);
        }
        rs.close();
    }
  • 相关阅读:
    2016-06-06:X264码率控制
    2016-04-12:图像差异查找算法
    2016-03-24:Windows内存泄露分析工具
    2016-03-15:关于VS中模块定义文件
    2016-03-10:libx265源码解析
    MSSQL字符串取相应的第几个数组值
    MSSQL字符串分割
    list 属性字段直接转成字符串数组
    WebApiTestClient
    获取文件路径
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/8143705.html
Copyright © 2011-2022 走看看