java


Can't print information from SQL in Java


The user must choose a Resort ID from the table that is displayed and the make a booking. I can't seem to find my problem, I want to print the name of the Resort that they are making a booking at.
String x = jTextFieldID.getText();
Integer Resort = Integer.valueOf(x);
int resort = Integer.parseInt(x);
String sql = "SELECT RESORT_NAME FROM LouwDataBase.Resorts WHERE ID = "+Resort;
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, resort);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
String resortName = rs.getString("RESORT_NAME");
JOptionPane.showMessageDialog(null,
"You want to book at " + resortName);
}
The error is that sql is passed to Statement.executeQuery(String) too, instead of the PreparedStatement.executeQuery().
int resort = Integer.parseInt(x);
//String sql = "SELECT RESORT_NAME FROM LouwDataBase.Resorts WHERE ID = ?";
String sql = "SELECT RESORT_NAME FROM LouwDataBase.Resorts WHERE ID = " + resort;
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
//pstmt.setInt(1, resort);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
String resortName = rs.getString("RESORT_NAME");
JOptionPane.showMessageDialog(null,
"You want to book at " + resortName);
}
}
} catch (SQLException ex) {
Logger.getLogger(Booking.class.getName()).log(Level.SEVERE, null, ex);
}
Commented is the alternative usage of a prepared statement (as normally used).
Also you should close statement and result set, which can be done automatically with try-with-resources as above.
Oh, oversaw almost, that rs.next() must be called. (The others already mentioned.)
You have to use rs.next() :
ResultSet rs = pstmt.executeQuery(sql);
String resortName = "";
if(rs.next()){//<<-----------------------------
resortName = rs.getString("RESORT_NAME");
}
JOptionPane.showMessageDialog(null, "You want to book at "+resortName);
If you want to get many results you can use while(rs.next){...} instead.
Note? for a good practice, don't use upper letter in beginning for the name of your variables ResortName use this instead resortName
You need to test over the ResultSet result before trying to read from it:
if(rs.next()) {
String ResortName = rs.getString(1);
JOptionPane.showMessageDialog(null, "You want to book at "+ResortName);
}
And you can use getString(1) to get the RESORT_NAME, check ResultSet .getString(int index) method for further details.

Related Links

calculating image similarity of two “line drawing” images
Unrecognized option: -security in Tomcat
How can I add new array elements at the end of an array? [duplicate]
Excel VBA - Shell command file creation
Running domain's tests in multi-module project with #SpringApplicationConfiguration throws NoSuchBeanDefinitionException?
What's the difference between " and '? [duplicate]
Extracting Information from Java using ANTLR
Issues creating a 3d rectangular prism in JavaFX
Changing fetch to EAGER seems to fix unrelated session caching problems
How generate Tests with evosuite on Ubuntu
sending bytes from Raspberry Pi (using python) to a java android application over bluetooth
Why can not cast a double value to Byte object?
Multiple Inheritance Ambiguity with Interface
Convert JavaUtilList to NSArray?
I just started learning Maven. How does Eclipse build without it?
Google Guava Cache On App Engine

Categories

HOME
pdf
pycharm
angular-material
relay
react-router
routes
sharepoint-designer
sql-server-2016
indesign
directx
ssl-client-authentication
rascal
qt-creator
orchardcms
carthage
openrefine
code-review
crystal-reports-2008
alpine
java-3d
clickonce
scriptcs
mustache.php
lcd
crystal-reports-2010
android-widget
sparse-matrix
kannel
libssl
c++-amp
file-rename
preg-match
react-chartjs
javascriptcore
phonegap
stringtemplate
mmenu
force-layout
plsql-psp
io-redirection
file-format
qwerty
neuroscience
unobtrusive-validation
total-commander
smartcontracts
sqlclient
typed-lambda-calculus
r-forge
akka-cluster
sqldf
quartz-composer
visual-c++-2008
vmware-tools
sigabrt
xpath-1.0
reactive-banana
t4mvc
browser-bugs
drawbitmap
iiviewdeckcontroller
truevault
modalpopup
google-reader
doskey
mysql-error-1062
gnu-smalltalk
cdc
quartz-graphics
eventlistener
lcs
jelly
delphi-6
kyotocabinet
mhtml
automount
hosts-file
appconkit
nsmanagedobject
dotnethighcharts
assembly-loading
javax.script
winbugs14
actionview
datareader
telerik-scheduler
.nettiers
castle-monorail

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile