当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » Java  » 编程基础
分享到: 
收藏 +0
2
利用java的反射机制实现对象的调用,方法调用传入不同的字符串(实际为相应按钮的事件处理方法的名称)来处理不同的功能。将数据库结果集对象传入displayTable()方法,返回一个表格对象即可显示;
标签: <无>

代码片段(4) [全屏查看所有代码]

1. [文件] Connect.java ~ 690B     下载(0)     跳至 [1] [2] [3] [4] [全屏预览]

package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Connect {
     
	public Connect() {
		
	}

	public static Statement getStatement(String dataBaseName ){
		
		Connection conn=null;
		Statement state = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306"+dataBaseName,"admin","admin");
			state=conn.createStatement();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return state;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

2. [文件] EventHandle.java ~ 898B     下载(0)     跳至 [1] [2] [3] [4] [全屏预览]

package com.test;

import java.sql.SQLException;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import com.cn.AddWindow;
import com.cn.BaseWindow;
import com.cn.Select;

public class EventHandle {
	//////根据数据库结果集返回表格对象
	private ShowTableResultSet showTable;
   // private JPanel pan=com.liu.MainWindow.panel;
	public EventHandle() {
		showTable =new ShowTableResultSet();
	}

	public void menuItemBack() {
		System.exit(0);
	}
    
	public void menuItemTest() {
		
		
	}

	public void menuItemInsert() {
          
		
	}

	public void menuItemDelete() {
	
	
	}

	public void menuItemUpdate() {
		
	
	}

	public void menuItemID() {
		
	}

	public void menuItemKool() {
         
	}

	public void menuItemTeacher() {
		
	}

	public void menuItemName() {
		
	}
  public void menuItemManager(){
	 
  }
	
}

3. [文件] MainWindow.java ~ 3KB     下载(0)     跳至 [1] [2] [3] [4] [全屏预览]

package com.test;



import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class MainWindow {
    private JFrame frame;
    private  JPanel panel;
    private  static JPanel main;
    private JMenuBar menuBar;
    private List<JMenuItem> menuItem;
    private JMenu menu1,menu2,menu3,menu4;
    private byte indexCommand=0;
	public MainWindow() {
		init();
		initLayout();
		addMenuItem("/增加/删除/修改",menu1);
		addMenuItem("/行政/教师/后勤",menu2);
		addMenuItem("/工号查询/姓名查询",menu3);
		addMenuItem("/连接测试/退出",menu4);
	}

	
	private void initLayout() {
		frame.add(panel);
		frame.add(panel);
		panel.add(menuBar,BorderLayout.NORTH);
		panel.add(main,BorderLayout.CENTER);
		menuBar.add(menu1);
		menuBar.add(menu2);
		
		menuBar.add(menu3);
		menuBar.add(menu4);
		menuBar.setBorder(new EmptyBorder(15,5,15,25));
	}

	private void init() {
		frame=new JFrame("企业客户信息管理系统");
		panel=new JPanel(new BorderLayout());
		main=new JPanel(new BorderLayout());
		menuBar=new JMenuBar();
		menuItem=new ArrayList<JMenuItem>();
		menu1=new JMenu("信息管理");
		menu2=new JMenu("分类查询");
		menu3=new JMenu("精确查询");
		menu4=new JMenu("通用功能");
		
		initFrame(frame);
		
	}
    private void initFrame(JFrame f){
    	f.setSize(800,600);
    	f.setLocationRelativeTo(null);
    	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	f.setVisible(true);
    }
    
   
    public void addMenuItem(String name,JMenuItem menu){
    	String n[]=name.trim().split("[/]+");
    	JMenuItem temp;
    	for(int i=0;i<n.length;i++){
    		temp=new JMenuItem(n[i].trim().toString());
    		menuItem.add(temp);
    		if(i!=0){
    		temp.setActionCommand(""+indexCommand++);
    		}
    		
    		menu.add(temp);
    		temp.addActionListener(new ActionListener(){
                private String dd[]={"Insert","Delete","Update",
                		"Manager","Teacher","Kool","ID","Name",
                		"Test","Back"};  
				@Override
				public void actionPerformed(ActionEvent e) {
					JMenuItem ve=(JMenuItem)e.getSource();
					for(int i=0;i<dd.length;i++){	
						if(i==Integer.parseInt(ve.getActionCommand())){
						getMethod(dd[i++]);
					     break;
						}
					}
					
				}
    			
    		});	
    	}
    	
    }
    public void getMethod(String method){
		
		try {
			
			Class<EventHandle> event=EventHandle.class;
			Constructor<EventHandle> con=event.getConstructor();
			EventHandle eventHandle=con.newInstance();
			Method met=event.getMethod("menuItem"+method);
			met.invoke(eventHandle);
		} catch (Exception e) {
			
			e.printStackTrace();
		} 
	}
   public static JPanel getMain(){
	   return MainWindow.main;
   }
	public static void main(String[] args) {
		MainWindow mian=new MainWindow();
        
	}

}

4. [文件] ShowTableResultSet.java ~ 2KB     下载(0)     跳至 [1] [2] [3] [4] [全屏预览]

package com.test;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTable;

public class ShowTableResultSet {
	public ShowTableResultSet() {
		
	}
    public void setTable(JTable t){
    }
    
    public JTable displayTable(ResultSet rs){
    	Vector<String> headColumn=new Vector<String>();//列向量 对象
		Vector<Vector<String>> row=new Vector<Vector<String>>();///行向量列
    	try {
			boolean moreRecord=rs.next();
			if(!moreRecord){
				JOptionPane.showMessageDialog(null, "结果集为空!请核对后再查询!",
						"系统信息", JOptionPane.INFORMATION_MESSAGE);
				return new JTable(null,headColumn);
			}
			
			ResultSetMetaData rsmd = rs.getMetaData();
			
			for (int i = 1; i <= rsmd.getColumnCount(); ++i){
				headColumn.addElement(rsmd.getColumnName(i));
			    System.out.println(rsmd.getColumnName(i));
			}
			do {
				row.addElement(getNextRow(rs, rsmd));//调用函数
			} while (rs.next());
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return	new JTable(row,headColumn);
    }
	private Vector<String> getNextRow(ResultSet rsNext, ResultSetMetaData rsmdNext){
		Vector<String> currentRow=new Vector<String>();
		try {
			for (int i = 1; i <= rsmdNext.getColumnCount(); ++i)
				currentRow.addElement(rsNext.getString(i));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return currentRow;
	}
	
}


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

开源从代码分享开始 分享代码