jdbc 封装 删除 更新等方法

package com.myivtec.type4.test;

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

import com.myivtec.type4.util.DB;

import junit.framework.TestCase;

public class JdbcTest extends TestCase {
    private Connection con ;
    private Statement stmt;
    private ResultSet rs;
    public void testDQL() {
        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            String username = "段岩";
            String sql = "select * from user where username='"+username+"'";
            rs = stmt.executeQuery(sql);
            rs.next();
            System.out.println(rs.getInt(1));
            System.out.println(rs.getString(2));
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                stmt.close();
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public void testDelete() {
        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            String sql = "delete from user where Id="+10;
            int i = stmt.executeUpdate(sql);
            System.out.println(i);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                stmt.close();
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public void testUpdate() {
        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            String username = "王五";
            String password = "123";
            int age = 100;
            String sql = "update user set username='"+username+"',password='"+password+"',age= "+age+" where Id=2";
            int i = stmt.executeUpdate(sql);
            System.out.println(i);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                stmt.close();
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public void testInsert() {
        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            String username = "包慧";
            String password = "123";
            int age = 23;
            String sex = "女";
            Timestamp time = new Timestamp(new java.util.Date().getTime());
            String sql = "insert into user values(null,'"+username+"','"+password+"',"+age+",'"+sex+"',now())";
            int i = stmt.executeUpdate(sql);
            System.out.println(i);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                stmt.close();
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
}

 

posted on 2013-12-03 18:58  weiguoyuan  阅读(209)  评论(0)    收藏  举报

导航