package lzkKS;import java.awt.Dimension;public class Bingfang extends JFrame implements ActionListener{
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btn7;
private JButton btn8;
private JButton btn9;
private JButton btn10;
private JButton btn11;
private JButton btn12;
private String sid;
public Bingfang(){
Toolkit tk = Toolkit.getDefaultToolkit();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-700)/2,(screen.height-500)/2);
setTitle("哲凯医院病房管理系统");
setSize(634, 431);
setResizable(false);
getContentPane().setLayout(null);

btn1 = new JButton("外科401");
btn1.addActionListener(this);
btn1.setFont(new Font("新宋体", Font.PLAIN, 10));
btn1.setBackground(new Color(153, 255, 204));
btn1.setBounds(502, 50, 71, 105);
btn1.addActionListener(this);
getContentPane().add(btn1);

btn2 = new JButton("外科402");
btn2.addActionListener(this);
btn2.setFont(new Font("新宋体", Font.PLAIN, 10));
btn2.setBackground(new Color(153, 255, 204));
btn2.setBounds(431, 200, 71, 105);
btn2.addActionListener(this);
getContentPane().add(btn2);

btn3 = new JButton("外科403");
btn3.addActionListener(this);
btn3.setFont(new Font("新宋体", Font.PLAIN, 10));
btn3.setBackground(new Color(153, 255, 204));
btn3.setBounds(431, 50, 71, 105);
btn3.addActionListener(this);
getContentPane().add(btn3);

btn4 = new JButton("内科1404");
btn4.addActionListener(this);
btn4.setFont(new Font("新宋体", Font.PLAIN, 10));
btn4.setBackground(new Color(153, 255, 204));
btn4.setBounds(359, 200, 71, 105);
btn4.addActionListener(this);
getContentPane().add(btn4);

btn5 = new JButton("内科405");
btn5.addActionListener(this);
btn5.setFont(new Font("新宋体", Font.PLAIN, 10));
btn5.setBackground(new Color(153, 255, 204));
btn5.setBounds(271, 50, 71, 105);
btn5.addActionListener(this);
getContentPane().add(btn5);

btn6 = new JButton("内科406");
btn6.addActionListener(this);
btn6.setFont(new Font("新宋体", Font.PLAIN, 10));
btn6.setBackground(new Color(153, 255, 204));
btn6.setBounds(287, 200, 71, 105);
btn6.addActionListener(this);
getContentPane().add(btn6);

btn7 = new JButton("儿科407");
btn7.addActionListener(this);
btn7.setFont(new Font("新宋体", Font.PLAIN, 10));
btn7.setBackground(new Color(153, 255, 204));
btn7.setBounds(200, 50, 71, 105);
btn7.addActionListener(this);
getContentPane().add(btn7);

btn8 = new JButton("儿科408");
btn8.addActionListener(this);
btn8.setFont(new Font("新宋体", Font.PLAIN, 10));
btn8.setBackground(new Color(153, 255, 204));
btn8.setBounds(214, 200, 71, 105);
btn8.addActionListener(this);
getContentPane().add(btn8);

btn9 = new JButton("儿科409");
btn9.addActionListener(this);
btn9.setFont(new Font("新宋体", Font.PLAIN, 10));
btn9.setBackground(new Color(153, 255, 204));
btn9.setBounds(128, 50, 71, 105);
btn9.addActionListener(this);
getContentPane().add(btn9);

btn10 = new JButton("妇科410");
btn10.addActionListener(this);
btn10.setFont(new Font("新宋体", Font.PLAIN, 10));
btn10.setBackground(new Color(153, 255, 204));
btn10.setBounds(54, 200, 100, 70);
btn10.addActionListener(this);
getContentPane().add(btn10);

btn11 = new JButton("妇科411");
btn11.addActionListener(this);
btn11.setFont(new Font("新宋体", Font.PLAIN, 10));
btn11.setBackground(new Color(153, 255, 204));
btn11.setBounds(54, 50, 71, 105);
btn11.addActionListener(this);
getContentPane().add(btn11);

btn12 = new JButton("妇科412");
btn12.addActionListener(this);
btn12.setFont(new Font("新宋体", Font.PLAIN, 10));
btn12.setBackground(new Color(153, 255, 204));
btn12.setBounds(54, 273, 100, 70);
btn12.addActionListener(this);
getContentPane().add(btn12);

JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("D:\\workspace\\lzkKS\\img\\hosiptalPlan.jpg"));
lblNewLabel.setBounds(0, 0, 639, 401);
getContentPane().add(lblNewLabel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==btn1){
sid = "401";
}
if(e.getSource()==btn2){
sid = "402";
}
if(e.getSource()==btn3){
sid = "403";
}
if(e.getSource()==btn4){
sid = "404";
}
if(e.getSource()==btn5){
sid = "405";
}
if(e.getSource()==btn6){
sid = "406";
}
if(e.getSource()==btn7){
sid = "407";
}
if(e.getSource()==btn8){
sid = "408";
}
if(e.getSource()==btn9){
sid = "409";
}
if(e.getSource()==btn10){
sid = "410";
}
if(e.getSource()==btn11){
sid = "411";
}
if(e.getSource()==btn12){
sid = "412";
}
String url = "jdbc:odbc:hosiptal";
String SQL = "SELECT sroomID FROM Patient";
Connection conn;
PreparedStatement sql;
ResultSet rs;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e2) {
// TODO: handle exception
}
try {
conn = DriverManager.getConnection(url);
sql = conn.prepareStatement(SQL);
rs = sql.executeQuery();
while(rs.next()){
String sid1 = rs.getString(12);
if(sid.equals(sid1)){
JOptionPane.showMessageDialog(null, "该病房已满!");
}
else{
JOptionPane.showMessageDialog(null, "该病房空闲!");
this.setVisible(false);
}
}
} catch (SQLException e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}表结构如下:报错信息如下:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]非法的描述器索引
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
at sun.jdbc.odbc.JdbcOdbc.SQLColAttributes(JdbcOdbc.java:2620)
at sun.jdbc.odbc.JdbcOdbcResultSet.getColAttribute(JdbcOdbcResultSet.java:5443)
at sun.jdbc.odbc.JdbcOdbcResultSet.getColumnType(JdbcOdbcResultSet.java:6054)
at sun.jdbc.odbc.JdbcOdbcResultSet.getMaxCharLen(JdbcOdbcResultSet.java:5471)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:277)
at lzkKS.Bingfang.actionPerformed(Bingfang.java:201)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

解决方案 »

  1.   

    你的查询语言:SELECT sroomID FROM Patient 只查了一个字段但是在取得字段值的时候,却想取第12个字段的值,这是不可能取到的
      

  2.   

    是不是我SELECT后面要把所有字段都加上去
      

  3.   

    sql Exception 是你的sql语句在逻辑上产生了问题
      

  4.   

    根据你的sql语句 "SELECT sroomID FROM Patient" ,查询结果只有一列
    但是你获取结果的时候用的 String sid1 = rs.getString(12);表示要获取第12列的内容,跟你的查询结果不符