码迷,mamicode.com
首页 > 数据库 > 详细

抽取JDBC工具类并增删改查

时间:2018-01-31 14:41:30      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:wan   one   runtime   select   .exe   font   out   url   res   

抽取工具类:

技术分享图片
package demo;
/*
 * 工具类
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBCUtils1 {

    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/mybase";
            String username = "root";
            String password = "xuyiqing";
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return conn;
    }

    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
View Code

 

数据准备:

技术分享图片
CREATE DATABASE mybase;
USE mybase;
CREATE TABLE users(
uid INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(64),
upassword VARCHAR(64)
);
INSERT INTO users (username,upassword) VALUES("zhangsan","123"),("lisi","456"),("wangwu","789");
SELECT * FROM users;
View Code

 

根据主键id查询:

技术分享图片
package demo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

public class TestUtils {

    @Test
    public void testFindUserById() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils1.getConnection();
            String sql = "select * from users where uid=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 2);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString(2) + "----" + rs.getString("upassword"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            JDBCUtils1.release(conn, pstmt, rs);
        }
    }
}
View Code

 

第二种抽取工具类方式:

配置文件(src目录下):db.properties

技术分享图片
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybase
username=root
password=xuyiqing
View Code

 

工具类:

技术分享图片
package demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;

public class JDBCUtils2 {
    
    public static String driver;
    public static String url;
    public static String username;
    public static String password;
    
    static{
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driver = bundle.getString("driver");
        url = bundle.getString("url");
        username = bundle.getString("username");
        password = bundle.getString("password");
    }
    
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return conn;
    }

    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
View Code

 

插入数据:

技术分享图片
package demo;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

public class TestUtils {
    @Test
    public void testAdd() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils2.getConnection();
            String sql = "insert into users values(null,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "xiaoming");
            pstmt.setString(2, "666");
            int row = pstmt.executeUpdate();
            if (row > 0) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils2.release(conn, pstmt, null);
        }
    }
}
View Code

 

 

第三种抽取工具类方式:

依然使用db.properties配置文件:

技术分享图片
package demo;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class JDBCUtils3 {
    public static String driver;
    public static String url;
    public static String username;
    public static String password;

    static {
        try {
            ClassLoader classLoader = JDBCUtils3.class.getClassLoader();
            InputStream is = classLoader.getResourceAsStream("db.properties");
            Properties props = new Properties();
            props.load(is);
            driver = props.getProperty("driver");
            url = props.getProperty("url");
            username = props.getProperty("username");
            password = props.getProperty("password");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return conn;
    }

    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
View Code

 

删除和修改数据:

技术分享图片
package demo;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

public class TestUtils {

    /*
     * 删除
     */
    @Test
    public void testDelete() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils3.getConnection();
            String sql = "delete from users where uid=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 3);
            int row = pstmt.executeUpdate();
            if (row > 0) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils3.release(conn, pstmt, null);
        }
    }

    /*
     * 修改
     */
    @Test
    public void testUpdate() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils3.getConnection();
            String sql = "update users set upassword=? where uid=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "123456789");
            pstmt.setInt(2, 2);
            int row = pstmt.executeUpdate();
            if (row > 0) {
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils3.release(conn, pstmt, null);
        }
    }
}
View Code

 

抽取JDBC工具类并增删改查

标签:wan   one   runtime   select   .exe   font   out   url   res   

原文地址:https://www.cnblogs.com/xuyiqing/p/8390807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!