2024.5.17

第五十一天

所花时间:2小时

代码量:200+

博客量:1

了解到的知识点:

团队大作业1

数据库连接

package com.example.share.utils;

import android.util.Log;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class DbOpenHelper {
    private static final String CLS="com.mysql.jdbc.Driver";
    private static final String URL="jdbc:mysql://192.168.43.157:3306/tuandui?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8";
    private static final String USER="root";
    private static final String PASSWORD="ADGJL12345syl";


    public static Connection connection; //连接对象
    public static Statement statement;  //命令集
    public static PreparedStatement preparedStatement;  //预编译命令集v
    public static ResultSet resultSet;  //结果集
    //获取连接的方法
    public static void getConnection(){
        try{
            Class.forName(CLS);
            connection = DriverManager.getConnection(URL,USER,PASSWORD);

        }catch (Exception e){
            Log.e("DB_CONNECTION", "Connection Error: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void closeAll(){
        try{
            if(connection != null){
                connection.close();
                connection = null;
            }
            if(statement != null){
                statement.close();
                statement = null;
            }
            if(preparedStatement != null){
                preparedStatement.close();
                preparedStatement = null;
            }
            if(resultSet != null){
                resultSet.close();
                resultSet = null;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

 

posted @ 2024-05-26 23:50  cvjj  阅读(6)  评论(0)    收藏  举报