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);
}
}