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

How to display half or a quarter of an ImageView in Android?
is it possible to create a static object in a static method using Java
When using xml signatures the whole xml document is jumbled
Gaussian Filter without using ConvolveOp
Soap query database
How to remove numbers from TokenStream in Lucene?
Facing difficulty while using BufferedInputStream inplace of Scanner
Sign a java applet with multiple certificates
Activity thread access
Error converting a HTTP POST response to JSON
JTable ArrayIndexOutOfBoundsException
How to edit comment template and add type or function comment in JetBrains IntelliJ IDEA?
Java unbounded counted semaphore?
I want to open source my Google App Engine Java project, is there any auth info in the Eclipse project?
CDI - Injecting Classes at runtime
how to setup a call to method of mocked object in mockito without calling the original method itself

Categories

HOME
yii2
winforms
isabelle
path-finding
json.net
syntax
youtube-api-v3
google-project-tango
callback
ojdbc
directx
kibana-4
communication
metatrader4
numeral.js
visual-studio-2005
fallback
progressive-web-apps
highlight.js
text-rendering
oxyplot
scichart
lldb
tar
mustache.php
jndi
vlsi
buildbot
apache-commons-io
tooltipster
maxmind
assistant
replaceall
large-file-upload
bluestacks
data-manipulation
siesta-swift
google-qpx-express-api
mpmediaquery
http-digest
revolution-slider
automake
lto
libvpx
ansible-playbook
tasker
firebase-admin
spring-security-kerberos
nxlog
arena-simulation
android-cursor
pdfclown
arrow-keys
django-scheduler
migradoc
abcpdf9
setuptools
wdf
azure-sdk
savon
clang-static-analyzer
moveit
energy
pervasive-sql
srand
qdialog
security-testing
kcachegrind
graph-drawing
notify
bridge.net
cdt
gulp-less
document-classification
yorick
transcoding
concurrent-collections
typo3-neos
multiprocessor
ruby-datamapper
buster.js
gridcontrol
bitsharp
first-responder
automount
code-cleanup
custom-backend
boost-filesystem
javax.script
coda-slider
mozilla-prism
mirah
bespin
microsoft-virtualization
backcolor
privilege

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