The JDBC code implements CRUD

package com.jdbc.demo2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

/**
 * JDBC的CRUD的操作
 * @author lyt
 *
 */

public class JDBCDemo2 {
    
    @Test
    /**
     * 查询单条记录
     */
    public void demo5() {
        Connection conn = null;
        Statement stmt = null; 
        The ResultSet RS = null ;
         the try {
             // Register drive 
            the Class.forName ( "com.mysql.jdbc.Driver" );
             // get a connection 
            conn = DriverManager.getConnection ( "jdbc: mysql : /// test", "root "," the Oracle " );
             // create SQL statement object 
            stmt = conn.createStatement ();
             // write SQL 
            String SQL =" the SELECT * from the User " ;
             // execute SQL statements 
            rs = stmt.executeQuery (SQL);
             IF  (rs.next ()) {
                System.out.println (rs.tinted("id")+" "+rs.getString("username")+" "+rs.getString("password"));
            }
            
            
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn = null;
            }
            if(stmt!=null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                stmt = null;
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    //Auto-Generated Block the catch the TODO 
                    e.printStackTrace ();
                } 
                Conn = null ; 
            } 
        } 
        
    } 
    
    @Test 
    / ** 
     * query multiple records 
     * / 
    public  void demo4 () { 
        Connection Conn = null ; 
        the Statement stmt = null ; 
        the ResultSet RS = null ;
         the try {
             // Register drive 
            Class.forName ( "com.mysql.jdbc.Driver" );
             // get connected
            conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "oracle");
            //创建SQL语句对象
            stmt = conn.createStatement();
            //编写SQL
            String sql = "select * from user";
            //执行SQL语句
            rs = stmt.executeQuery(sql);
            while(rs.next()) {
                System.out.println(rs.getInt("id")+" "+rs.getString("username")+" "+rs.getString("password"));
            }
            
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn = null;
            }
            if(stmt!=null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                stmt = null;
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn = null;
            }
        }
        
    }
    
    @Test
    /**
     * Delete operation code for 
     * / 
    public  void Demo3 () { 
        Connection conn = null ; 
        the Statement stmt = null ;
         the try {
             // Register drive 
            the Class.forName ( "com.mysql.jdbc.Driver" );
             // get a connection 
            conn DriverManager.getConnection = ( "jdbc: MySQL: /// the Test", "root", "the Oracle" );
             // create SQL statement object 
            stmt = conn.createStatement ();
             // write the SQL statement 
            String sql = "delete from . 6 WHERE ID = User " ;
            // execute SQL statement 
            int NUM = stmt.executeUpdate (SQL);
             IF (NUM> 0 ) { 
                System.out.println ( "ID of user 6 removed successfully" ); 
            } 
            
        } the catch (Exception E) { 
            e.printStackTrace (); 
            
        } the finally {
             IF (Conn =! null ) {
                 the try { 
                    conn.Close (); 
                } the catch (SQLException E) {
                     // the TODO Auto-Generated Block the catch 
                    e.printStackTrace ();
                } 
            } 
            
            IF (stmt =! Null ) {
                 the try { 
                    stmt.close (); 
                } the catch (SQLException E) {
                     // the TODO Auto-Generated Block the catch 
                    e.printStackTrace (); 
                } 
            } 
            
                
                
            } 
            
        } 
    
    
    @Test 
    / ** 
     * Modify operation code implementation 
     * / 
    public  void demo2 () { 
        Connection Conn = null ; 
        the Statement stmt = null ;
         the try {
             // Register drive 
            the Class.forName ( "com.mysql.jdbc.Driver" );
             // get a connection 
            conn = DriverManager.getConnection ( "jdbc: mysql : /// test", "root", "oracle" );
             // perform operations
             // create SQL statement executed object 
            stmt = conn.createStatement ();
             // write SQL statements 
            String SQL = "Update the User the SET password = 'abc' the WHERE the above mentioned id = 4" ;
             // execute SQL 
            int = NUM stmt.executeUpdate (SQL);
             IF (NUM> 0 ) { 
                System.out.println ( "Change password successful!");
            }
        }catch(Exception e) {
            e.printStackTrace();
            
        }finally {
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn =null;
            }
            
            if(stmt!=null) {
                the try { 
                    stmt.close ();
                } The catch (SQLException E) {
                     // the TODO Auto-Generated Block the catch 
                    e.printStackTrace (); 
                } 
                Conn = null ; 
            } 
            
        } 
    } 
    
    @Test 
    / ** 
     * save operation code is implemented 
     * / 
    public  void the demo1 () { 
        Connection Conn = null ; 
        the Statement stmt = null ;
         the try {
             // Register drive; 
            the Class.forName ( "com.mysql.jdbc.Driver" );
             // get a connection 
            conn = DriverManager.getConnection ( "jdbc: MySQL: /// the Test", "root", "the Oracle" );
             // Create a statement object to execute SQL 
            stmt = conn.createStatement ();
             // writing SQL statements 
            String sql = "insert into user values (null, 'eee', '123', ' millet', 21)" ;
             // execute SQL 
            int NUM = stmt.executeUpdate (SQL);
             IF (NUM> 0 ) { 
                System.out.println ( "save user successfully !!" );
            }

        }catch(Exception e) {
            
            e.printStackTrace();
            
        }finally {
            //资源释放
            if(stmt!=null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                stmt = null;
            }
            
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn = null;
            }
            
            
            
        }
    }
    

}

As FIG profile

Guess you like

Origin www.cnblogs.com/lytuser/p/12227226.html