zoukankan      html  css  js  c++  java
  • [JDBC-2] JDBC CURD

    package com.amuos.jdbc.curd;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import com.amuos.jdbc.util.JdbcUtils;
    
    /**
     * 
     * 2015-1-25
     * 
     * @author <a href="mailto:472846889@qq.com">王娟</a>
     * 
     */
    public class CRUD {
    
        /**
         * @param args
         * @throws SQLException
         */
    //    public static void main(String[] args) throws SQLException {
    ////         create();
    ////         read();
    ////         update();
    ////         delete();
    //    }
    
        static void delete() throws SQLException {
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                // 2.建立连接
                conn = JdbcUtils.getConnection();
                // conn = JdbcUtilsSing.getInstance().getConnection();
                // 3.创建语句
                st = conn.createStatement();
    
                String sql = "delete from contacts where id = 2";
    
                // 4.执行语句
                int i = st.executeUpdate(sql);
    
                System.out.println("Have deleted " + i + " row.");
            } finally {
                JdbcUtils.free(rs, st, conn);
            }
        }
    
        static void update() throws SQLException {
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                // 2.建立连接
                conn = JdbcUtils.getConnection();
                // conn = JdbcUtilsSing.getInstance().getConnection();
                // 3.创建语句
                st = conn.createStatement();
    
                String sql = "update contacts set mail2 = 'test@163.com' where name = 'test' ";
    
                // 4.执行语句
                int i = st.executeUpdate(sql);
    
                System.out.println("Have updated " + i + " row.");
            } finally {
                JdbcUtils.free(rs, st, conn);
            }
        }
    
        static void create() throws SQLException {
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                // 2.建立连接
                conn = JdbcUtils.getConnection();
                // conn = JdbcUtilsSing.getInstance().getConnection();
                // 3.创建语句
                st = conn.createStatement();
    
                String sql = "insert into contacts(name,main,mail1,mail2,relationship) values ('test', '1234565', 'test@test.com','','同事') ";
    
                // 4.执行语句
                int i = st.executeUpdate(sql);
    
                System.out.println("Have inserted " + i + " row into table contacts.");
            } finally {
                JdbcUtils.free(rs, st, conn);
            }
        }
    
        static void read() throws SQLException {
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                // 2.建立连接
                conn = JdbcUtils.getConnection();
                // conn = JdbcUtilsSing.getInstance().getConnection();
                // 3.创建语句
                st = conn.createStatement();
    
                // 4.执行语句
                rs = st.executeQuery("select id, name, birthday, mobile  from contacts");
    
                // 5.处理结果
                while (rs.next()) {
                    System.out.println(rs.getObject("id") + "	"
                            + rs.getObject("name") + "	"
                            + rs.getObject("birthday") + "	"
                            + rs.getObject("mobile"));
                }
            } finally {
                JdbcUtils.free(rs, st, conn);
            }
        }
    
    }
  • 相关阅读:
    个人案例分析
    软工结对作业
    交点问题
    C语言复习
    【软件工程】提问回顾与个人总结
    【技术博客】Arxiv的新Paper获取和机翻
    【技术博客】动态面包屑导航
    对对碰 -- 软工结对编程博客
    交点计数 -- 软工个人项目作业
    面向对象的程序设计-模块四课程总结
  • 原文地址:https://www.cnblogs.com/juan-wang/p/4264667.html
Copyright © 2011-2022 走看看