MyEclipse 中文乱码问题

 

问题一:程序源文件java code 乱码问题

package demo;

import java.sql.*;

import demo.NoticeBean;

public class JDBCTest {
public static void main(String args[]) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName).newInstance();
String url = "jdbc:mysql://localhost:3306/JavaWeb?useUnicode=true&characterEncoding=gb2312";
connection = DriverManager.getConnection(url, "root", "root");
System.out.println("杩炴帴MySQL 5.0.18鎴愬姛!(椹卞姩3.1.12)");

statement = null;
resultSet = null;
String strSql = null;
NoticeBean bean = null;
String title = null;
String content = null;

// 瀵规暟鎹簱鐨勬彃鍏ユ搷浣�
try {
title = "鏍囬";
content = "鍐呭";

strSql = "INSERT INTO notice(title, content) VALUES('" + title
+ "','" + content + "')";
statement = connection.createStatement();
statement.executeUpdate(strSql);
System.out.println("鎻掑叆璇彞鎵ц鎴愬姛锛�" + strSql);
} catch (SQLException ex1) {
System.out.println("鎻掑叆澶辫触锛�");
}

// 瀵规暟鎹簱鐨勬煡璇㈡搷浣�
try {
strSql = "SELECT * FROM notice";
statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
resultSet = statement.executeQuery(strSql);

if (resultSet.next()) {
int id = resultSet.getInt("id");
title = resultSet.getString("title");
content = resultSet.getString("content");
if (resultSet.next()) {
bean = new NoticeBean(id, title, content);
}
System.out.println("notice琛ㄧ殑绗竴琛屾暟鎹槸锛�" + bean.getId() + " "
+ bean.getContent() + " " + bean.getTitle());
}
} catch (SQLException ex1) {
System.out.println("鏌ヨ澶辫触锛�");
}

// 瀵规暟鎹簱鐨勫垹闄ゆ搷浣�
try {
strSql = "DELETE FROM notice";
statement = connection.createStatement();
statement.executeUpdate(strSql);
System.out.println("notice琛ㄧ殑鎵�鏈夋暟鎹凡琚垹闄わ紒");
} catch (SQLException ex1) {
System.out.println("鍒犻櫎澶辫触锛�");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
resultSet = null;
}
if (statement != null) {
statement.close();
statement = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}

}
}

 

 

解决方法如下:
Window -> preference, 左边 General -> Content Types, 然后在右边上面的框中打开Text, 选中Java Source File (你看到下面的框中有个*.java 就对了), 然后在下面的“Default edcodng”文本框中输入“UTF-8”, 点“Update”,然后重启myeclipse。

 

 

 

 

问题二:MyEclipse 工程里的数据库脚本文件 **.sql 乱码问题

解决方法如下:
选中 **.sql文件 -> 右击-> Properties ->Resource, 将Texty file encoding 那一项选择Other 输入“UTF-8”, 点“OK”,然后重启myeclipse。

 

你可能感兴趣的:(MyEclipse 中文乱码问题)