择善师的专栏

。。。

手写JAVA DB工具类(3)应用示例

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.DB.Utils.DBUtil;

public class TestB {

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    DBUtil db = new DBUtil();        
        //UpdateOne(db);    
        InserteOne(db);         
        Thread.sleep(1000);
        SelectOne(db);
        //Thread.sleep(1000);
        //SelectAll(db);
        DBUtil.closeAll();  

}

private static void UpdateOne(DBUtil db) throws Exception {
    // TODO Auto-generated method stub
    String sql = "UPDATE users  SET PASSWORD=? WHERE id=?";
    String args1="675835";
    String args2="10";
    int i = db.Update(sql,args1,args2);

    if (i > 0) {
        System.out.println("影响" + i + "行");
    } else if (i < 0) {
        System.out.println("错误");
    } else {
        System.out.println("影响" + i + "行");
    }
}

private static void InserteOne(DBUtil db) {

    String sql = "INSERT INTO  users VALUES (NULL,?,?,?,?)";
    String args1="TestB-HuJg";
    String args2="TestB-1232433";
    String args3="TestB-276575@QQ.COM";
    String args4="1806-01-01";
    int i;
    try {
        i = db.Update(sql,args1,args2,args3,args4);

    if (i > 0) {
        System.out.println("影响" + i + "行");
    } else if (i < 0) {
        System.out.println("错误");
    } else {
        System.out.println("影响" + i + "行");
    }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private static void SelectAll(DBUtil db) throws Exception, SQLException {
    String sql = "select * from users";
    ResultSet rs = db.Query(sql);
    List<User> list = new ArrayList<User>();
    while (rs.next()) {
        User u = new User();
        u.setId(rs.getInt(1));
        u.setName(rs.getString(2));
        u.setPassword(rs.getString(3));
        u.setEmail(rs.getString(4));
        u.setBirthday(rs.getDate(5));
        list.add(u);
    }

    for (User user : list) {
        System.out.println(user);
    }
}

private static void SelectOne(DBUtil db) {
    String sql = "select * from users where username=? and password=?";
    String str1="TestB-HuJg";
    String str2="TestB-1232433";
    ResultSet rs;
    try {
            rs = db.Query(sql,str1,str2);

        List<User> list = new ArrayList<User>();
        while (rs.next()) {
            User u = new User();
            u.setId(rs.getInt(1));
            u.setName(rs.getString(2));
            u.setPassword(rs.getString(3));
            u.setEmail(rs.getString(4));
            u.setBirthday(rs.getDate(5));
            list.add(u);
        }

        for (User user : list) {
            System.out.println(user);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

阅读更多
文章标签: java DB 
个人分类: java.DBUtil
想对作者说点什么? 我来说一句

手写JAVA DB工具类

2018年05月14日 614KB 下载

SpringMVC+mongodb应用实例

2016年08月16日 15.48MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭