java - Trying to take data from JFrame and insert into SQLite. Connection works but insertion only inserts blank line. Why? -
i trying insert data sqlite database method "insertsqlb1". everytime type in textfield inserts blank line database. still beginner in java , thankful advice can :)
code follows:
import javax.swing.*; public class main { public static void main(string args[]) { jframe frame1 = new jframe(); frame1.settitle("password saver"); frame1.setsize(600, 600); frame1.setlocation(800, 200); frame1.setresizable(false); frame1.add(new jframefunctionality()); frame1.setdefaultcloseoperation(jframe.exit_on_close); frame1.setvisible(true); } } import javax.swing.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.sql.*; public class jframefunctionality extends jpanel { jtextfield tf1; jtextfield tf2; jtextfield tf3; preparedstatement prepstat = null; public jframefunctionality() { setlayout(null); //first part - create jlabel let user know enter in first textfield jlabel label1 = new jlabel("neues passwort:"); label1.setbounds(50, 70, 150, 40); add(label1); //first part - create first textfield enter password tf1 = new jtextfield(); tf1.setbounds(50, 100, 150, 40); add(tf1); repaint(); //first part - create second textfield enter name of program tf2 = new jtextfield(); tf2.setbounds(50, 140, 150, 40); add(tf2); repaint(); //first part - create first button add new password jbutton button1 = new jbutton("add new password"); button1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { codeforbuttons cfbobj1 = new codeforbuttons(); cfbobj1.insertsqlb1(); } }); button1.setbounds(200, 100, 150, 30); add(button1); //second part - create jlabel let user know enter when changing password jlabel label2 = new jlabel("enter new password change:"); label2.setbounds(50, 170, 200, 40); add(label2); //second part//create second textfield enter new password when changing password tf3 = new jtextfield(); tf3.setbounds(50, 200, 150, 40); add(tf3); //second part//create second button take changed password , put old password jbutton button3 = new jbutton("change password"); button3.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) {} }); button3.setbounds(200, 200, 150, 30); add(button3); //third part//create third button display existing password jbutton button2 = new jbutton("display password"); button2.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { //databaseconnection con2 = new databaseconnection (); //con2.listpasswords(); } }); button2.setbounds(200, 250, 150, 30); add(button2); } //konstruktur ende public string retrievetexttf1() { //retrieve text textfield 1 return tf2.gettext(); } } import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.statement; import java.sql.resultset; import javax.swing.joptionpane; public class codeforbuttons extends jframefunctionality { connection con1 = null; connection con2 = null; preparedstatement stat1 = null; statement stat2 = null; resultset rs = null; public void getconnection() { //establishes connection database "passwords" try { class.forname("org.sqlite.jdbc"); con1 = drivermanager.getconnection("jdbc:sqlite:passworddatabase.sqlite"); system.out.println("connection established..."); } catch (exception e) { system.out.println("error: " + " " + e.getmessage()); } } public void insertsqlb1() { //inserts application name database *not working, inserts blank row* try { getconnection(); string query = "insert passwords (anwendung) values (?)"; preparedstatement stat1 = con1.preparestatement(query); stat1.setstring(1, tf2.gettext()); stat1.execute(); joptionpane.showmessagedialog(null, "data saved!"); con1.close(); } catch (exception e) { system.out.println("error: " + e.getmessage()); } } public void listpasswords() { //gibt alle passwörter aus der datenbank aus und unterbricht dann die verbindung zur db try { getconnection(); this.stat2 = con2.createstatement(); resultset rs = stat1.executequery("select * passwords"); while (rs.next()) { string password = rs.getstring("passwort"); string program = rs.getstring("anwendung"); system.out.println("passwort: " + password + " " + "anwendung: " + program); } } catch (exception e) { system.out.println("error: " + e.getmessage()); } } }
the problem have codeforbuttons class extending jframefunctionality getting own instance of tf2 hiding instance used in display. (you creating 2 instance of tf2)
move codeforbuttons nested inner class of jframefunctionality , remove extends jframefunctionality codeforbuttons , have correct scope read tf2 variable.
see below:
public class jframefunctionality extends jpanel { jtextfield tf1; jtextfield tf2; jtextfield tf3; preparedstatement prepstat = null; public jframefunctionality() { setlayout(null); // first part - create jlabel let user know enter in // first textfield jlabel label1 = new jlabel("neues passwort:"); label1.setbounds(50, 70, 150, 40); add(label1); // first part - create first textfield enter password tf1 = new jtextfield(); tf1.setbounds(50, 100, 150, 40); add(tf1); // repaint(); // first part - create second textfield enter name of program tf2 = new jtextfield(); tf2.settext("test"); tf2.setbounds(50, 140, 150, 40); add(tf2); // repaint(); // first part - create first button add new password jbutton button1 = new jbutton("add new password"); button1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { system.out.println("::" + tf2.gettext()); codeforbuttons cfbobj1 = new codeforbuttons(); cfbobj1.insertsqlb1(); } }); button1.setbounds(200, 100, 150, 30); add(button1); // second part - create jlabel let user know enter when changing password jlabel label2 = new jlabel("enter new password change:"); label2.setbounds(50, 170, 200, 40); add(label2); // second part//create second textfield enter new password when changing password tf3 = new jtextfield(); tf3.setbounds(50, 200, 150, 40); add(tf3); // second part//create second button take changed password , put old password jbutton button3 = new jbutton("change password"); button3.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { } }); button3.setbounds(200, 200, 150, 30); add(button3); // third part//create third button display existing password jbutton button2 = new jbutton("display password"); button2.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { // databaseconnection con2 = new databaseconnection (); // con2.listpasswords(); } }); button2.setbounds(200, 250, 150, 30); add(button2); } // konstruktur ende public static void main(string args[]) { jframe frame1 = new jframe(); frame1.settitle("password saver"); frame1.setsize(600, 600); frame1.setlocation(800, 200); frame1.setresizable(false); frame1.add(new jframefunctionality()); frame1.setdefaultcloseoperation(jframe.exit_on_close); frame1.setvisible(true); } public class codeforbuttons { connection con1 = null; connection con2 = null; preparedstatement stat1 = null; statement stat2 = null; resultset rs = null; public void getconnection() { // establishes connection database "passwords" try { class.forname("org.sqlite.jdbc"); con1 = drivermanager.getconnection("jdbc:sqlite:passworddatabase.sqlite"); system.out.println("connection established..."); } catch (exception e) { system.out.println("error: " + " " + e.getmessage()); } } public void insertsqlb1() { // inserts application name database *not working, inserts blank // row* try { getconnection(); string query = "insert passwords (anwendung) values (?)"; preparedstatement stat1 = con1.preparestatement(query); stat1.setstring(1, tf2.gettext()); stat1.execute(); joptionpane.showmessagedialog(null, "data saved!"); con1.close(); } catch (exception e) { system.out.println("error: " + e.getmessage()); } } public void listpasswords() { // gibt alle passwörter aus der datenbank aus und unterbricht dann die verbindung // zur // db try { getconnection(); this.stat2 = con2.createstatement(); resultset rs = stat1.executequery("select * passwords"); while (rs.next()) { string password = rs.getstring("passwort"); string program = rs.getstring("anwendung"); system.out.println("passwort: " + password + " " + "anwendung: " + program); } } catch (exception e) { system.out.println("error: " + e.getmessage()); } } } } ===
or alternativly, remove extends jframefunctionality codeforbuttons change insertsqlb1 accept string parameter passed in.
public void insertsqlb1(string value) { // inserts application name database *not working, inserts blank // row* try { getconnection(); string query = "insert passwords (anwendung) values (?)"; preparedstatement stat1 = con1.preparestatement(query); stat1.setstring(1, value); stat1.execute(); joptionpane.showmessagedialog(null, "data saved!"); con1.close(); } catch (exception e) { system.out.println("error: " + e.getmessage()); } } then change caller
cfbobj1.insertsqlb1(tf2.gettext()); this way passing value data management class , achieve better split between interface layer , data management layer.
Comments
Post a Comment