?? id3panel.java
字號:
package id3;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Text;
import javax.swing.JOptionPane;
import java.sql.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
public class Id3Panel {
private Shell sShell = null; // @jve:decl-index=0:visual-constraint="3,9"
private Button Read = null;
private Button Show = null;
private Button Result = null;
private Text ResultText = null;
public Id3Panel() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/* Before this is run, be sure to set up the launch configuration (Arguments->VM Arguments)
* for the correct SWT library path in order to run with the SWT dlls.
* The dlls are located in the SWT plugin jar.
* For example, on Windows the Eclipse SWT 3.1 plugin jar is:
* installation_directory\plugins\org.eclipse.swt.win32_3.1.0.jar
*/
Display display = Display.getDefault();
Id3Panel thisClass = new Id3Panel();
thisClass.createSShell();
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* This method initializes sShell
*/
private void createSShell() {
sShell = new Shell();
sShell.setText("Id3");
sShell.setSize(new Point(1112, 641));
sShell.setLayout(null);
Read = new Button(sShell, SWT.NONE);
Read.setText("讀入數(shù)據(jù)");
Read.setSize(new Point(94, 32));
Read.setLocation(new Point(65, 50));
Show = new Button(sShell, SWT.NONE);
Show.setText("顯示數(shù)據(jù)");
Show.setSize(new Point(94, 32));
Show.setLocation(new Point(65, 113));
Result = new Button(sShell, SWT.NONE);
Result.setText("決策樹運算結(jié)果");
Result.setSize(new Point(94, 32));
Result.setLocation(new Point(65, 179));
ResultText = new Text(sShell, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.READ_ONLY);
ResultText.setBounds(new Rectangle(188, 48, 845, 504));
ResultText.setBackground(new Color(Display.getCurrent(), 200, 203, 203));
ResultText.setFont(new Font(Display.getDefault(), "\u6977\u4f53_GB2312", 18, SWT.BOLD));
ResultText.setTextLimit(-1);
ResultText.setEditable(false);
Read.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
Object[] Database = new Object[]{};
public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
System.out.println("mouseDown()"); // TODO Auto-generated Event stub mouseDown()
Database = ConnectToSqlServer();
Show.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
System.out.println("mouseDown()"); // TODO Auto-generated Event stub mouseDown()
ShowData( Database );
}
});
Result.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
System.out.println("mouseDown()"); // TODO Auto-generated Event stub mouseDown()
ImplementId3( Database );
}
});
}
});
}
public Object[] ConnectToSqlServer() {
String Name=JOptionPane.showInputDialog( "請輸入登錄名:" );
String Password=JOptionPane.showInputDialog( "請輸入密碼" );
String DatabaseName=JOptionPane.showInputDialog( "請輸入數(shù)據(jù)庫名:" );
String TableName=JOptionPane.showInputDialog( "請輸入表名:" );
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=" + DatabaseName;
//String driverName = "com.mysql.jdbc.Driver";
//String dbURL = "jdbc:mysql://localhost:3306/" + DatabaseName;
String userName = Name;
String userPwd = Password;
Connection dbConn;
try {
Class.forName( driverName );
dbConn = DriverManager.getConnection( dbURL, userName, userPwd );
ResultText.setText( "Database Read Successful!" );
Statement Sql = dbConn.createStatement();
String query = "select * from " + TableName;
ResultSet result1 = Sql.executeQuery( query );
ResultSetMetaData rsmd = result1.getMetaData();
int ColNum = rsmd.getColumnCount();
int RowNum = 0;
String[] ColName = new String[ColNum];
for( int i = 1; i <= ColNum;i ++ ) {
ColName[i - 1] = rsmd.getColumnName(i);
}
while( result1.next() ) {
RowNum++;
}
ResultSet result = Sql.executeQuery( query );
Object[] AllData = new Object[RowNum + 1];
int count= 0;
AllData[count] = ColName;
while( result.next() ) {
String[] Data = new String[ColNum];
count++;
for( int i = 1; i <= ColNum; i++ ) {
Data[i - 1] = result.getString(i);
}
AllData[count] = Data;
}
Sql.close();
dbConn.close();
return AllData;
}
catch(java.lang.ClassNotFoundException a) {
System.err.println("ClassNotFoundException: " + a.getMessage() );
ResultText.setText("Database Read Failed!");
return null;
}
catch (SQLException ex) {
ex.printStackTrace();
ResultText.setText("Database Read Failed!");
return null;
}
}
public void ShowData( Object[] a ) {
String res = "所讀取的表的數(shù)據(jù)為:\n ";
for( int j = 0; j < ( ( String[] )a[0] ).length; j++ ) {
res += ( ( String[] ) a[0] )[j] + "\t";
}
res += "\n";
for( int i = 1; i < a.length; i++ ) {
res += i + " ";
for( int j = 0; j < ( ( String[] )a[0] ).length; j++ ) {
res += ( ( String[] ) a[i] )[j] + " \t";
}
res += "\n";
}
ResultText.setText(res);
}
public void ImplementId3( Object[] x ) {
int count = x.length;
Object[] a = new Object[count-1];
String[] Name = new String[]{};
Name = ( String[] )x[0];
for( int i = 1; i < count; i++ ) {
a[i - 1] = x[i];
}
CreateId3 tree = new CreateId3();
int index = ( ( String[] )a[0] ).length - 1;
String res = "所得的決策樹經(jīng)先序遍歷后為:\n";
res = tree.create(a, index, res, Name);
ResultText.setText(res);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -