Home > Database > Mysql Tutorial > mysql中文问题的解决_MySQL

mysql中文问题的解决_MySQL

WBOY
Release: 2016-06-01 13:53:29
Original
953 people have browsed it

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.util;

import java.io.UnsupportedEncodingException;
import java.sql.*;

/**
*
* @author swing
*/
public class DbUtility {

    private Connection conn = null;
    private ResultSet set = null;
    private Statement st = null;
    // 数据库连接使用的参数;
    private String DBUrl = "jdbc:mysql://localhost/acctest?useUnicode=true&characterEncoding=gbk";
   //这里使用编码就可以解决mysql中文问题,其他得不需要设置
    private String DBUser = "root";// 用户名
    private String DBPass = "root";// 密码
    private String DBDriver = "org.gjt.mm.mysql.Driver";// mysql驱动
    public DbUtility() {

    }
    //获取数据库连接
    public Connection getAConnection(String DBDriver, String DBUrl, String DBUser, String DBPass) {
        try {
            Class.forName(DBDriver).newInstance();
        } catch (Exception e) {
            System.out.println("没有安装Mysql Java Connector或类路径未设置正确:   " + e);
            return null;
        }
        try {
            conn = DriverManager.getConnection(DBUrl, DBUser, DBPass);
        } catch (SQLException e) {
            System.out.println("com.util.DbUtility.getAConnection:数据库错误:   " + e);
            return null;
        }
        return conn;
    }
    //insert or update table
    public boolean execute(String sql) throws UnsupportedEncodingException {
        conn = getAConnection(DBDriver, DBUrl, DBUser, DBPass);
        if (conn == null) {
            System.out.println("连接数据库失败");
            return false;
        }
        try {
            st = conn.createStatement();
            st.execute(sql);
            st.close();
            conn.close();
            conn = null;

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template