JDBC的增删查改

jar包的导入

    1.下载mysql-connector-java-5.0.8-bin.jar包
         http://www.java2s.com/Code/Jar/m/Downloadmysqlconnectorjava508bingjar.htm
    2.在项目下创建名为lib的目录
    3.将下载的jar包加入目录文件下
    4.设置jar包的add aslibrary

1.数据库表格创建

    create table_user(
       id int primary key,
       username varchar(255),
       password varchar(255)
       )

2.增加记录

import java.sql.*;
public class Test2 {
        public static void main(String[] args)  {
            Connection conn=null;
            PreparedStatement pstmt=null;
            ResultSet rs=null;
            try {
                //1.加载驱动
                Class.forName("com.mysql.jdbc.Driver");
                //2.创建连接
                conn = DriverManager.getConnection("jdbc:mysql://localhost/ningda?", "root", "root");
                System.out.println("创建连接成功");
                //3.sql语句
                String sql="insert into tb_user value(2,'asd',456) ";
                //4.得到statement对象
                pstmt = conn.prepareStatement(sql);
                //5.执行sql
                int count = pstmt.executeUpdate();
                //6.处理结果集
                //7.关闭资源
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally {
               if (conn!=null)
               {
                   try {
                       conn.close();
                   } catch (SQLException e) {
                       e.printStackTrace();
                   }
               }
                if (rs!=null)
                {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (pstmt!=null)
                {
                    try {
                        pstmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

3.查找所有记录

import java.sql.*;
public class Test {
    public static void main(String[] args) throws Exception {
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.创建连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost/ningda?", "root", "root");
            System.out.println("创建连接成功");
            //3.sql语句
            String sql="select * from tb_user";
            //4.得到statement对象
            pstmt = conn.prepareStatement(sql);
            //5.执行sql
            rs = pstmt.executeQuery();
            //6.处理结果集
            while(rs.next())
            {
                System.out.println( rs.getInt(1));
                System.out.println(rs.getString("username"));
                System.out.println(rs.getString("password"));
            }
            //7.关闭资源
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
          finally {
            conn.close();
            pstmt.close();
            rs.close();
        }
    }
}

4. 修改记录

import java.sql.*;
public class Test3 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.创建连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost/ningda?", "root", "root");
            System.out.println("创建连接成功");
            //3.sql语句
            String sql = "update tb_user set password='789' where id=2";
            //4.得到statement对象
            pstmt = conn.prepareStatement(sql);
            //5.执行sql
            int count = pstmt.executeUpdate();
            //6.处理结果集
            //7.关闭资源
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

5. 删除记录

import java.sql.*;

/**
 * Created by 13781 on 2019/8/8.
 */
public class Test4 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.创建连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost/ningda?", "root", "root");
            System.out.println("创建连接成功");
            //3.sql语句
            String sql = "delete from tb_user where id=2";
            //4.得到statement对象
            pstmt = conn.prepareStatement(sql);
            //5.执行sql
            int count = pstmt.executeUpdate();
            //6.处理结果集
            //7.关闭资源
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值