java


Is it necessary to close jdbc connection in localhost


I use static methods in a java project that i build, to make queries to a localhost mysql db.
Something like that:
public static void sqlQuery() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(localhost,root,password);
stmt = conn.prepareStatement(Some SQL);
rs = stmt.executeQuery();
} catch(Exception e) {
// Error Handling
} finally {
try { if (rs != null) rs.close(); } catch (Exception e) {};
try { if (stmt != null) stmt.close(); } catch (Exception e) {};
try { if (conn != null) conn.close(); } catch (Exception e) {};
}
}
My Question is if i can keep the conn variable without close it and reuse it again for another query.
No other app use this db and i can do that i want with only 1 connection..
P.S. Sorry for my bad english..

Connections are not thread-safe, so using only one is fraught with peril.You should use a connection pool, with a new logical connection for each logical activity.

You can reuse a connection in the same context. Once you have changed your context, you should close the former connection and reopen another connection.

You should always close resources like JDBC connection. If you don't close them, it creates a resource leak and it can slow down your program.

It is always advisable to close the connection, since its not a thread safe. It is also not advisable to keep the connection object as the singleton.


Related Links

Arrays comparation in java [duplicate]
Rsa C# Encryption and java decryption
can't update foreign Key
regex for nested quotes
passing null to a method not working [duplicate]
SQLite how to build DB schema for unknown column amount (multiple tables?)
How to populate Google Sheets via API in Java
Any hint to simplify this POST? (Java)
Write a java program using Scanner to help you decide what to do this weekend
Java Reading large files into byte array chunk by chunk
How to set Abitrary Units properly?
Spring RestTemplate exchange DELETE with body jdk 1.8
Get response from request PHP in Android
How do I structure my Android Studio project to avoid compilation issues with running the app with unit tests integrated
Using AsyncTask with SQLite Database
Hibernate Many-to-Many self join with extra column

Categories

HOME
gerrit
openstack
dictionary
kde
iterator
fft
octobercms
tizen-web-app
rubygems
angular-ui-bootstrap
windows-server
flyway4
ios-charts
cross-validation
elasticsearch-hadoop
communication
izpack
timeout
remote-access
carthage
django-simple-history
windows-7-x64
facebook-page
database-replication
normalizr
text-rendering
lucene.net
karma-jasmine
visual-composer
sylius
chromium-embedded
zurb-foundation-6
crystal-reports-2010
smb
microsoft-sync-framework
sparse-matrix
entitlements
opentype
file-rename
microsoft-chart-controls
bcd
.net-4.0
framemaker
textmate
simplexml
espeak
gesture
neuroscience
xenforo
webdriver-manager
eigenvalue
http-redirect
s
logfiles
angular-resource
dropbox-php
tizen-native-app
sqlclient
google-perftools
windows-iot-core-10
nbconvert
gnome-shell-extensions
firmata
color-picker
log4c
url-masking
simplewebrtc
fadeout
oauth2client
tform
freelancer.com-api
angular-leaflet-directive
sframe
appfabric-cache
unity5.2.3
dereference
lustre
xc16
winddk
asp.net-web-api-odata
flask-cors
terminfo
heisenbug
p4java
expected-exception
meteor-velocity
uv-mapping
farseer
ivyde
cdc
dataservice
eventual-consistency
chronoforms
isnullorempty
mercurial-server
table-footer
winbugs14
actionview
mongomapper
w3wp.exe
h.323
wise
bespin
webkit.net
ctp4
aquaticprime
post-build





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