Home  >  Article  >  Java  >  Solution to garbled code when java inserts data into mysql

Solution to garbled code when java inserts data into mysql

尚
Original
2019-11-28 10:52:492158browse

Solution to garbled code when java inserts data into mysql

Java Solution to garbled data and question marks when inserting data into mysql: (Recommended: java video tutorial)

Java Question marks when inserting data into the database For garbled code problems, first determine whether the encoding format of the database is correct. You can insert a statement in the MySQL database to see if the Chinese can be displayed normally. If it can be displayed normally, then it means that when Java connects to the database, the database is accessed without encoding.

Solution

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/dbname?characterEncoding=UTF-8";

Complete encoding

package com.music.test;

import java.sql.*;

public class DBMySQL {
    // JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/dbname?characterEncoding=UTF-8";

    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "***";
    static final String PASS = "***";
    private Connection conn = null;

    public DBMySQL() {
        try {
            // 注册 JDBC 驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }
    }

    public int insert(Album album) {
        int i = 0;
        String sql = "insert into album (album_name,singer,album_url) values(?,?,?)";
        try {

            PreparedStatement preStmt = conn.prepareStatement(sql);
            preStmt.setString(1, album.getAlbum_name());
            preStmt.setString(2, album.getSinger());// 或者:preStmt.setInt(1,值);
            preStmt.setString(3, album.getAlbum_url());// 或者:preStmt.setInt(1,值);

            i = preStmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return i;// 返回影响的行数,1为执行成功
    }
}

Add ?characterEncoding=UTF-8 after your database name, and the data can be successfully inserted.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Solution to garbled code when java inserts data into mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn