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

Run TypeScript compiler from Java
calling a dll file in a Java EE application on linux
Do entity classes always need IDs?
Database access in struts 2
Split string based on regex but keep delimiters
Running a jar from within another jar
How do I make an int appear in LogCat in Eclipse with Java/Android?
Layout labels from left to right
Result is not random in Android
Eclipse: Dependencies for .war generation
Correct use of Singleton Pattern
Intent is not able to run
NodeJS TCP Server, onData small chunks
Flipping a multidimensional array java [closed]
How can i get device, manufacturer and model in a device that support J2ME? [closed]
Most efficient way of checking if Date object and Calendar object are in the same month

Categories

HOME
cakephp
deployment
cookies
activiti
bpmn
maven-3
amazon-ecs
jsrender
ebean
contact
node-pdfkit
kentor-authservices
modx-revolution
windows-azure-storage
spring-tool-suite
circuit
iron-router
decimal
vaadin7
transformation
openedx
introduction
plunker
autoconf
physics-engine
css-animations
cultureinfo
jndi
one-to-many
sparse-matrix
react-css-modules
brunch
social-media
fifo
windowbuilder
javascriptcore
asset-pipeline
openoffice.org
android-nestedscrollview
mmenu
dosbox
restlet
x11-forwarding
splice
lumberjack
greenrobot-eventbus
cookiecutter-django
mesos-chronos
eclipse-scout
sql-server-agent
pdf-reactor
gulp-sourcemaps
dynamic-reports
core-plot
botbuilder
midl
wso2carbon
termination
zip4j
logparser
sage-one
integrity
lowpass-filter
pearson
nbconvert
orthogonal
pycaffe
static-ip-address
radtreelist
holder.js
bluegiga
rhino-servicebus
r-tree
pundit
angular-leaflet-directive
sankey-diagram
processmodel
dundas
apache-commons-net
nsmutabledictionary
formatjs
sorl-thumbnail
typekit
jscript.net
document-classification
article
sgen
uitouch
dml
navigationservice
p4java
phpthumb
inbox
eventual-consistency
django-nonrel
pushbackinputstream
sqlperformance
comaddin
type-equivalence
android-hardware
e4x
quickdialog
gdata-api
online-compilation
punbb
subscript
jmock
h.323
createwindow
digest-authentication
dmx512
icanhaz.js
vc90
ext3
meego
memory-size
ti-dsp
simpletest
wsdl.exe

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