자바 처리 데이터베이스 CRUD

8758 단어
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);
        }
    }
}

 
다음으로 전송:https://www.cnblogs.com/lihaoyang/p/4758689.html

좋은 웹페이지 즐겨찾기