基础问题解决示例

idea控制台tomcat中文乱码的处理方法

解决办法如下:

(一)在idea的bin目录中找到idea64.exe.vmoptions或idea.exe.vmoptions根据系统自行选择修改

img

添加

-Dfile.encoding=UTF-8

关闭idea再重新启动。

(二)在往tomcat中部署项目时在VM options中添加 -Dfile.encoding=UTF-8

img

启动tomcat,这样控制台中文乱码就解决了.

JavaWeb DAO层(data access Object)基础代码 BaseDao.java

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms_copy?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=123456

BaseDao.java

//操作数据库的公共类
public class BaseDao {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    //静态代码块,类加载的时候就初始化了
    static {
        //通过类加载器读取响应的资源
        Properties properties = new Properties();
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");

        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }

    //获取数据库的链接
    public static Connection getconnection() {
        Connection connection = null;

        try {
            //加载驱动
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return connection;
    }

    //编写查询公共类方法
    public static ResultSet execute(Connection connection, String sql, PreparedStatement pstm,
                                    Object[] params, ResultSet rs) throws SQLException {

        pstm = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            pstm.setObject(i+1,params[i]);
        }

        rs = pstm.executeQuery();

        return rs;
    }

    //编写增删改公共类方法
    public static int execute(Connection connection, String sql, PreparedStatement pstm,
                                    Object[] params) throws SQLException {

        pstm = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            pstm.setObject(i+1,params[i]);
        }

        int updateRows = pstm.executeUpdate();
        return updateRows;
    }


    //释放资源
    public static boolean closeReource(Connection connection, PreparedStatement pstm, ResultSet rs) {
        boolean flag = true;

        if (connection != null) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        if (pstm != null) {
            try {
                pstm.close();
                pstm = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }


        if (rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        return flag;
    }
}

Maven项目报错:java.lang.ExceptionInInitializerError

Maven由于它的约定大于配置,我们之后可能遇到我们自己写的配置文件,无法被导出或者生效问题,解决方案:在build中配置resources,来防止我们资源导出失败。

解决方案:将该配置写在pom.xml中
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

解决Java连接阿里云服务器redis 提示连接被拒绝问题

https://my.oschina.net/AmosWang/blog/1475187

通过浏览器的开发者工具, 查看我们保存在浏览器上的密码


国内下在 vscode 十分缓慢, 是因为服务器地址是国外的, 这时应改为国内镜像下载.

// 先去官网找到下载地址
https://az764295.vo.msecnd.net/stable/f4af3cbf5a99787542e2a30fe1fd37cd644cc31f/VSCodeSetup-x64-1.62.1.exe

// 将国外的地址 改为 国内的镜像版本
https://vscode.cdn.azure.cn/stable/f4af3cbf5a99787542e2a30fe1fd37cd644cc31f/VSCodeSetup-x64-1.62.1.exe

az764295.vo.msecnd.net/stable/f4af3cbf5a99787542e2a30fe1fd37cd644cc31f/VSCodeSetup-x64-1.62.1.exe

vscode.cdn.azure.cn/stable/f4af3cbf5a99787542e2a30fe1fd37cd644cc31f/VSCodeSetup-x64-1.62.1.exe

posted @ 2021-04-17 16:25  HonkSun  阅读(84)  评论(0)    收藏  举报