java處理資料庫的CRUD

來源:互聯網
上載者:User

標籤:

package com.lhy.jdbc.util;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * * 增刪改查 * @author hy *  */public class CRUD {    public static void main(String[] args) {        //create();        //read();        //update();        delete();        }    /**     * 查詢     */    static void read() {        Connection conn = null;        Statement stmt = null;        ResultSet rs = null;        try {            // 建立串連,JdbcUtil工具類請看我的另一篇部落格            conn = JdbcUtil.getConnection();            // 建立語句            stmt = conn.createStatement();            /**             *  執行語句,一般不建議直接寫select *,可讀性不好。             */            rs = stmt.executeQuery("select * from user");            // 處理結果            while (rs.next()) {                System.out.println(rs.getString("username") + "\t"                        + rs.getString("password"));            }        } catch (SQLException e) {            e.printStackTrace();        } finally {            JdbcUtil.close(rs);            JdbcUtil.close(stmt);            JdbcUtil.close(conn);        }    }    /**     * insert into插一條記錄     */    static void create(){                Connection conn = null;        Statement stmt = null;            try {            // 建立串連            conn = JdbcUtil.getConnection();            // 建立語句            stmt = conn.createStatement();                        String sql = "insert into user values(‘你好‘,‘147‘,1000)";                        //執行語句,傳回值是int  幾行被插入            stmt.executeUpdate(sql);            //int i = stmt.executeUpdate(sql);            //System.out.println("i="+i);        } catch (SQLException e) {            e.printStackTrace();        } finally {                        JdbcUtil.close(stmt);            JdbcUtil.close(conn);        }    }                    /**     * 更新     */        static void update(){                Connection conn = null;        Statement stmt = null;                try {            // 建立串連            conn = JdbcUtil.getConnection();            // 建立語句            stmt = conn.createStatement();            // 執行語句            String sql = "update user set money = money + 100";        //    int i = stmt.executeUpdate(sql);           // System.out.println("i="+i);        } catch (SQLException e) {            e.printStackTrace();        } finally {                        JdbcUtil.close(stmt);            JdbcUtil.close(conn);        }    }        static void delete(){                Connection conn = null;        Statement stmt = null;            try {            // 建立串連            conn = JdbcUtil.getConnection();            // 建立語句            stmt = conn.createStatement();            // 執行語句            String sql = "delete  from user where money <600";            int i = stmt.executeUpdate(sql);            System.out.println("i="+i);        } catch (SQLException e) {            e.printStackTrace();        } finally {                        JdbcUtil.close(stmt);            JdbcUtil.close(conn);        }    }}

 

java處理資料庫的CRUD

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.