1

我正在使用以下编码从数据库中获取值,它运行良好......但问题是当我按下回车按钮时,它会发出类似声音的错误......请告诉我哪里是错误编码????它是文本字段的文本字段和事件侦听器。

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Trans implements ActionListener
{
Connection con;
Statement st;
ResultSet rs;
PreparedStatement ps;

Frame f;

Label accountl=new Label("Account Number:");
Label name=new Label("Customer Name:");
Label dob=new Label("Customer DOB:");

TextField accountt=new TextField(15);
Label namel=new Label();
Label dobl=new Label();

Label[] bal=new Label[50];
Label[] dep=new Label[50];
Label[] wit=new Label[50];

Label withdraw=new Label("Withdraw");
Label deposit=new Label("Deposit");
Label balance=new Label("Balance");

ImageIcon ic=new ImageIcon("close.jpg");
JButton ok=new JButton(ic);

Font lt=new Font("Book Antiqua",Font.BOLD,14);

ImageIcon i0=new ImageIcon("transaction.png");
JLabel idl=new JLabel(i0);

ImageIcon i01=new ImageIcon("blue.png");
JLabel idl1=new JLabel(i01);

int stringcounter,temp=0;
String s,s1,s2,s3,s4;
String[] str=new String[50];
String[] str1=new String[50];
String[] str2=new String[50];

public Trans()
{

connect();
frame();

}

    public void frame()
    {

    f=new Frame("Last 5 Transactions Only");
    f.setResizable(false);
    f.setVisible(true);
    f.setBounds(250,20,600,500);            //(hor,ver,width,hieght)

    Panel p=new Panel();
    f.add(p);
    p.setLayout(null);
    p.setBackground(Color.WHITE);   // set bg if you want

    accountl.setFont(lt);
    accountt.setFont(lt);
    name.setFont(lt);
    namel.setFont(lt);
    dob.setFont(lt);
    dobl.setFont(lt);

    p.add(accountl);
    accountl.setBounds(20,30,150,25);   //(hor,ver,width,hieght)
    p.add(accountt);            // automatic account num.
    accountt.setBounds(180,30,100,25);  //(hor,ver,width,hieght)

    p.add(name);
    name.setBounds(20,75,150,25);           //(hor,ver,width,hieght)
    p.add(namel);                   // automatic account num.
    namel.setBounds(180,75,100,25);         //(hor,ver,width,hieght)

    p.add(dob);
    dob.setBounds(20,120,150,25);           //(hor,ver,width,hieght)
    p.add(dobl);                    // automatic account num.
    dobl.setBounds(180,120,100,25);         //(hor,ver,width,hieght)

    int diff=40,fix=170;            // hor ver arrangement

    for(int j=49;j>44;j--)
    {
    bal[j]=new Label("");           // last five labels added
    p.add(bal[j]);
    bal[j].setFont(lt);
    fix=fix+diff;
    bal[j].setBounds(180,fix,60,25);
    }

    int diff1=40,fix1=170;          // hor ver arrangement

    for(int j1=49;j1>44;j1--)
    {
    dep[j1]=new Label("");          // last five labels added
    p.add(dep[j1]);
    dep[j1].setFont(lt);
    fix1=fix1+diff1;
    dep[j1].setBounds(100,fix1,60,25);
    }

    int diff2=40,fix2=170;          // hor ver arrangement

    for(int j2=49;j2>44;j2--)
    {
    wit[j2]=new Label("");          // last five labels added
    p.add(wit[j2]);
    wit[j2].setFont(lt);
    fix2=fix2+diff2;
    wit[j2].setBounds(20,fix2,60,25);
    }

    p.add(withdraw);
    withdraw.setFont(lt);
    withdraw.setBounds(20,180,70,25);

    p.add(deposit);
    deposit.setFont(lt);
    deposit.setBounds(100,180,60,25);

    p.add(balance);
    balance.setFont(lt);
    balance.setBounds(180,180,60,25);

    p.add(ok);
    ok.setBorder(null);
    ok.setCursor(new Cursor(Cursor.HAND_CURSOR));
    ok.setBounds(260,390,100,35);           //(hor,ver,width,hieght)

    p.add(idl);
    idl.setBounds(370,150,197,173);         //(hor,ver,width,hieght)

    p.add(idl1);
    idl1.setBounds(20,150,280,37);          //(hor,ver,width,hieght)

    accountt.addActionListener(this);
    ok.addActionListener(this);

    }

public void actionPerformed(ActionEvent ae)
{


if(ae.getSource()==accountt)
{
try
{
    accountt.setEditable(false);
    s=accountt.getText();
    stringcounter=0;
    String sql="select * from CUSTOMERS where ACCOUNT="+s;
    rs=st.executeQuery(sql);

    while(rs.next())
    {
    s4=rs.getString(3);     // user NAME return
    s2=rs.getString(5);     // DOB
    temp++;
    }

    if(temp==0)
    {
    JOptionPane.showMessageDialog(null,"Record Not Found"); 
    }

    namel.setText(s4);
    dobl.setText(s2);

    String sql1="select * from TRANSACTIONS where ACCOUNT="+s;
    rs=st.executeQuery(sql1);       
    while(rs.next())
    {

     str[stringcounter]=rs.getString(4);    //balance
     str1[stringcounter]=rs.getString(3);   //deposit
     str2[stringcounter]=rs.getString(2);   //Withdraw
     stringcounter++;

    }
        stringcounter--;            // extra subtracted
        int cond=stringcounter,lastfive=49; // needed below

        for(int x=stringcounter; x>(cond-5); x--)
        {

        bal[lastfive].setText(str[x]);
        dep[lastfive].setText(str1[x]);
        wit[lastfive].setText(str2[x]);

        if(str[x]==null)
        {
        break;              // if no record break
        }

        if(str1[x]==null)
        dep[lastfive].setText("---");

        if(str2[x]==null)
        wit[lastfive].setText("---");

        lastfive--;

        }

}       //try

catch(Exception e)
{
// System.out.println("error in change button event"+stringcounter+s2+s+s4);
}
}   // if accountt

if(ae.getSource()==ok)
{
f.setVisible(false);
}

}           // action performed



    public void connect()
    {

    try
    {

    String driver="sun.jdbc.odbc.JdbcOdbcDriver";   //declaring drivers
    Class.forName(driver);              //loading drivers

    String path="jdbc:odbc:mybank";         //defining path to database
    con=DriverManager.getConnection(path);      //connecting to database
    st=con.createStatement();           //accessing to Tabels of DB

    }   // try

    catch(Exception e)
    {
    System.out.println("Error in connect method");
    }

    }           // connect

public static void main(String arg[])
{
    new Trans();
}
}
4

0 回答 0