import java.sql.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class inensshow extends JFrame {
/***********************************
*
***********************************/
private static final long serialVersionUID = 1L;
private Connection connection;
private Statement statement;
private ResultSet resultSet;
//private ResultSetMetaData rsMetaData;
//GUI变量定义
private JTable table;
private JButton submitQuery;
private JTextField fromYear;
private JTextField fromMonth;
private JTextField fromDay;
private JTextField fromHour;
private JTextField fromMinute;
private JTextField fromSecond;
private JTextField toYear;
private JTextField toMonth;
private JTextField toDay;
private JTextField toHour;
private JTextField toMinute;
private JTextField toSecond;
public inensshow() {
super("Information search");
String url = "jdbc:mysql://localhost:3306/elevator";
String username = "root";
String password = "881026";
//加载驱动程序以连接数据库
try {
Class.forName("org.gjt.mm.mysql.Driver");
connection = DriverManager.getConnection(url, username, password );
}
//捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex ) {
System.err.println("装载 JDBC/ODBC 驱动程序失败。" );
//cnfex.printStackTrace();
System.exit(1); // terminate program
}
//捕获连接数据库异常
catch ( SQLException sqlex ) {
System.err.println( "无法连接数据库" );
//sqlex.printStackTrace();
System.exit(1); // terminate program
}
//如果数据库连接成功,则建立GUI
//SQL语句
submitQuery = new JButton( "查询" );
//Button事件
submitQuery.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//TO DO
search();
}
});
fromYear = new JTextField();
fromYear.setBorder(new TitledBorder("Year"));
fromMonth = new JTextField();
fromMonth.setBorder(new TitledBorder("Month"));
fromDay = new JTextField();
fromDay.setBorder(new TitledBorder("Day"));
fromHour = new JTextField();
fromHour.setBorder(new TitledBorder("Hour"));
fromMinute = new JTextField();
fromMinute.setBorder(new TitledBorder("Minute"));
fromSecond = new JTextField();
fromSecond.setBorder(new TitledBorder("Second"));
toYear = new JTextField();
toYear.setBorder(new TitledBorder("Year"));
toMonth = new JTextField();
toMonth.setBorder(new TitledBorder("Month"));
toDay = new JTextField();
toDay.setBorder(new TitledBorder("Day"));
toHour = new JTextField();
toHour.setBorder(new TitledBorder("Hour"));
toMinute = new JTextField();
toMinute.setBorder(new TitledBorder("Minute"));
toSecond = new JTextField();
toSecond.setBorder(new TitledBorder("Second"));
JPanel topLeftPanel = new JPanel();
topLeftPanel.setBorder(new TitledBorder("From time"));
topLeftPanel.setLayout(new GridLayout(2,3));
topLeftPanel.add(fromYear);
topLeftPanel.add(fromMonth);
topLeftPanel.add(fromDay);
topLeftPanel.add(fromHour);
topLeftPanel.add(fromMinute);
topLeftPanel.add(fromSecond);
JPanel topRightPanel = new JPanel();
topRightPanel.setBorder(new TitledBorder("To time"));
topRightPanel.setLayout(new GridLayout(2,3));
topRightPanel.add(toYear);
topRightPanel.add(toMonth);
topRightPanel.add(toDay);
topRightPanel.add(toHour);
topRightPanel.add(toMinute);
topRightPanel.add(toSecond);
JPanel topUpPanel = new JPanel();
topUpPanel.setLayout(new GridLayout(1,2));
topUpPanel.add(topLeftPanel);
topUpPanel.add(topRightPanel);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
//将"输入查询"编辑框布置到 "CENTER"
topPanel.add(topUpPanel, BorderLayout.CENTER );
//将"提交查询"按钮布置到 "SOUTH"
topPanel.add( submitQuery, BorderLayout.SOUTH );
table = new JTable();
Container c = getContentPane();
c.setLayout(new BorderLayout());
//将"topPanel"编辑框布置到 "NORTH"
c.add(topPanel, BorderLayout.NORTH);
//将"table"编辑框布置到 "CENTER"
c.add(table, BorderLayout.CENTER);
setSize(500, 300);
setLocation(200,200);
setVisible(true);
//显示Form
}
public void insert(int year,int month,int day,
int hour,int minute,int second,String message){
String str ="insert into log values('";
str += year+"'," + "'"+month+"','"+day+"','";
str += hour+"','"+minute+"','"+second+"','";
str += message+"')";
execute(str, false, true);
}
public void search() {
int fromYear = 0;
int fromMonth = 0;
int fromDay = 0;
int fromHour = 0;
int fromMinute = 0;
int fromSecond = 0;
int toYear = 0;
int toMonth = 0;
int toDay = 0;
int toHour = 0;
int toMinute = 0;
int toSecond = 0;
try {
fromYear = Integer.parseInt(this.fromYear.getText());
fromMonth = Integer.parseInt(this.fromMonth.getText());
fromDay = Integer.parseInt(this.fromDay.getText());
fromHour = Integer.parseInt(this.fromHour.getText());
fromMinute = Integer.parseInt(this.fromMinute.getText());
fromSecond = Integer.parseInt(this.fromSecond.getText());
toYear = Integer.parseInt(this.toYear.getText());
toMonth = Integer.parseInt(this.toMonth.getText());
toDay = Integer.parseInt(this.toDay.getText());
toHour = Integer.parseInt(this.toHour.getText());
toMinute = Integer.parseInt(this.toMinute.getText());
toSecond = Integer.parseInt(this.toSecond.getText());
}catch (Exception e){
JOptionPane.showMessageDialog(this, "数值错误!", "错误", 0);
return;
}
String str = "select * from log where ";
str += "year > " + fromYear + " and year < " + toYear + " and ";
str += "month > " + fromMonth + " and month < " + toMonth + " and ";
str += "day > " + fromDay + " and day < " + toDay + " and ";
str += "hour > " + fromHour + " and hour < " + toHour + " and ";
str += "minute > " + fromMinute + " and minute < " + toMinute + " and ";
str += "second > " + fromSecond + " and second < " + toSecond + ";";
execute(str, true, false);
}
public void execute(String query, boolean display, boolean update) {
try {
//执行SQL语句
//String query = inputQuery.getText();
statement = connection.createStatement();
if(update)
statement.executeUpdate(query);
else
resultSet = statement.executeQuery(query);
//如果需要,则在表格中显示结果
if (display)
displayResultSet(resultSet);
}catch (SQLException sqlex) {
sqlex.printStackTrace();
JOptionPane.showMessageDialog(this, "SQL错误!", "错误", 0);
}
}
@SuppressWarnings("unchecked")
private void displayResultSet(ResultSet rs)throws SQLException {
//定位到达第一条记录
boolean moreRecords = rs.next();
//如果没有记录,则提示一条消息
if (!moreRecords) {
JOptionPane.showMessageDialog(this,"无记录", "警告", 2);
return;
}
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
//获取字段的名称
ResultSetMetaData rsmd = rs.getMetaData();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );
//获取记录集
do {
rows.addElement( getNextRow( rs, rsmd ) );
} while ( rs.next() );
//在表格中显示查询结果
table = new JTable( rows, columnHeads );
JScrollPane scroller = new JScrollPane( table );
Container c = getContentPane();
c.remove(1);
c.add(scroller, BorderLayout.CENTER);
//刷新Table
c.validate();
}catch ( SQLException sqlex ) {
//sqlex.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private Vector getNextRow(ResultSet rs,ResultSetMetaData rsmd) throws SQLException {
Vector currentRow = new Vector();
for ( int
- 1
- 2
前往页