sql - Converting a text entry(char) to an integer in java to be used in a java database -


i'm making database system school project(matric pat) have make database in java (for i'm using netbeans) , have create java program connects data , able perform specific tasks change data in database, display data etc.

i'm creating way add new entry, i've made jframe form (gui) using entries , alright far except, when try add entry says cannot put char variable types column has variable type of integer. feel i've tried don't know how fix this.

here sql statement used generate table database i'm working on:

create table tblpatients ( patient_id integer not null, first_name varchar(25) not null, last_name varchar(25) not null, date_of_birth date, sex varchar (6), address varchar(25), city varchar(25), postalcode varchar (4), charge integer, paid boolean, doctor_id integer not null, primary key (patient_id), foreign key (doctor_id) references tbldoctors(doctor_id) ) 

and code i'm using add entry database:

 private void btnaddpatientactionperformed(java.awt.event.actionevent evt) {                                                  try    {         string url = "jdbc:derby://localhost:1527/pat_db";          //only use these if database has username or password         //string username = "";         //string password = "";          connection con = drivermanager.getconnection(url);         statement stmt = con.createstatement();          string query = "insert tblpatients (patient_id, first_name, last_name, date_of_birth, sex, address, city, "                 + "postalcode, charge, paid, doctor_id) values ('"+patientid_field.gettext()+"', "                 + "'"+firstname_field.gettext()+"', '"+lastname_field.gettext()+"', "                 + "'"+day_field.getselecteditem()+month_field.getselecteditem()+year_field.getselecteditem()+"',"                 + " '"+sex_field.getselecteditem()+"', '"+address_field.gettext()+"', '"+city_field.gettext()+"',"                 + " '"+postalcode_field.gettext()+"', '"+charge_field.gettext()+"',"                 + " '"+paid_field.getselecteditem()+"', '"+doctorid_field.gettext()+"')";           stmt.execute(query);          joptionpane.showmessagedialog(null, "patient added database.");          patientid_field.settext(null);         firstname_field.settext(null);         lastname_field.settext(null);         day_field.setselecteditem("00");         month_field.setselecteditem("00");         year_field.setselecteditem("0000");         sex_field.setselecteditem("0");         address_field.settext(null);         city_field.settext(null);         postalcode_field.settext(null);         charge_field.settext("0");         paid_field.setselecteditem(0);         doctorid_field.settext(null);              }     catch(sqlexception ex)    {        joptionpane.showmessagedialog(null, ex.tostring());    }  }           

i add picture of gui , error message don't have enough rep yet, error message: "java.sql.sqlsyntaxerrorexception: columns of type 'integer' cannot hold values of type 'char'."

some of values put in statement surrounded single quotes. example, line:

            + " '"+postalcode_field.gettext()+"', '"+charge_field.gettext()+"'," 

not databases accept values surrounded single quotes valid values integers/numbers, should be:

            + " '"+postalcode_field.gettext()+"', "+charge_field.gettext()+"," 

but have recommend use prepared statement instead. building statement prone sql injection , makes code hard read , debug. prepared statement takes care of single quotes based on the type of value.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -