SQL not Updating Data
I'm trying to update data in table in JavaFX. For some reason it wouldn't work. Anyone has an idea what could be wrong in the following code?
public void replaceData(ActionEvent event) {
try {
Connection conn = SqliteConnection.Connector();
this.data = FXCollections.observableArrayList();
ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName='" + ProductName1.getText() + "', Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock='" + TotalStock1.getText() + "' Where ID=" + ID2.getText() + "");
this.dataid.setText(ProductName1.getText());
this.dataBarcode.setText(Barcode1.getText());
this.dataCategory.setText(Category1.getText());
this.dataSubCategory.setText(SubCategory1.getText());
this.dataTotalStock.setText(TotalStock1.getText());
this.dataSinglePrice.setText(SinglePrice1.getText());
} catch (SQLException ex) {
ex.printStackTrace();
}
}
java sql jdbc
add a comment |
I'm trying to update data in table in JavaFX. For some reason it wouldn't work. Anyone has an idea what could be wrong in the following code?
public void replaceData(ActionEvent event) {
try {
Connection conn = SqliteConnection.Connector();
this.data = FXCollections.observableArrayList();
ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName='" + ProductName1.getText() + "', Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock='" + TotalStock1.getText() + "' Where ID=" + ID2.getText() + "");
this.dataid.setText(ProductName1.getText());
this.dataBarcode.setText(Barcode1.getText());
this.dataCategory.setText(Category1.getText());
this.dataSubCategory.setText(SubCategory1.getText());
this.dataTotalStock.setText(TotalStock1.getText());
this.dataSinglePrice.setText(SinglePrice1.getText());
} catch (SQLException ex) {
ex.printStackTrace();
}
}
java sql jdbc
probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them
– kleopatra
Nov 24 '18 at 14:38
It's better to use aPreparedStatementfor creating a query containing data you need to insert based on the gui input. Furthermore by leaving thecatchempty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least putex.printStackTrace();in there.) There are other issues though: There is a,just beforeWHEREand you useexecuteQueryfor a update query.
– fabian
Nov 24 '18 at 15:24
add a comment |
I'm trying to update data in table in JavaFX. For some reason it wouldn't work. Anyone has an idea what could be wrong in the following code?
public void replaceData(ActionEvent event) {
try {
Connection conn = SqliteConnection.Connector();
this.data = FXCollections.observableArrayList();
ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName='" + ProductName1.getText() + "', Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock='" + TotalStock1.getText() + "' Where ID=" + ID2.getText() + "");
this.dataid.setText(ProductName1.getText());
this.dataBarcode.setText(Barcode1.getText());
this.dataCategory.setText(Category1.getText());
this.dataSubCategory.setText(SubCategory1.getText());
this.dataTotalStock.setText(TotalStock1.getText());
this.dataSinglePrice.setText(SinglePrice1.getText());
} catch (SQLException ex) {
ex.printStackTrace();
}
}
java sql jdbc
I'm trying to update data in table in JavaFX. For some reason it wouldn't work. Anyone has an idea what could be wrong in the following code?
public void replaceData(ActionEvent event) {
try {
Connection conn = SqliteConnection.Connector();
this.data = FXCollections.observableArrayList();
ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName='" + ProductName1.getText() + "', Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock='" + TotalStock1.getText() + "' Where ID=" + ID2.getText() + "");
this.dataid.setText(ProductName1.getText());
this.dataBarcode.setText(Barcode1.getText());
this.dataCategory.setText(Category1.getText());
this.dataSubCategory.setText(SubCategory1.getText());
this.dataTotalStock.setText(TotalStock1.getText());
this.dataSinglePrice.setText(SinglePrice1.getText());
} catch (SQLException ex) {
ex.printStackTrace();
}
}
java sql jdbc
java sql jdbc
edited Nov 24 '18 at 15:52
a_horse_with_no_name
301k46459552
301k46459552
asked Nov 24 '18 at 14:09
Sektor InspektorSektor Inspektor
42
42
probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them
– kleopatra
Nov 24 '18 at 14:38
It's better to use aPreparedStatementfor creating a query containing data you need to insert based on the gui input. Furthermore by leaving thecatchempty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least putex.printStackTrace();in there.) There are other issues though: There is a,just beforeWHEREand you useexecuteQueryfor a update query.
– fabian
Nov 24 '18 at 15:24
add a comment |
probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them
– kleopatra
Nov 24 '18 at 14:38
It's better to use aPreparedStatementfor creating a query containing data you need to insert based on the gui input. Furthermore by leaving thecatchempty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least putex.printStackTrace();in there.) There are other issues though: There is a,just beforeWHEREand you useexecuteQueryfor a update query.
– fabian
Nov 24 '18 at 15:24
probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them
– kleopatra
Nov 24 '18 at 14:38
probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them
– kleopatra
Nov 24 '18 at 14:38
It's better to use a
PreparedStatement for creating a query containing data you need to insert based on the gui input. Furthermore by leaving the catch empty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least put ex.printStackTrace(); in there.) There are other issues though: There is a , just before WHERE and you use executeQuery for a update query.– fabian
Nov 24 '18 at 15:24
It's better to use a
PreparedStatement for creating a query containing data you need to insert based on the gui input. Furthermore by leaving the catch empty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least put ex.printStackTrace(); in there.) There are other issues though: There is a , just before WHERE and you use executeQuery for a update query.– fabian
Nov 24 '18 at 15:24
add a comment |
2 Answers
2
active
oldest
votes
You did mistake in query statement you put comma before where close. Just delete that comma
ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName=" + ProductName1.getText() + ", Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock=" + TotalStock1.getText() + " Where ID=" + ID2.getText() + "");
paste this code
This won't work either, even if theTextFields do not contain quotes... This is an update query, not a query returning aResultSet.
– fabian
Nov 24 '18 at 15:26
add a comment |
At least two issues here:
UPDATEstatements do not return the updated row (would be nice if they did...) So you cannot expect to get aResultSetfrom an update statement. Had you been usingexecuteUpdate()instead ofexecuteQuery()this would have been easier to spot.
Instead of quoting the parameters when forming the SQL string, it is better to use a
PreparedStatement. I.e., use something like:
String sql = "UPDATE " + SupermarketDB + " SET ProductName=?, Barcode=?, Category=?, SubCategory=?, TotalStock=? Where ID=?");
try(Connection conn = SqliteConnection.Connector();
PreparedStatement stmt = conn.prepareStatement(sql)) {
int i = 0;
stmt.setString(++i, ProductName1.getText());
..
stmt.setString(++i, ID2.getText());
stmt.executeUpdate();
}
The above also ensures that the connection is properly closed.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458994%2fsql-not-updating-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You did mistake in query statement you put comma before where close. Just delete that comma
ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName=" + ProductName1.getText() + ", Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock=" + TotalStock1.getText() + " Where ID=" + ID2.getText() + "");
paste this code
This won't work either, even if theTextFields do not contain quotes... This is an update query, not a query returning aResultSet.
– fabian
Nov 24 '18 at 15:26
add a comment |
You did mistake in query statement you put comma before where close. Just delete that comma
ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName=" + ProductName1.getText() + ", Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock=" + TotalStock1.getText() + " Where ID=" + ID2.getText() + "");
paste this code
This won't work either, even if theTextFields do not contain quotes... This is an update query, not a query returning aResultSet.
– fabian
Nov 24 '18 at 15:26
add a comment |
You did mistake in query statement you put comma before where close. Just delete that comma
ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName=" + ProductName1.getText() + ", Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock=" + TotalStock1.getText() + " Where ID=" + ID2.getText() + "");
paste this code
You did mistake in query statement you put comma before where close. Just delete that comma
ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName=" + ProductName1.getText() + ", Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock=" + TotalStock1.getText() + " Where ID=" + ID2.getText() + "");
paste this code
edited Nov 24 '18 at 15:42
answered Nov 24 '18 at 14:46
Varun_AmbhireVarun_Ambhire
93
93
This won't work either, even if theTextFields do not contain quotes... This is an update query, not a query returning aResultSet.
– fabian
Nov 24 '18 at 15:26
add a comment |
This won't work either, even if theTextFields do not contain quotes... This is an update query, not a query returning aResultSet.
– fabian
Nov 24 '18 at 15:26
This won't work either, even if the
TextFields do not contain quotes... This is an update query, not a query returning a ResultSet.– fabian
Nov 24 '18 at 15:26
This won't work either, even if the
TextFields do not contain quotes... This is an update query, not a query returning a ResultSet.– fabian
Nov 24 '18 at 15:26
add a comment |
At least two issues here:
UPDATEstatements do not return the updated row (would be nice if they did...) So you cannot expect to get aResultSetfrom an update statement. Had you been usingexecuteUpdate()instead ofexecuteQuery()this would have been easier to spot.
Instead of quoting the parameters when forming the SQL string, it is better to use a
PreparedStatement. I.e., use something like:
String sql = "UPDATE " + SupermarketDB + " SET ProductName=?, Barcode=?, Category=?, SubCategory=?, TotalStock=? Where ID=?");
try(Connection conn = SqliteConnection.Connector();
PreparedStatement stmt = conn.prepareStatement(sql)) {
int i = 0;
stmt.setString(++i, ProductName1.getText());
..
stmt.setString(++i, ID2.getText());
stmt.executeUpdate();
}
The above also ensures that the connection is properly closed.
add a comment |
At least two issues here:
UPDATEstatements do not return the updated row (would be nice if they did...) So you cannot expect to get aResultSetfrom an update statement. Had you been usingexecuteUpdate()instead ofexecuteQuery()this would have been easier to spot.
Instead of quoting the parameters when forming the SQL string, it is better to use a
PreparedStatement. I.e., use something like:
String sql = "UPDATE " + SupermarketDB + " SET ProductName=?, Barcode=?, Category=?, SubCategory=?, TotalStock=? Where ID=?");
try(Connection conn = SqliteConnection.Connector();
PreparedStatement stmt = conn.prepareStatement(sql)) {
int i = 0;
stmt.setString(++i, ProductName1.getText());
..
stmt.setString(++i, ID2.getText());
stmt.executeUpdate();
}
The above also ensures that the connection is properly closed.
add a comment |
At least two issues here:
UPDATEstatements do not return the updated row (would be nice if they did...) So you cannot expect to get aResultSetfrom an update statement. Had you been usingexecuteUpdate()instead ofexecuteQuery()this would have been easier to spot.
Instead of quoting the parameters when forming the SQL string, it is better to use a
PreparedStatement. I.e., use something like:
String sql = "UPDATE " + SupermarketDB + " SET ProductName=?, Barcode=?, Category=?, SubCategory=?, TotalStock=? Where ID=?");
try(Connection conn = SqliteConnection.Connector();
PreparedStatement stmt = conn.prepareStatement(sql)) {
int i = 0;
stmt.setString(++i, ProductName1.getText());
..
stmt.setString(++i, ID2.getText());
stmt.executeUpdate();
}
The above also ensures that the connection is properly closed.
At least two issues here:
UPDATEstatements do not return the updated row (would be nice if they did...) So you cannot expect to get aResultSetfrom an update statement. Had you been usingexecuteUpdate()instead ofexecuteQuery()this would have been easier to spot.
Instead of quoting the parameters when forming the SQL string, it is better to use a
PreparedStatement. I.e., use something like:
String sql = "UPDATE " + SupermarketDB + " SET ProductName=?, Barcode=?, Category=?, SubCategory=?, TotalStock=? Where ID=?");
try(Connection conn = SqliteConnection.Connector();
PreparedStatement stmt = conn.prepareStatement(sql)) {
int i = 0;
stmt.setString(++i, ProductName1.getText());
..
stmt.setString(++i, ID2.getText());
stmt.executeUpdate();
}
The above also ensures that the connection is properly closed.
answered Nov 24 '18 at 16:05
nimrodmnimrodm
17.7k64750
17.7k64750
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458994%2fsql-not-updating-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them
– kleopatra
Nov 24 '18 at 14:38
It's better to use a
PreparedStatementfor creating a query containing data you need to insert based on the gui input. Furthermore by leaving thecatchempty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least putex.printStackTrace();in there.) There are other issues though: There is a,just beforeWHEREand you useexecuteQueryfor a update query.– fabian
Nov 24 '18 at 15:24