java


JavaFX - Insert form textfield to SQLite Database


I made a Form scene in my Application and wanted to insert the entered data to my SQLite Database when I hit the Save button. Here is the short code:
#FXML
private void handleSaveNewShop(ActionEvent event) {
String name = shopname.getText();
String adress = streetadress.getText();
String city = cityname.getText();
String state = statename.getText();
String country = countryname.getText();
String zip = zipcode.getText();
String phonept1 = phonecountryid.getText();
String phonept2 = phoneareaid.getText();
String phonept3 = phoneothernumber.getText().toString();
Connection conn = null;
Statement stmt = null;
try{
Class.forName("org.sqlite.JDBC");
System.out.print("\nConnecting to database...");
conn = DriverManager.getConnection("jdbc:sqlite:FranchiseManagement.sqlite");
System.out.println(" SUCCESS!\n");
/*Tried this
String insertToshopList = "INSERT INTO shopList (name, adress, city, state, country, zipcode, phonect, phonearea, phonemain)" + "values(name,adress,city,state,country,zip,phonept1,phonept2,phonept3)";
stmt.executeUpdate(insertToshopList);
*/
//And This
//stmt.executeUpdate( "INSERT INTO shopList ('name','adress','city', 'state', 'country', 'zipcode', 'phonect', 'phonearea', 'phonemain') VALUES('"+name+"','"+adress+"','"+city+"'),'"+state+"','"+country+"','"+zip+"','"+phonept1+"','"+phonept2+"','"+phonept3+"'");
conn.commit();
System.out.println(" SUCCESS!\n");
conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
But I got NullPointException on my commited parts. The database connection seems okay and Connected.
Here is how my Database Looks:
Fixed it by removing the commit and added the INSERT into method well-formatted:
#FXML
private void handleSaveNewShop(ActionEvent event) {
String name = shopname.getText();
String adress = streetadress.getText();
String city = cityname.getText();
String state = statename.getText();
String country = countryname.getText();
String zip = zipcode.getText();
String phonept1 = phonecountryid.getText();
String phonept2 = phoneareaid.getText();
String phonept3 = phoneothernumber.getText();
Connection conn;
Statement stmt = null;
try{
Class.forName("org.sqlite.JDBC");
System.out.print("\nConnecting to database...");
conn = DriverManager.getConnection("jdbc:sqlite:FranchiseManagement.sqlite");
System.out.println(" SUCCESS!\n");
stmt = conn.createStatement();
stmt.executeUpdate( "INSERT INTO shopList ('name','adress','city', 'state', 'country', 'zipcode', 'phonect', 'phonearea', 'phonemain') VALUES('"+name+"','"+adress+"','"+city+"','"+state+"','"+country+"','"+zip+"','"+phonept1+"','"+phonept2+"','"+phonept3+"')");
//conn.commit();
System.out.println(" SUCCESS!\n");
conn.close();
} catch (ClassNotFoundException | SQLException e){
Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, e);
}

Related Links

Build an API for textfiles
How to write a List of ArrayLists to CSV format in Java
cmd command not running within java program
Do you need System.out.flush() before System.error.print(“”);
How to use Kerberos ticket on another machine
Define default locale and treat exceptions for Spring Locale Interceptor
overwrite a value inside a file in java
Open Source Continuous Integration tool for solo developer [Java] [closed]
Why is redis throwing NullPointerException running via JUnit with spring and default config?
Why does this python script run out of memory?
JPanel content not displaying
Regarding initialization of class instances
Sending events via code generation with annotations
Automatically generate Retrofit Types
ClassNotFoundException Error Spring + JSF trying to implement Spring Security login
JPA Entity updating database without persist/merge

Categories

HOME
ionic-framework
osgi
testng
netsuite
cookies
keras
oracle11g
xmpp
adb
jira
umd
enterprise-library-5
ssl-client-authentication
numeral.js
rascal
quicklook
visual-studio-2005
hex-editors
decomposition
connection-string
flux
predix
csrf-protection
wkwebview
language-agnostic
wijmo
buildbot
info.plist
bluestacks
twilio-api
rotational-matrices
y86
html5-fullscreen
disassembling
retina-display
espeak
dynamics-crm-2013
xenforo
reportbuilder
streamreader
broadcastreceiver
glew
spring-security-kerberos
no-www
s
node-sass
.net-4.6.2
galleria
jlink
nsarray
r-forge
pillow
account-kit
or-tools
google-cdn
jquery-filter
visual-c++-2008
spring-android
nxt
gridpane
vmware-tools
paypal-express
impresspages
firebaseui
lttng
actionbardrawertoggle
php-parse-error
msys
collapse
census
unity5.2.3
truevault
formatjs
fscommand
tween
network-interface
gulp-less
internet-connection
html-helper
sgen
fluentautomation
mechanize-ruby
prettify
multiprocessor
chronoforms
jquery-mobile-dialog
bitsharp
nsnetservice
dsn
external-accessory
coercion
django-tagging
site.master
h.323
wise
mozilla-prism
web-architecture
sustainable-pace
defensive-programming

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App