在线急等:jsp+mysql的中文问题,帮忙看一下啊 刚才看了一下mysql数据库里,显示的也是乱码,不是中文英文和数字可以正常显示 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /* * 创建日期 2004-3-14 * * 更改所生成文件模板为 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释 */import java.sql.*;import java.util.*;import java.io.*;/** * @author Administrator * * 更改所生成类型注释的模板为 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释 */public class MysqlTest { static String sqlstmt1 = "insert into Greetings values ('hello,world,山西用友')"; private static byte[] dbByte; public static void main(String[] args) { try { Connection conn = getConnection(); Statement stat = conn.createStatement(); // stat.execute("use pubs"); stat.execute("drop table greetings"); stat.execute("create table Greetings (name char(30))"); dbByte = sqlstmt1.getBytes(); String sqlstmt = new String (dbByte,"ISO-8859-1");// stat.execute("insert into Greetings values ('hello,world,"+sqlstmt+"')"); stat.execute(sqlstmt); ResultSet result = stat.executeQuery("select * from Greetings"); result.next(); String dbstr2 =new String (); dbstr2 = result.getString(1); //String dbByte = new String(); dbByte = dbstr2.getBytes("ISO-8859-1"); String dbStr1 = new String(dbByte); System.out.println(dbStr1); result.close(); stat.close(); conn.close(); } catch (SQLException ex) { while (ex != null) { ex.printStackTrace(); ex = ex.getNextException(); } } catch (IOException ex) { ex.printStackTrace(); } } public static Connection getConnection() throws SQLException , IOException { Properties props = new Properties(); FileInputStream in = new FileInputStream("database.properties"); props.load(in); in.close(); String drivers = props.getProperty("jdbc.drivers"); if (drivers != null) System.setProperty("jdbc.drivers",drivers); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); return DriverManager.getConnection(url,username,password); }} 你插入数据库时,先把要插入的汉字字符串经过如下tran方法转码:public String tran(String code) { String tmp= new String(code.getBytes(),"ISO-8859-1"); return tmp; }把tmp再插入数据库 "jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=GBK"; "jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=GBK";爱笛生的这个方法不行呢不过还是谢谢你啊我的问题依然有待解决啊 SD_wxb()的方法你试试吧,应该可以解决你的问题,乱码是你在插入的时候没有转换字符集导致的。 2个表相同的记录更新以及不同的记录插入的问题 MYSQL中查询字段中只包含字母的数据 求一sql语句 read committed 会不会造成第二类丢失更新 求一条mysql update语句(急。。。)分不够再加! limit问题 Mysql不能创建新数据库及新表 请问mysql的这个错误是怎么回事?? 求一条sql语句 msyql使用 default current_timestamp报错 MYSQL权限问题! mysql数据导出问题(在线)
* 创建日期 2004-3-14
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
import java.sql.*;
import java.util.*;
import java.io.*;
/**
* @author Administrator
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class MysqlTest { static String sqlstmt1 = "insert into Greetings values ('hello,world,山西用友')";
private static byte[] dbByte;
public static void main(String[] args) {
try
{
Connection conn = getConnection();
Statement stat = conn.createStatement();
// stat.execute("use pubs");
stat.execute("drop table greetings");
stat.execute("create table Greetings (name char(30))");
dbByte = sqlstmt1.getBytes();
String sqlstmt = new String (dbByte,"ISO-8859-1");
// stat.execute("insert into Greetings values ('hello,world,"+sqlstmt+"')");
stat.execute(sqlstmt);
ResultSet result = stat.executeQuery("select * from Greetings");
result.next();
String dbstr2 =new String ();
dbstr2 = result.getString(1);
//String dbByte = new String();
dbByte = dbstr2.getBytes("ISO-8859-1");
String dbStr1 = new String(dbByte);
System.out.println(dbStr1);
result.close();
stat.close();
conn.close();
}
catch (SQLException ex)
{
while (ex != null)
{
ex.printStackTrace();
ex = ex.getNextException();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public static Connection getConnection() throws SQLException , IOException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null)
System.setProperty("jdbc.drivers",drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
return DriverManager.getConnection(url,username,password);
}
}
{
String tmp= new String(code.getBytes(),"ISO-8859-1");
return tmp;
}
把tmp再插入数据库
乱码是你在插入的时候没有转换字符集导致的。