jdbc 回顾

Posted kakaisgood

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
}

 

以上是关于jdbc 回顾的主要内容,如果未能解决你的问题,请参考以下文章

关于mysql驱动版本报错解决,Cause: com.mysql.jdbc.exceptions.jdbc4Unknown system variable ‘query_cache_size(代码片段

JDBC回顾

MyBatis源码分析一.JDBC回顾

MyBatis源码分析一.JDBC回顾

jdbc 回顾

面试常用的代码片段