安卓的工具类

package com.skiers.demo_learn.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 基于Android Gson Assets
 */

public class JsonUtil {

    public static String getJson(String fileName, Context context) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            InputStream is = context.getAssets().open(fileName);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();
    }

    public static <T> T fromJson(TypeToken<?> typeToken, String fileName, Context context) {

        InputStream is = null;
        try {
            is = context.getAssets().open(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));

        Gson gson = new Gson();
        return gson.fromJson(bufferedReader, typeToken.getType());
    }

    public static <T> T fromJson(Class<T> cls, String fileName, Context context) {

        InputStream is = null;
        try {
            is = context.getAssets().open(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));

        Gson gson = new Gson();
        return gson.fromJson(bufferedReader, cls);
    }

    public static <T> T fromJson(TypeToken<?> typeToken, String json) {
        Gson gson = new Gson();
        return gson.fromJson(json, typeToken.getType());
    }

    public static <T> T fromJson(Class<T> cls, String json) {
        Gson gson = new Gson();
        return gson.fromJson(json, cls);
    }

    public static <T, V extends Object> String toJson(V v) {
        Gson gson = new Gson();
        return gson.toJson(v);
    }
}

package com.skiers.demo_learn.utils;

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

public class JdbcUtil {

    private static String driverClassName = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://118.31.124.77:3306/server10008?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
    private static String username = "server10008";
    private static String password = "X3pQMFjIfLZsdmi";

    private static Connection connection = null;

    static {
        try {
            Class.forName(driverClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {

            return DriverManager.getConnection(url, username, password);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

    public static Statement getStatement() {
        Statement statement = null;
        connection = getConnection();

        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return statement;
    }

    public static PreparedStatement getStatement(String sql) {
        PreparedStatement statement = null;

        connection = getConnection();
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return statement;
    }


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



package com.skiers.demo_learn.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class Intents {

    private static Intents intents;

    public static Intents getIntents() {
        if (intents == null)
            intents = new Intents();
        return intents;
    }

    // context this, cs跳转对象 bundle 传递参数
    public void intent(Context context, Class<?> cs, Bundle bundle) {
        Intent i = new Intent(context, cs);
        if (bundle != null)
            i.putExtras(bundle);
        context.startActivity(i);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值