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

📄 authorjpanel.java

📁 培训时做的学生管理系统.基于J2SE平台开发
💻 JAVA
字号:
package cn.com.studentsystem.classnews;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import cn.com.studentsystem.log.Log;
import cn.com.util.DBConnection;

public class AuthorJPanel extends JPanel {
	Vector vector = new Vector();
	JTextField author_text;
	JButton authorselect_button ;
	JButton authorrefresh_button ;
	JButton authorcancel_button;
	public AuthorJPanel(){
		init();
	}
	public void init(){
		File file = new File("logdiary.txt");
		PrintWriter pw = null;
		try {
			 pw = new PrintWriter(new FileWriter(file,true),true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.log("AuthorJPanel", pw, "进入了按作者查询的界面");
	    
		 JLabel author_label = new JLabel("请输入作者");
		 author_text = new JTextField(50);
		 authorselect_button = new JButton("查找");
		 authorrefresh_button = new JButton("刷新");
		 authorcancel_button = new JButton("取消");
		 
		 this.add(author_label);
		 this.add(author_text);
		 this.add(authorselect_button);
		 this.add(authorrefresh_button);
		 this.add(authorcancel_button);
		 
		 class AuthorAction implements ActionListener{

		
			public void actionPerformed(ActionEvent arg0) {
				
				if(arg0.getActionCommand().equals("查找")){
					File file = new File("logdiary.txt");
					PrintWriter pw = null;
					try {
						 pw = new PrintWriter(new FileWriter(file,true),true);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.log("AuthorJPanel的查询按钮", pw, "通过输入的作者进行记录查询");
	    			DefaultTableModel model = (DefaultTableModel)ClassNews.jt1.getModel();
	    			int now_row = model.getRowCount();
	    			for(int i=now_row-1;i>=0;i--){
						model.removeRow(i);	
	    			}
	                
				    Connection con = DBConnection.getConnectionOracle();
				    try {
						PreparedStatement ps = con.prepareStatement("select datetime,title,author from news where author like ? order by datetime");
						 ps.setString(1, "%"+author_text.getText()+"%");
						ResultSet rs = ps.executeQuery();
						while(rs.next()){
						String date = rs.getString(1);
						String title = rs.getString(2);
						String author = rs.getString(3);
					    NewsVo vo = new NewsVo(date,title,author);
							  vector.add(vo);
						}
					    Iterator ite = vector.iterator();
						    
					    while(ite.hasNext()){
					    NewsVo nv = (NewsVo)ite.next();
						Object[] obj = {nv.getDatetime(),nv.getTitle(),nv.getAuthor()};
						ClassNews.dtm.addRow(obj);
						}
							SelectTopic.jf.dispose();
					    
					    } catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
					
				}else if(arg0.getActionCommand().equals("刷新")){
					File file = new File("logdiary.txt");
					PrintWriter pw = null;
					try {
						 pw = new PrintWriter(new FileWriter(file,true),true);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.log("AuthorJPanel的刷新按钮", pw, "查询数据库中的所有记录");
					DefaultTableModel model = (DefaultTableModel)ClassNews.jt1.getModel();
	    			int now_row = model.getRowCount();
	    			for(int i=now_row-1;i>=0;i--){
						model.removeRow(i);	
	    			}
	                
				    Connection con = DBConnection.getConnectionOracle();
							
					try {
							
						PreparedStatement ps = con.prepareStatement("select datetime,title,author from news order by datetime ");
						ResultSet rs = ps.executeQuery();
							    
					    while(rs.next()){
											    		 
						String date = rs.getString(1);
						String title = rs.getString(2);
					    String author = rs.getString(3);
					    NewsVo vo = new NewsVo(date,title,author);
						vector.add(vo);
							 }
					
						Iterator ite = vector.iterator();
							    
						while(ite.hasNext()){
						NewsVo nv = (NewsVo)ite.next();
						Object[] obj = {nv.getDatetime(),nv.getTitle(),nv.getAuthor()};
						ClassNews.dtm.addRow(obj);
							    	 
							 }
						SelectTopic.jf.dispose();
							     
					
				    } catch (SQLException e) {
					    e.printStackTrace();
				    }
					
				    }else if(arg0.getActionCommand().equals("取消")){
				    	File file = new File("logdiary.txt");
						PrintWriter pw = null;
						try {
							 pw = new PrintWriter(new FileWriter(file,true),true);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Log.log("AuthorJPanel的取消按钮", pw, "取消了任何查询");
					 SelectTopic.jf.dispose();
			      
				}
				
			   }
			 
	      } 
		 AuthorAction author_action = new AuthorAction();
		 authorselect_button.addActionListener(author_action);
		 authorrefresh_button.addActionListener(author_action);
		 authorcancel_button.addActionListener(author_action);
		 
	
	}

}

⌨️ 快捷键说明

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