標籤:註冊 manager 文字編碼 int height rom rman cat string
因為這是通用編碼,像中國通常使用的GBK、GB2312、Big5等只是針對中文而言,但是對其他文字就不適用了,為了使得這個問題的解決具有文字編碼通用性,所以我這裡設定了UTF8這個編碼。
編碼一致性涉及到的四個方面為:應用程式編碼、資料庫系統編碼、資料庫編碼、應用程式與資料庫系統的串連編碼。
1.mysql的設定,我的系統字元設定是拉丁文,也是夠夠的,發現之後要記得修改啊
2.java的設定,記得自己在剛開始的時候,設定的是:conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/xsxx","root","123456");
這樣在儲存的時候,就會出現中文儲存的亂碼
其實,僅僅需要加上“?characterEncoding=utf8”就能夠實現中文的儲存。
運行效果如下:
3.代碼:
/*****
* java串連mysql
* @author yanlong
*2017/5/7
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
//import java.util.Collection;
import java.sql.SQLException;
//import javax.sql.Statement;
public class TestJDBC {
public static void main(String[] args){
ResultSet rs=null;
Statement stmt=null;
Connection conn=null;
try{
/*載入並註冊mysql的JDBC驅動*/
Class.forName("com.mysql.jdbc.Driver");
/*建立到mysql的串連*/
//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/xsxx","root","123456");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/xsxx?characterEncoding=utf8","root","123456");
/*訪問資料庫,並執行sql語句*/
stmt=conn.createStatement();
/*添加記錄*/
System.out.println("添加記錄後:");
String sqll="insert into xs value (111111,‘小公舉‘,‘水利工程‘)";
stmt.executeUpdate(sqll);
rs=stmt.executeQuery("select * from xs");
while(rs.next()){
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("name"));
System.out.println(rs.getString("major"));
}
rs=stmt.executeQuery("select *from xs");
while(rs.next()){
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("name"));
System.out.println(rs.getString("major"));
}
}catch(ClassNotFoundException e){
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
java串連資料庫讀取資料出現亂碼