java - I can't insert into MsAccess database -
i have following ui
but then, can't insert data ms access database
. first row generated manually through ms access database
i don't know what's matter code, seems they're doing fine
private void dosimpan(string idnya, string namanya,string alamatnya,string teleponnya,string emailnya,string passwordnya,string rolesnya) { try { string query = "insert msemployee (employeeid, employeename, employeeaddress, employeephone, employeeemail, employeepassword, employeerole)values ('"+idnya+"','"+namanya+"','"+alamatnya+"','"+teleponnya+"','"+emailnya+"','"+passwordnya+"','"+rolesnya+"')"; connect.executequery(query); filltable(); cmd.printsuccess("master employee", namanya+" saved successfully"); } catch (exception e) { } }
here execquery
method
private statement st; public void executequery(string query) { try { st.executeupdate(query); } catch (sqlexception ex) { system.out.println("error: " +ex); } }
here printask()
method
public boolean printask(string title, string text) { if(joptionpane.showconfirmdialog(null, text, title, joptionpane.yes_no_option) == joptionpane.yes_option) { return true; } else { return false; } }
here isvalid()
method
private boolean isvalid(string name,string address,string phone,string email,string password) { if(name.equals("") || address.equals("") || phone.equals("") || email.equals("") || password.equals("")) { return false; } else if(val.isnumeric(phone) || !email.contains("@") && !email.endswith(".com")) { return false; } else { return true; } }
and here code on save button
//save private void jbutton4actionperformed(java.awt.event.actionevent evt) { string idnya = jtextfield1.gettext(); string namanya = jtextfield2.gettext(); string alamatnya = jtextarea1.gettext(); string teleponnya = jtextfield3.gettext(); string emailnya = jtextfield4.gettext(); string passwordnya = new string(jpasswordfield1.getpassword()); string rolesnya = jcombobox2.getselecteditem().tostring(); if(status==1) //to insert { if(isvalid(namanya, idnya, teleponnya, emailnya, passwordnya)) { if(cmd.printask("master employee", namanya+" entered, continue ?")) { dosimpan(idnya, namanya, alamatnya, teleponnya, emailnya, passwordnya, rolesnya); filltable(); defaultcontrol(false,false); crudbutton(true); } } else { cmd.printfail("master employee", "all field must filled"); } } else if (status==2)//to update { } }
where go wrong ? got "nencor saved successfully" then, can't find employee named nencor
on ms access database
my connect
constructor
public connect() { try { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); con = drivermanager.getconnection("jdbc:odbc:driver={microsoft access driver (*.mdb)};dbq=tifproject.mdb"); st = con.createstatement(1004, 1008); } catch (exception ex) { system.out.println("database error" + ex); } }
you have add in program.
class.forname("sun.jdbc.odbc.jdbcodbcdriver").newinstance(); connection = drivermanager.getconnection(databaseurl ,"",""); //databaseurl url db st=connection.createstatement();
error occurred because didn't mention , initialize variables needed database interaction.write above 3 lines in execquery
method , done.
Comments
Post a Comment