The Artima Developer Community
Sponsored Link

Java Answers Forum
Connection Closing Problem

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Muhammad amjad

Posts: 1
Nickname: amjad
Registered: Feb, 2003

Connection Closing Problem Posted: Feb 1, 2003 3:43 AM
Reply to this message Reply
Advertisement
I'm new in java and JDBC but I?m trying to make a database connection to MS Access2000 with the following code (see after a few line of my question) i'm trying to make two methods one will create a new connection and second is for closing the already opened connection but second method (connection closing method)isn?t working properly I want 2 execute if part of the code but java execute else part of the code what is wrong with this code

Can anybody help me about the following code I?m copying each line of my code of MyFirstConnection dot java file which is following. is there a better way to do this code this?

import java.sql.*;

public class MyFIrstConnection
{
static Connection conn;
public static Connection getConnection(Connection connection){
if(connection == null){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("jdbc:odbc:nwind","","");
System.out.println("Connection Successeded");
return connection;
}catch(SQLException sqle){
System.out.println("Error while Connecting Database");
sqle.printStackTrace();
return null;

}catch(ClassNotFoundException cnfe){
System.out.println("Error while Loading Driver");
cnfe.printStackTrace();
return null;
}
}
return connection;
}

public static boolean closeConnection(Connection connection){
if(connection!=null){
try{
connection.close();
System.out.println("Connection Successfully Close");
return true;
}catch(SQLException sqle){
System.out.println("Error while Closing Connection");
sqle.printStackTrace();
return false;
}
}else{
System.out.println("connection is already closed");
return false;
}
}

public static void main(String a[]){
new MyFIrstConnection();
getConnection(conn);
try{
boolean cls = conn.isClosed();
System.out.println(cls);
}catch(Exception exp){
System.out.println("Error while Closing Connection");
}
closeConnection(conn);
}
}

Any help would be appreciated by me

Topic: CHANGE THE EDGE DEFINITION FOR THE GRAPH Previous Topic   Next Topic Topic: simple help needed

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use