⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 id3panel.java

📁 商业只能中数据挖掘的决策树算法 用于数据分类
💻 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("读入数据");
		Read.setSize(new Point(94, 32));
		Read.setLocation(new Point(65, 50));
		
		Show = new Button(sShell, SWT.NONE);
		Show.setText("显示数据");
		Show.setSize(new Point(94, 32));
		Show.setLocation(new Point(65, 113));
		
		Result = new Button(sShell, SWT.NONE);
		Result.setText("决策树运算结果");
		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( "请输入数据库名:" );
        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 = "所读取的表的数据为:\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 = "所得的决策树经先序遍历后为:\n";
        res = tree.create(a, index, res, Name);
  
        ResultText.setText(res);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -