📄 duty.java
字号:
import javax.swing.*;//GUI
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
class Duty extends JFrame implements ActionListener{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JButton go=new JButton("查询房号");
JButton not=new JButton("取消");
JButton sear=new JButton("查询管理员");
JLabel lnum=new JLabel("房号");
JLabel lname=new JLabel("管理员姓名");
JTextField fnum=new JTextField();
JTextField fname=new JTextField();
JScrollPane jsp=new JScrollPane();
JTextArea anum=new JTextArea();
JMenuBar bar = new JMenuBar();
JMenu m1 = new JMenu("链接");
JMenuItem ni1 = new JMenuItem("管理员信息表info");
JMenuItem ni2 = new JMenuItem("房客登记表hotel");
Connection con=null;Statement Stmt=null;
Duty() {
try {
setLayout(new GridLayout(2,0));
setLocale(java.util.Locale.getDefault());
setJMenuBar(bar);
bar.add(m1);
m1.add(ni1);
m1.addSeparator();
m1.add(ni2);
p1.setLayout(new GridLayout(2,2));
p1.add(lnum);p1.add(fnum);p1.add(lname);p1.add(fname);
// p2.setLayout(new GridLayout(1,2));
p2.add(go);p2.add(not);p2.add(sear);
p3.setLayout(new GridLayout(3,0));
p3.add(p1); p3.add(p2);
add(p3);
anum.setText("房号列表");
// jsp.setLocation(100,400);jsp.setSize(500,200);
add(jsp);
jsp.getViewport().add(anum);
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection("jdbc:odbc:hotel","sa","");
Stmt=con.createStatement();
}
catch(SQLException ee) {}
setVisible(false);
setBounds(0,0,400,450);
validate();
go.addActionListener(this);
not.addActionListener(this);
sear.addActionListener(this);
ni1.addActionListener(this);
ni2.addActionListener(this);
}
catch(Exception e) { e.printStackTrace(); }
}
//按钮事件
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==go)
{
try {
if ( !fname.getText().equals( "" ) ) {
Statement statement =con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String query="SELECT * FROM duty WHERE yname = '" + fname.getText() + "'";
ResultSet rs = statement.executeQuery( query );
display( rs );
anum.append( "\n查询成功\n" );
statement.close();
}
else
anum.setText( "请输入要查找的管理员姓名" );
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
else if(e.getSource()==sear)
{
try {
if ( !fnum.getText().equals( "" ) ) {
Statement statement =con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String query="SELECT * FROM duty WHERE fnum = '" + fnum.getText() + "'";
ResultSet rs = statement.executeQuery( query );
show( rs );
anum.append( "\n查询成功\n" );
statement.close();
}
else
anum.setText( "请输入要查找的房号" );
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
else if(e.getSource()==not)
{
fnum.setText( "" );
fname.setText( "" );
}
else if(e.getSource()==ni1)
{
setVisible(false);
Manager mana=new Manager();
mana.setVisible(true);
}
else if(e.getSource()==ni2)
{
setVisible(false);
Hotel ho=new Hotel();
ho.setVisible(true);
}
}
public void display( ResultSet rs )
{
try {
boolean morerows=false;//是否该管理员不只负责一个房间
if (rs.next() ) {
fnum.setText( rs.getString(2) );
if(rs.next()) morerows=true;
}
else anum.append( "\n没找到记录\n" );
if(morerows)
{
int m=0;
rs.beforeFirst();
rs.next();
String p=rs.getString(2);
while(rs.next())
{
p=p + " " + rs.getString(2);
if(++m%5==0) p=p+"\n";
}
anum.setText(p);
}
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}//display()结束
public void show( ResultSet rs )
{
try {
if (rs.next() ) { fname.setText( rs.getString(1) ); }
else anum.append( "\n没找到记录\n" );
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}//show()结束
}
/*public static void main(String[] args){
Duty d = new Duty();
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -