search
HomeJavaJavaBaseSolution to garbled code when java inserts data into mysql

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools