|
- package 学籍管理系统3;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Vector;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JTabbedPane;
- import javax.swing.JLabel;
- import javax.swing.JComboBox;
- import javax.swing.LayoutStyle.ComponentPlacement;
- import javax.swing.JButton;
- import javax.swing.JSeparator;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.JScrollPane;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class Student extends JFrame {
- JPanel contentPane;
- JTextField textField;
- JTextField textField_1;
-
- Vector rowData;
- Vector columnNames;
-
- PreparedStatement ps=null;
- Connection ct=null;
- ResultSet rs =null;
-
- JComboBox comboBox;
- JTable table;
- JScrollPane scrollPane;
-
-
- JButton button;@SuppressWarnings({ "rawtypes", "unchecked" })
-
-
-
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Student frame = new Student();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
-
- }
- /**
- * Create the frame.
- * @throws SQLException
- */
- public Student() throws SQLException {
-
- setTitle("\u5B66\u751F");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(new BorderLayout(0, 0));
-
- JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
- contentPane.add(tabbedPane, BorderLayout.CENTER);
-
- JPanel panel = new JPanel();
- tabbedPane.addTab("\u67E5\u8BE2", null, panel, null);
- panel.setLayout(new BorderLayout(0, 0));
-
- JPanel panel_2 = new JPanel();
- panel.add(panel_2, BorderLayout.NORTH);
-
- JLabel label = new JLabel("\u8BF7\u9009\u62E9\u67E5\u8BE2\u79D1\u76EE\uFF1A");
- comboBox = new JComboBox();
- comboBox.setModel(new DefaultComboBoxModel(new String[] {"Java", "C++", "C#", "Ruby", "Android", "All"}));
- comboBox.setSelectedIndex(4);
-
- button = new JButton("\u67E5\u8BE2");
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- columnNames=new Vector();
- rowData =new Vector();
- if(comboBox.getSelectedItem().toString()=="All"){
- columnNames.add("姓名");
- columnNames.add("学号");
- columnNames.add("Java");
- columnNames.add("C++");
- columnNames.add("C#");
- columnNames.add("Ruby");
- columnNames.add("Android");
-
- try{
- //加载驱动
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //得到连接
- ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=Management","sa","zsmj1992");
- ps=ct.prepareStatement("select * from student ");
- rs=ps.executeQuery();
- while(rs.next())
- {
- Vector hang =new Vector();
- hang.add(rs.getString(1));
- hang.add(rs.getString(2));
- hang.add(rs.getString(3));
- hang.add(rs.getString(4));
- hang.add(rs.getString(5));
- hang.add(rs.getString(6));
- hang.add(rs.getString(7));
-
-
- //加入到rowData
- rowData.add(hang);
- }
- }
- catch(Exception e1){
- e1.printStackTrace();
- }
- finally{
- if(rs!=null)
- try {
- rs.close();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- if(ps!=null)
- try {
- ps.close();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- if(ct!=null)
- try {
- ct.close();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- table = new JTable(rowData, columnNames);
- //table.setVisible(false);
- scrollPane.setViewportView(table);
- }
- else{
- String str;
- str=comboBox.getSelectedItem().toString();
- int i;
- i=comboBox.getSelectedIndex()+3;
- columnNames.add("姓名");
- columnNames.add("学号");
- columnNames.add(str);
-
- try{
- //加载驱动
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //得到连接
- ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=Management","sa","zsmj1992");
- ps=ct.prepareStatement("select * from student ");
- rs=ps.executeQuery();
- while(rs.next())
- {
- Vector hang =new Vector();
-
- hang.add(rs.getString(1));
- hang.add(rs.getString(2));
- hang.add(rs.getString(i));
-
-
-
- //加入到rowData
- rowData.add(hang);
- }
- }
- catch(Exception e1){
- e1.printStackTrace();
- }
- finally{
- if(rs!=null)
- try {
- rs.close();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- if(ps!=null)
- try {
- ps.close();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- if(ct!=null)
- try {
- ct.close();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- table = new JTable(rowData, columnNames);
-
- scrollPane.setViewportView(table);
- }
- }
- });
-
-
-
-
-
-
-
-
-
- GroupLayout gl_panel_2 = new GroupLayout(panel_2);
- gl_panel_2.setHorizontalGroup(
- gl_panel_2.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel_2.createSequentialGroup()
- .addComponent(label)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(button)
- .addContainerGap(151, Short.MAX_VALUE))
- );
- gl_panel_2.setVerticalGroup(
- gl_panel_2.createParallelGroup(Alignment.LEADING)
- .addGroup(Alignment.TRAILING, gl_panel_2.createSequentialGroup()
- .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
- .addComponent(label)
- .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(button)))
- );
- panel_2.setLayout(gl_panel_2);
-
- JScrollPane scrollPane = new JScrollPane();
- panel.add(scrollPane, BorderLayout.CENTER);
-
-
-
- JPanel panel_1 = new JPanel();
- tabbedPane.addTab("\u8D26\u53F7\u7BA1\u7406", null, panel_1, null);
-
- JLabel label_1 = new JLabel("\u5B66\u53F7\uFF1A");
-
- textField = new JTextField();
- textField.setEnabled(false);
- textField.setColumns(10);
-
- JLabel label_2 = new JLabel("\u8BF7\u8F93\u5165\u65B0\u5BC6\u7801\uFF1A");
-
- textField_1 = new JTextField();
- textField_1.setColumns(10);
-
- JButton button_1 = new JButton("\u786E\u8BA4\u4FEE\u6539");
- GroupLayout gl_panel_1 = new GroupLayout(panel_1);
- gl_panel_1.setHorizontalGroup(
- gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel_1.createSequentialGroup()
- .addContainerGap()
- .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addComponent(label_1, Alignment.TRAILING)
- .addComponent(label_2, Alignment.TRAILING))
- .addPreferredGap(ComponentPlacement.RELATED)
- .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addComponent(button_1)
- .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
- .addComponent(textField_1)
- .addComponent(textField, GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)))
- .addContainerGap(181, Short.MAX_VALUE))
- );
- gl_panel_1.setVerticalGroup(
- gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel_1.createSequentialGroup()
- .addContainerGap()
- .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
- .addComponent(label_1)
- .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(18)
- .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addComponent(label_2)
- .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(28)
- .addComponent(button_1)
- .addContainerGap(102, Short.MAX_VALUE))
- );
- panel_1.setLayout(gl_panel_1);
-
-
-
-
- //这是数据库导出代码
-
-
-
-
-
-
-
-
- }
- }
-
复制代码 代码 有点长 但是麻烦大家给看下 可能这个问题很简单 但是困扰了我很长事件 求解决
|
|