-3

I want to add new info to my mysql database from my Java GUI. I have 3 textfields which takes info from user and have 1 checkbutton. I have to add these datas, however I have a query for it.

String query="insert into buses (busId,custname,custsurname,custid,seats)
values('"+busId+"','"+textfield_1.getText()+"','"+textfield_2.getText()+
"','"+textfield_3.getText()+"','"+checkbox[1].getName()+"')";

When I write it in Java button, compiler give me a error like this (busId should be final) how can I solve this problem?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
gzml
  • 1
  • 2
  • 4
  • you want this text to appear on the button? or you want to execute the code when the user clicks the button? – jlordo Jan 02 '13 at 20:04
  • yes i want to execute it when user click the button,how??? – gzml Jan 02 '13 at 20:05
  • 1
    Also, consider using a [PreparedStatement](http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html), because of [this](http://xkcd.com/327/) – jlordo Jan 02 '13 at 20:05
  • Google Java Swing tutorial. Follow a few simple examples. Than transfer that knowledge to your current problem, – jlordo Jan 02 '13 at 20:07

1 Answers1

1
compiler give me a error like this(busId should be final) 

I'm assuming that you're using this variable within an anonymous inner class. All you have to do, as the error suggests, is declare the busId to be final, e.g.

final int busId = 0;
mre
  • 43,520
  • 33
  • 120
  • 170
  • but i take busid from other class with constructor ,can it affect something wrong?? – gzml Jan 02 '13 at 20:07
  • user select busid in jlist so i cant make it 0... – gzml Jan 02 '13 at 20:09
  • 1
    @gzml, Please see [this](http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class). – mre Jan 02 '13 at 20:10
  • 2
    @gzml, And for better help sooner, please include an [sscce](http://www.sscce.org). – mre Jan 02 '13 at 20:11