中軟培訓 day04 idea的安裝及jdbc的兩個寫法

中軟培訓 day4

jdbc:

1、加載驅動

2、創建連接

3、寫sql語句

4、得到statement對象

5、執行sql語句

6、處理結果集

7、關閉資源

方法一:

1、寫配置文件

#數據庫驅動
driver=com.mysql.jdbc.Driver
#連接數據庫的URL
url=jdbc:mysql://127.0.0.1:3306/nbufe?useSSL=true&characterEncoding=utf-8&user=root&password=***
#用戶名
username=root
#密碼
password=***

2、寫公共類

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;

    static{
        try {
            //讀取配置文件
            InputStream in = DBUtil.class.getResourceAsStream("db.properties");
            Properties properties = new Properties();
            //加載配置文件
            properties.load(in);
            //獲取配置文件中的數據
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("username");
            password = properties.getProperty("password");
            //加載數據庫鏈接驅動
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 獲取一個數據庫鏈接
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }
}

3、寫增刪查改

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

public class Test1 {
    public static void main(String[] args) throws SQLException {


    }

    public static void add() throws SQLException {
        String sql = "INSERT INTO tb_user (username,password) VALUES('yjy','123')";
        Connection conn = DBUtil.getConnection();
        conn.setAutoCommit(false);
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.executeUpdate();
        conn.commit();
        conn.close();
    }

    public static void delete() throws SQLException {
        String sql = "DELETE FROM tb_user WHERE tb_user.username = 'yjy'";
        Connection conn = DBUtil.getConnection();
        conn.setAutoCommit(false);
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.executeUpdate();
        conn.commit();
        conn.close();
    }


    public static void updata() throws SQLException {
        String sql = "UPDATE tb_user SET tb_user.username = 'yao'";
        Connection conn = DBUtil.getConnection();
        conn.setAutoCommit(false);
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.executeUpdate();
        conn.commit();
        conn.close();
    }

    public static void query() throws SQLException{
        String sql = "SELECT * FROM tb_user";
        Connection conn = DBUtil.getConnection();
        conn.setAutoCommit(false);
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        //執行查詢語句並返回結果集
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String username = resultSet.getString("username");
            String password = resultSet.getString("password");
            System.out.println(username + " " + password);
        }
        conn.commit();
        conn.close();
    }
}

方法二

import java.sql.*;

import static java.lang.Class.forName;

public class Test {
    public static void main(String[] args) {
    update();
    }

    public static void add(){
        ResultSet resultSet=null;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nbufe?useSSL=true&characterEncoding=utf-8&user=root&password=yaojieyu1999");
            System.out.println("創建數據庫成功1");
            String sql = "insert into tb_user (username,password) value('yjy','yjy123')";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                connection.close();
                preparedStatement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void query(){
        ResultSet resultSet=null;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try{
            //1、加載驅動
            Class.forName("com.mysql.jdbc.Driver");
            //2、創建連接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nbufe?useSSL=true&characterEncoding=utf-8&user=root&password=yaojieyu1999");
            System.out.println("創建數據庫成功2");
            //3、寫sql
            String sql="select * from tb_user";
            //4、得到statement對象
            preparedStatement = connection.prepareStatement(sql);
            //5、執行sql
            resultSet=preparedStatement.executeQuery();
            //6、處理結果集
            while (resultSet.next()){
                System.out.println(resultSet.getInt("id"));
                System.out.println(resultSet.getString("username"));
                System.out.println(resultSet.getString("password"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //7.關閉資源
            try{
                connection.close();
                preparedStatement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    public static void delete(){
        ResultSet resultSet=null;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try{
            //1、加載驅動
            Class.forName("com.mysql.jdbc.Driver");
            //2、創建連接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nbufe?useSSL=true&characterEncoding=utf-8&user=root&password=yaojieyu1999");
            System.out.println("創建數據庫成功3");
            //3、寫sql
            String sql="delete from tb_user where id=1";
            //4、得到statement對象
            preparedStatement = connection.prepareStatement(sql);
            //5、執行sql
            preparedStatement.executeUpdate();
            //6、處理結果集


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //7.關閉資源
            try{
                connection.close();
                preparedStatement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void update(){
        ResultSet resultSet=null;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try{
            //1、加載驅動
            Class.forName("com.mysql.jdbc.Driver");
            //2、創建連接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nbufe?useSSL=true&characterEncoding=utf-8&user=root&password=yaojieyu1999");
            System.out.println("創建數據庫成功1");
            //3、寫sql
            String sql="update tb_user set username = 'xiaoyao' where id=7";
            //4、得到statement對象
            preparedStatement = connection.prepareStatement(sql);
            //5、執行sql
            preparedStatement.executeUpdate();
            //6、處理結果集


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //7.關閉資源
            try{
                connection.close();
                preparedStatement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章