📄 louyuinfo.java
字号:
import javax.swing.table.AbstractTableModel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.sql.*;
import java.util.*;
public class LouYuinfo extends JFrame
{
private JPanel buttonPanel,tablePanel,fieldPanel,fieldPanel1,fieldPanel2;
private JLabel label1 = new JLabel();
private JTextField districtid = new JTextField(3);
private JLabel label2 = new JLabel();
private JTextField buildingid = new JTextField(3);
private JLabel label3 = new JLabel();
private JTextField storey = new JTextField(4);
private JLabel label4 = new JLabel();
private JTextField area = new JTextField(4);
private JLabel label5 = new JLabel();
private JTextField height = new JTextField(4);
private JLabel label6 = new JLabel();
private JTextField type = new JTextField(5);
private JLabel label7 = new JLabel();
private JTextField state = new JTextField(6);
/*private JLabel label8 = new JLabel();
private JTextField sex = new JTextField(4);
private JLabel label9 = new JLabel();
private JTextField idnum = new JTextField(13);
private JLabel label10 = new JLabel();
private JTextField addr = new JTextField(15);
private JLabel label11 = new JLabel();
private JTextField tetel = new JTextField(8);*/
private JButton searchButton = new JButton("按小区查询");
private JButton addButton = new JButton("添加");
private JButton changeButton = new JButton("修改");
private JButton deleteButton = new JButton("删除");
private JButton renewButton = new JButton("重置");
private JButton updateButton= new JButton("更新");
String title[]= {"小区编号","楼宇编号","楼宇层数","产权面积","楼宇高度","类型","楼宇状态"};
Vector vector=new Vector();
Connection connection = null;
ResultSet rSet = null;
Statement statement = null;
AbstractTableModel tm;
public LouYuinfo()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
Container con = getContentPane();
con.setLayout(new BorderLayout());
label1.setText("小区编号");
label2.setText("楼宇编号");
label3.setText("楼宇层数");
label4.setText("产权面积");
label5.setText("楼宇高度");
label6.setText("类型");
label7.setText("楼宇状态");
searchButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
searchButton_actionPerformed(e);
}
});
addButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addButton_actionPerformed(e);
}
});
changeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
changeButton_actionPerformed(e);
}
});
deleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
deleteButton_actionPerformed(e);
}
});
renewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
renewButton_actionPerformed(e);
}
});
updateButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
updateButton_actionPerformed(e);
}
});
fieldPanel1=new JPanel();
fieldPanel1.setLayout(new FlowLayout());
fieldPanel1.add(label1);
fieldPanel1.add(districtid);
fieldPanel1.add(label2);
fieldPanel1.add(buildingid);
fieldPanel1.add(label3);
fieldPanel1.add(storey);
fieldPanel1.add(label4);
fieldPanel1.add(area);
fieldPanel2=new JPanel();
fieldPanel2.setLayout(new FlowLayout());
fieldPanel2.add(label5);
fieldPanel2.add(height);
fieldPanel2.add(label6);
fieldPanel2.add(type);
fieldPanel2.add(label7);
fieldPanel2.add(state);
fieldPanel = new JPanel();
fieldPanel.setLayout(new BorderLayout());
fieldPanel.add(fieldPanel1,BorderLayout.NORTH);
fieldPanel.add(fieldPanel2,BorderLayout.CENTER);
//fieldPanel.add(fieldPanel3,BorderLayout.SOUTH);
buttonPanel=new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(searchButton);
buttonPanel.add(addButton);
buttonPanel.add(changeButton);
buttonPanel.add(deleteButton);
buttonPanel.add(renewButton);
buttonPanel.add(updateButton);
tablePanel=new JPanel();
createtable();
con.add(tablePanel,BorderLayout.NORTH);
con.add(fieldPanel,BorderLayout.CENTER);
con.add(buttonPanel,BorderLayout.SOUTH);
updated();
setSize(600,650);
setResizable(false);
setVisible(true);
}
public static void main(String[] args)
{
LouYuinfo userInformation=new LouYuinfo();
userInformation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
private void createtable()
{
JTable table;
JScrollPane scroll;
// vector = new Vector();
tm = new AbstractTableModel()
{
public int getColumnCount()
{
return title.length;
}
public int getRowCount()
{
return vector.size();
}
public String getColumnName(int col)
{
return title[col];
}
public Object getValueAt(int row, int column)
{
if(!vector.isEmpty())
{
return ((Vector)vector.elementAt(row)).elementAt(column);
}
else
{
return null;
}
}
public void setValueAt(Object value, int row, int column)
{
}
public Class getColumnClass(int c)
{
return getValueAt(0,c).getClass();
}
public boolean isCellEditable(int row, int column)
{
return false;
}
};
table = new JTable(tm);
table.setToolTipText("Display Query Result");
table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
table.setCellSelectionEnabled(false);
table.setShowHorizontalLines(true);
table.setShowVerticalLines(true);
scroll = new JScrollPane(table);
scroll.setBounds(6,20,540,250);
tablePanel.add(scroll);
}
void addButton_actionPerformed(ActionEvent e)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:estate";
connection = DriverManager.getConnection(url);
statement = connection.createStatement();
String sql1= "insert into building_info(district_id,building_id,total_storey,total_area,height,type,status) values("+Integer.parseInt(districtid.getText())+","+Integer.parseInt(buildingid.getText())+","+Integer.parseInt(storey.getText())+","+Integer.parseInt(area.getText())+","+Integer.parseInt(height.getText())+","+Integer.parseInt(type.getText())+",'"+state.getText()+"')";
statement.executeUpdate(sql1);
}
catch(Exception ex){
JOptionPane.showMessageDialog(LouYuinfo.this,"填加数据出错","错误",JOptionPane.ERROR_MESSAGE);
//ex.printStackTrace();
}
finally{
try{
if(statement != null){
statement.close();
}
if(connection != null){
connection.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -