JDBC接続のMySQLデータベース操作のCRUD

JDBC接続のMySQLデータベース操作のCRUD

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

JDBCデータベース接続

JDBC(Javaデータベース接続、Javaデータベース接続)を使用したSQL文を実行するためのJava APIで、Java言語およびインタフェースで書かれたクラスのセットで構成され、リレーショナルデータベースの様々な、に統一されたアクセスを提供することができます。次のようにデータベースに接続します:
1.負荷ドライバ
2は、接続を作成
3.書き込みSQL
4.所与の文オブジェクト
、5. SQLを実行し、結果セット
6.結果が設定
7.閉じる資源を

ジャーパッケージに追加IDEA

ファイル- >新規作成- >パッケージ
ここに画像を挿入説明
のlibという名前の、OKをクリックしここに画像を挿入説明
たjarパッケージのlibに直接それをコピー&ペーストすることができますここに画像を挿入説明
libにパッケージを右クリックし、ライブラリとして追加]を選択します
ここに画像を挿入説明

データベースへの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();
            }
        }
    }
}

データベースのCRUD

 //查
    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();
        }
    }
}

おすすめ

転載: blog.csdn.net/JIAXY0705/article/details/93843325