jdbc

简单增删改查
package Practice170829;

import org.junit.Test;

import java.sql.*;

/**
 * Created by 8102 on 2017/8/29.
 */
public class JDBC01 {
    @Test
    public void queryAll(){ //查询所有
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            // 1 加载JDBC驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 2 连接数据库
            conn = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/my_test?useUnicode=true&character=utf-8",
                    "root","root");
            // 3 准备执行的sql对象
            String sql = "SELECT * FROM users";
            pstmt = conn.prepareStatement(sql);
            // 4 执行sql语句,并显示结果
            rs = pstmt.executeQuery();
            while (rs.next()){
                System.out.println(rs.getString("user_name")+":"+rs.getInt("user_age"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5 关闭 先开的后关,后开的先关
            try {
                if(rs != null){
                    rs.close();
                }
                if(pstmt != null){
                    pstmt.close();
                }
                if(conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void queryByConditions(){    //条件查询
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/my_test?useUnicode=true&character=utf-8",
                    "root","root");
            String sql = "SELECT * FROM users WHERE user_age>25;";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                System.out.println(rs.getString("user_name")+":"+rs.getInt("user_age"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs != null){
                    rs.close();
                }
                if(pstmt != null){
                    pstmt.close();
                }
                if(conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void del(){  //删除
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/my_test?useUnicode=true&character=utf-8",
                    "root","root");
            String sql = "DELETE FROM users WHERE user_name=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,"wangqing");
            int flag = pstmt.executeUpdate();
            if(flag>0){
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(pstmt != null){
                    pstmt.close();
                }
                if(conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void update(){  //修改
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/my_test?useUnicode=true&character=utf-8",
                    "root","root");
            String sql = "UPDATE users SET user_age=? WHERE user_id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,0);
            pstmt.setInt(2,2);
            int flag = pstmt.executeUpdate();
            if(flag>0){
                System.out.println("修改成功!");
            } else {
                System.out.println("修改失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(pstmt != null){
                    pstmt.close();
                }
                if(conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void add(){  //增加
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/my_test?useUnicode=true&character=utf-8",
                    "root","root");
            String sql = "INSERT INTO login(username,pwd) VALUES(?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,"");
            pstmt.setString(2,"514");
            int flag = pstmt.executeUpdate();
            if(flag>0){
                System.out.println("增加成功!");
            } else {
                System.out.println("增加失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(pstmt != null){
                    pstmt.close();
                }
                if(conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

存储过程和函数


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值