?? chinacharconversionframe.java
字號:
package chinacharconversion;import com.borland.jbcl.layout.*;import java.awt.*;import java.awt.event.*;import java.lang.*;import java.lang.Object.*;import java.sql.*;import java.util.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.table.*;/** * Title: 中文字符轉換程序 * Description: 教學示范 * Copyright: Copyright (c) 2003 * Company: 北京師范大學計算機系 * @author 孫一林 * @version 1.0 */public class ChinaCharConversionFrame extends JFrame { private JPanel contentPane; private XYLayout xYLayout1 = new XYLayout(); // 構造XYLayout布局管理器 // 創建顯示信息的組件 private Label label1 = new Label(); private Label label2 = new Label(); private Label label3 = new Label(); private Label label4 = new Label(); private Label label5 = new Label(); private TextField stunumField = new TextField(); private TextField nameField = new TextField(); private TextField ageField = new TextField(); private TextField sexField = new TextField(); private TextField departmentField = new TextField(); private Button addButton = new Button(); private Button displayButton = new Button(); Vector vector; // 聲明一個向量對象 String title[] = {"學號","姓名","年齡","性別","系名"}; // 二維表列名 Connection connection = null; // 聲明Connection接口對象connection ResultSet rs = null; // 定義數據庫查詢的結果集 Statement statement = null; // 定義查詢數據庫的Statement對象 AbstractTableModel tm; // 聲明一個AbstractTableModel類對象tm String name,sex,department; // 定義姓名、性別、系名文本框對應的字符串變量以進行轉換 public ChinaCharConversionFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); // 初始化組件 label1.setText("學號"); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(432, 434)); this.setTitle("中文字符轉換"); label2.setText("姓名"); label3.setText("年齡"); label4.setText("性別"); label5.setText("系名"); addButton.setLabel("添加記錄"); addButton.addActionListener(new java.awt.event.ActionListener() {// 設置addButton按鈕監聽器 public void actionPerformed(ActionEvent e) { addButton_actionPerformed(e); } }); displayButton.setLabel("顯示所有記錄"); displayButton.addActionListener(new java.awt.event.ActionListener() {// 設置displayButton按鈕監聽器 public void actionPerformed(ActionEvent e) { displayButton_actionPerformed(e); } }); contentPane.add(displayButton, new XYConstraints(226, 325, -1, 34)); contentPane.add(label1, new XYConstraints(39, 256, 41, 26)); contentPane.add(label2, new XYConstraints(113, 256, 41, 26)); contentPane.add(label3, new XYConstraints(187, 256, 41, 26)); contentPane.add(label4, new XYConstraints(260, 256, 41, 26)); contentPane.add(label5, new XYConstraints(334, 256, 41, 26)); contentPane.add(stunumField, new XYConstraints(29, 286, 59, 26)); contentPane.add(nameField, new XYConstraints(104, 286, 59, 26)); contentPane.add(ageField, new XYConstraints(180, 286, 59, 26)); contentPane.add(sexField, new XYConstraints(255, 286, 59, 26)); contentPane.add(departmentField, new XYConstraints(330, 286, 59, 26)); contentPane.add(addButton, new XYConstraints(86, 325, 107, 34)); createtable(); // 在初始化函數中調用createtable()函數顯示表格 } void createtable() { // 定義createtable()函數 JTable table; // 聲明一個JTable類對象table JScrollPane scroll; // 聲明一個滾動杠對象scroll vector = new Vector(); // 創建向量對象 tm = new AbstractTableModel() { // 創建AbstractTableModel類對象tm public int getColumnCount() { // 取得表格列數 return title.length; } public int getRowCount() { // 取得表格行數 return vector.size(); } public Object getValueAt(int row, int column) { // 取得單元格中的屬性值 if(!vector.isEmpty()) { return ((Vector)vector.elementAt(row)).elementAt(column); } else { return null; } } public void setValueAt(Object value, int row, int column) { // 數據模型不可編輯,該方法設置為空 } public String getColumnName(int column) { // 取得表格列名 return title[column]; } public Class getColumnClass(int c) { // 取得列所屬對象類 return getValueAt(0,c).getClass(); } public boolean isCellEditable(int row, int column) {// 設置單元格不可編輯,為缺省實現 return false; } }; table = new JTable(tm); // 生成自己的數據模型 table.setToolTipText("Display Query Result"); // 設置幫助提示 table.setAutoResizeMode(table.AUTO_RESIZE_OFF); // 設置表格調整尺寸模式 table.setCellSelectionEnabled(false); // 設置單元格選擇方式 table.setShowHorizontalLines(true); // 設置是否顯示單元格之間的分割線 table.setShowVerticalLines(true); scroll = new JScrollPane(table); // 給表格加上滾動杠 scroll.setBounds(6,20,540,250); contentPane.add(scroll,new XYConstraints(16, 20, 395, 200)); } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public static String toTureAsciiStr(String str){ // 定義將中英文字串轉換成純英文字串共用靜態方法 StringBuffer sb=new StringBuffer(); byte[] bt=str.getBytes(); // 把要轉換的字符串轉換為字節形式 for(int i=0;i<bt.length;i++){ if(bt[i]<0){ // 判斷是否為漢字,如是則去高位1 sb.append((char)(bt[i]&(0x7f))); } else{ // 是英文字符補0作記錄 sb.append((char)0); sb.append((char)bt[i]); } } return sb.toString(); // 返回轉換后的英文字符串 } public static String unToTrueAsciiStr(String str){// 定義將經轉換的字串還原方法 byte[] bt=str.getBytes() ; int i; int l=0; int length=bt.length; int j=0; for(i=0;i<length;i++){ // 判斷有幾個英文字符以去除Byte 0 if(bt[i]==0){ l++; } } byte[] bt2=new byte[length-l]; // 定義返回的字節數組 for(i=0;i<length;i++){ if(bt[i]==0){ // 是英文字符去掉0 i++; bt2[j]=bt[i]; } else{ // 是漢字補上高位1 bt2[j]=(byte)(bt[i]|0x80); } j++; } String tt=new String(bt2); return tt; // 返回還原后的字符串 } void addButton_actionPerformed(ActionEvent e) { try { // 調用toTureAsciiStr()函數將中英文信息轉換為英文信息 name=toTureAsciiStr(nameField.getText()); sex=toTureAsciiStr(sexField.getText()); department=toTureAsciiStr(departmentField.getText()); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 實例化JDBC-ODBC橋的驅動 String url = "jdbc:odbc:TestDbStu"; // 設置連接字符串 connection = DriverManager.getConnection(url); // 連接數據庫 statement = connection.createStatement(); String sql = "insert into studentbase (學號,姓名,年齡,性別,系名) values (" +Integer.parseInt(stunumField.getText()) +", '" +name+"',"+Integer.parseInt(ageField.getText())+",'"+sex+"',"+"'"+department+ "')"; statement.executeUpdate(sql); // 執行增加新的數據記錄語句 // 清空文本框信息 stunumField.setText(""); nameField.setText(""); ageField.setText(""); sexField.setText(""); departmentField.setText(""); } catch(SQLException ex){ // 捕捉異常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException(); } } catch(Exception ex ) { ex.printStackTrace(); } finally { try { if(statement != null) { statement.close(); // 關閉Statement接口實例 } if(connection != null) { connection.close(); // 關閉Connection接口實例 } } catch (SQLException ex) { System.out.println("\nERROR:----- SQLException -----\n"); System.out.println("Message: " + ex.getMessage( )); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); } } } void displayButton_actionPerformed(ActionEvent e) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 實例化JDBC-ODBC橋的驅動 String url = "jdbc:odbc:TestDbStu"; // 設置連接字符串 connection = DriverManager.getConnection(url); // 連接數據庫 // 創建Statement接口對象 statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql = "select * from studentbase"; // 執行statement對象的executeQuery方法,查詢studentbase表,將查詢結果放入rs中 ResultSet rs = statement.executeQuery(sql); vector.removeAllElements(); // 初始化向量對象 tm.fireTableStructureChanged(); // 更新表格內容 while(rs.next()) { Vector rec_vector = new Vector(); // 從結果集中取數據放入向量rec_vector中 rec_vector.addElement(String.valueOf(rs.getInt("學號"))); rec_vector.addElement(unToTrueAsciiStr(rs.getString("姓名"))); // 調用unToTrueAsciiStr函數還原為中文信息再添加 rec_vector.addElement(String.valueOf(rs.getInt("年齡"))); rec_vector.addElement(unToTrueAsciiStr(rs.getString("性別"))); // 調用unToTrueAsciiStr函數還原為中文信息再添加 rec_vector.addElement(unToTrueAsciiStr(rs.getString("系名"))); // 調用unToTrueAsciiStr函數還原為中文信息再添加 vector.addElement(rec_vector); // 向量rec_vector加入向量vector中 } tm.fireTableStructureChanged(); // 更新表格,顯示向量vector的內容 rs.close() ; // 關閉結果集 } catch(SQLException ex){ // 捕捉異常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException(); } } catch(Exception ex ) { ex.printStackTrace(); } finally { try { if(statement != null) { statement.close(); // 關閉Statement接口實例 } if(connection != null) { connection.close(); // 關閉Connection接口實例 } } catch (SQLException ex) { System.out.println("\nERROR:----- SQLException -----\n"); System.out.println("Message: " + ex.getMessage( )); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -