标签归档:java

gitbash 使用 .bash_profile

示例:添加别名

cd ~
touch .bashrc
touch .bash_profile
echo 'if [ -f ~/.bashrc ]; then . ~/.bashrc; fi' > .bash_profile
echo 'alias java="java -Dfile.encoding=UTF-8"' > .bashrc # 在windows下java输出乱码的解决办法
. ~/.bashrc

java 连接 mysql 并使用 prepareStatement 查询数据库

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

import java.sql.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {

        Connection conn = null;
        User user = null;
        Gson gson = new Gson();

        try {
            # JDK6 之前的版本需要加载驱动
            # new com.mysql.jdbc.Driver();

            # 连接数据库
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname?" +
                    "useSSL=false&useUnicode=true&characterEncoding=UTF8", "root", "root");

            # 使用 prepareStatement 查询数据库
            PreparedStatement preStatement = conn.prepareStatement("select * from tables");
            ResultSet res = preStatement.executeQuery();

            List<User> users = new ArrayList<User>();
            while (res.next()) {
                user = new User();
                user.setId(Integer.parseInt(res.getString("id")));
                user.setUser(res.getString("user"));
                users.add(user);
            }
            # 导出为 json
            String strJson = gson.toJson(users);
            System.out.println(strJson);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

继续阅读

使用 Gson 序列化 和 反序列化 json

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

public class Main {

    public static void main(String[] args) {

        User user = new User();
        user.setId(1);
        user.setUser("admin");

        # 实例化 Gson
        Gson gson = new Gson();
        # 序列化 json
        String jsonUser = gson.toJson(user);
        System.out.println(jsonUser);
        # 反序列化 json
        User user1 = gson.fromJson(jsonUser, User.class);
        System.out.println(user1);

        System.out.println("==============================");

        # 列表,使用 List
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
        users.add(user);
        }
        String jsonUsers = gson.toJson(users);
        System.out.println(jsonUsers);

        List<User> users1 = gson.fromJson(jsonUsers, new TypeToken<List<User>>() {
        }.getType());
        for (User _user : users1) {
        System.out.println(_user);
        }

        System.out.println("==============================");

        # 多属性键值对,使用 Map
        Map<String, String> ips = new HashMap<>();
        for (int i = 0; i < 9; i++) {
            ips.put(i + "", "192.168.1.10" + i);
        }
        String jsonIps = gson.toJson(ips);
        System.out.println(jsonIps);

        Map<String, String> ips1 = gson.fromJson(jsonIps, new TypeToken<Map<String, String>>() {
        }.getType());
        Set<String> set = ips1.keySet();
        for (String _string : set) {
        System.out.println(_string + ":" + ips1.get(_string));
        }
    }
}

继续阅读