JDBC连接MySQL数据库操作增删改查

JDBC连接MySQL数据库操作增删改查

环境:jdk1.8、MySQL5.5、IDEA2018.2

JDBC连接数据库

JDBC(Java数据库连接,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成.连接数据库步骤如下:
1.加载驱动
2.创建连接
3.写sql
4.得到statement对象
5.执行SQL,得到结果集
6.处理结果集
7.关闭资源

IDEA中添加jar包

File->New->Package
JDBC连接MySQL数据库操作增删改查_第1张图片
命名为lib,点击OKJDBC连接MySQL数据库操作增删改查_第2张图片
将jar直接复制粘贴到lib包中即可JDBC连接MySQL数据库操作增删改查_第3张图片
鼠标右击lib包,选择Add as Library
JDBC连接MySQL数据库操作增删改查_第4张图片

JDBC创建连接数据库及关闭资源

package com.IDEA.utill;
import java.sql.*;

public class DBUtill {
public static Connection getConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.创建连接
            Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/idea?//(idea为数据库名)
            +useSSL=true&characterEncoding=utf-8&user=账号&password=密码");//账号、密码为Mysql账号密码
            System.out.println("创建连接成功");
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

数据库增删改查

 //查
    public class Select {
    
        public static void main(String[] args) {
            ResultSet resultset=null;
            PreparedStatement statement=null;
            Connection connection=null;
    
            try {
                // 1.加载驱动
                //2.创建连接
                connection=DBUtill.getConnection();
                //System.out.println("创建连接成功");
                //3.写SQL语句
                String sql="select * from userinfo";
                //4.得到statement对象
              statement = connection.prepareStatement(sql);
              //5.执行SQL语句,得到结果集
               resultset = statement.executeQuery();
               //6.处理结果集
                while (resultset.next())
                {
                    System.out.println(resultset.getInt(1));
                    System.out.println(resultset.getString(2));
                    System.out.println(resultset.getString(3));
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                //7.关闭资源
                DBUtill.close(resultset,statement,connection);
                } 
        }
    }
//删
    public class Detele {
        public static void main(String[] args){
            Connection connection=null;
            PreparedStatement statement=null;
            ResultSet resultSet=null;
    
            //4.得到statement对象
            try {
                //1.创建连接
                connection= DBUtill.getConnection();
                //3.写SQL语句
                String sql="delete from userinfo where id=?";
                statement=connection.prepareStatement(sql);
                statement.setInt(1,2);
                //4.执行SQL语句
                statement.executeUpdate();
                System.out.println("删除成功");
            } catch (SQLException e) {
                System.out.println("删除失败");
                e.printStackTrace();
            }finally {
                DBUtill.close(resultSet,statement,connection);
            }
        }
    }
//插    
public class Insert {
    public static void main(String[] args){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultset=null;
        try {
            connection = DBUtill.getConnection();
            //3.写SQL语句
            String sql="insert into userinfo (username,password) values(?,?)";
            //4.得到statement对象
            statement=connection.prepareStatement(sql);
            statement.setString(1,"王五");
            statement.setString(2,"456");
            //5.执行SQL语句
            statement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtill.close(resultset,statement,connection);
        }
    }
}
//改
public class Update {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultset = null;


        try {
            connection= DBUtill.getConnection();
            String sql="update userinfo set username='赵四' where username='张三' ";
            statement=connection.prepareStatement(sql);
            //statement.setString(赵四,2);
            statement.executeUpdate();
            System.out.println("修改成功");
        } catch (SQLException e) {
            System.out.println("修改失败");
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(JDBC连接MySQL数据库操作增删改查)