java成神之——MySQL Connector/J 的基本使用

  • 使用示例
  • DBCP连接池
  • 结语

使用示例

public class demo {
    static Connection con = null;
    static Statement st = null;
    static ResultSet rs = null;

    public static void main(String[] args) {
        try {
            String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
            String username = "root";
            String password = "123";
                        
            con = DriverManager.getConnection(url, username, password);
            st = con.createStatement();
                        
            /* 
            * 插入操作
            * String sql = "insert into event values (\"xiaohuang\",\"1920-12-09\",\"birthday\",\"First birthday\");";
            * int i = st.executeUpdate(sql);
            * 
            * 
            * 查找操作
            * String sql = "SELECT * FROM test.event;";
            * rs = st.executeQuery(sql);
            * while(rs.next()) {
            *       System.out.println(rs.getObject("petname") +"   "+ rs.getObject("eventdate")+ "   " +rs.getObject("eventtype")+"   "+rs.getObject("remark"));
            * }
            * 
            * 
            * 传递sql参数
            * String sql = "SELECT * FROM test.event where petname=?;";
            * PreparedStatement psql = con.prepareStatement(sql);
            * psql.setString(1, "xiaohuang");
            * rs = psql.executeQuery();
            * while(rs.next()) {
            *       System.out.println(rs.getObject("petname") +"   "+ rs.getObject("eventdate")+ "   " +rs.getObject("eventtype")+"   "+rs.getObject("remark"));
            * } 
            * */
                        
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                st.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

DBCP连接池

连接池能够对并发连接节省创建资源链接对象的开销

public class demo {
    static Statement st = null;
    static ResultSet rs = null;

    @SuppressWarnings("resource")
    public static void main(String[] args) {
        try {
            String url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
            String username = "root";
            String password = "123";
                
            BasicDataSource ds = new BasicDataSource();
            ds.setUrl(url);
            ds.setUsername(username);
            ds.setPassword(password);
            ds.setInitialSize(10);  // 设置连接数
            ds.setMaxIdle(5);       // 设置最大空闲连接数
            ds.setMinIdle(1);       // 设置最小空闲连接数
                
            Connection con = ds.getConnection();
            st = con.createStatement();
                
            String sql = "insert into test values (7, \"小董\", 30, 'f');";
            st.executeUpdate(sql);
                
            rs = st.executeQuery("SELECT * FROM test;");
            while(rs.next()) {
                System.out.println(rs.getObject("id") +"   "+ rs.getObject("name")+ "   " +rs.getObject("age")+"   "+rs.getObject("sex"));
            }
        } catch (SQLException e) {
            try {
                if(st != null) st.close();
                if(rs != null) rs.close();
                    
            } catch (SQLException e1) {
                    
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
        }
    }
}

结语

本文章是java成神的系列文章之一

如果你想知道,但是本文没有的,请下方留言

我会第一时间总结出来并发布填充到本文

你可能感兴趣的:(java成神之——MySQL Connector/J 的基本使用)