首页 > > 详细

辅导留学生LogServer JDialog Java语言、Java编程解析、讲解Java

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class LogServer extends JDialog {
LogServer logServer;
private final JPanel contentPanel = new JPanel();
private JTextArea textArea;
int x=1;
Connection conn=null;
PreparedStatement prep=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
LogServer dialog = new LogServer();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public LogServer() {
logServer=this;
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle("\u670D\u52A1\u7AEF");
setBounds(100, 100, 598, 473);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);

JLabel lblNewLabel = new JLabel("\u72B6\u6001\u4FE1\u606F\uFF1A");
lblNewLabel.setBounds(10, 21, 71, 24);
contentPanel.add(lblNewLabel);

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(44, 108, 495, 211);
contentPanel.add(scrollPane);

textArea = new JTextArea();
scrollPane.setViewportView(textArea);

JButton btnNewButton = new JButton("\u6E05\u7A7A\u72B6\u6001\u4FE1\u606F");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});
btnNewButton.setBounds(410, 22, 115, 23);
contentPanel.add(btnNewButton);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton kButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable(){
public void run(){
ServerSocket logSever = null;
int severPort = 9900;
try {
logSever = new ServerSocket(severPort);
System.out.println("。。。");
textArea.setText(textArea.getText()+"\r\n"+"。。。");
while (true){
Socket client = logSever.accept();
new LogThread(client).start();
System.out.println(":"+client.getInetAddress().getHostAddress()+"。");
textArea.setText(textArea.getText()+"\r\n"+":"+client.getInetAddress().getHostAddress()+"。");

}
}catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(logSever!=null){logSever.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}).start();
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
logServer.dispose();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
class LogThread extends Thread{

Socket logSocket;
public LogThread(Socket logSocket){this.logSocket= logSocket;}
public void run(){
InputStream is= null;BufferedReader reader= null;
try {
is = logSocket.getInputStream();
reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line;
while (true){
line = reader.readLine();
if(line!= null){
System.out.println(":"+logSocket.getInetAddress().getHostAddress()+":"+line);
textArea.setText(":"+logSocket.getInetAddress().getHostAddress()+":"+line+getDate()+"\r\n"+textArea.getText());
insertDate(line);


}else{
System.out.println(":"+logSocket.getInetAddress().getHostAddress()+"!");
textArea.setText(textArea.getText()+"\r\n"+":"+logSocket.getInetAddress().getHostAddress()+"!");
try {
prep.close();
conn.close();
} catch (SQLException e) {
// TODO catch
e.printStackTrace();
}
System.out.println("。。。");
textArea.setText(textArea.getText()+"。。。");
break;
}
}

}catch (IOException e) {
e.printStackTrace();
}finally{
try{
reader.close();
is.close();
logSocket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
private String getDate() {
// TODO
Date now=new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//
String hehe = dateFormat.format(now);
return hehe;
}
//
private void insertDate(String line) {
String url="jdbc:sqlserver://localhost:1433;DatabaseName=log";
String name="haha";
String pasw="123456";
String sql="insert into log values(?,?);";

// TODO
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url,name,pasw);
prep = conn.prepareStatement(sql);
prep.setInt(1,x);
prep.setString(2, line);
prep.addBatch();
prep.executeBatch();
x++;

} catch ( Exception e) {
// TODO catch
e.printStackTrace();
}

}
}
}
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!