Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible to separate the code into 3 part. I mean 3 different file. First part for connecting to SQL server, second part for manipulating database and third part is for closing the connection. The code is just an example.

Java
/**
 * CloseConnection.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class CloseConnection {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Load Microsoft JDBC Driver 1.0
      Class.forName(
        "com.microsoft.sqlserver.jdbc.SQLServerDriver");

// Obtaining a connection to SQL Server
      con = DriverManager.getConnection(
          "jdbc:sqlserver://localhost:1269;"
        + "user=sa;password=HerongYang");

// Connection is ready to use
      DatabaseMetaData meta = con.getMetaData();
      System.out.println("Server name: " 
        + meta.getDatabaseProductName());
      System.out.println("Server version: "
        + meta.getDatabaseProductVersion());

// Closing the connection
      con.close();
      if (con.isClosed()) 
        System.out.println("Connection closed.");

    } catch (java.lang.ClassNotFoundException e) {
      System.err.println("ClassNotFoundException: "
        +e.getMessage());
    } catch (SQLException e) {
      System.err.println("SQLException: "
        +e.getMessage());
    }
  }
}
Posted

Extend a base class, which provides the stuff that is shared by all 3 parts (EDIT: like the Connection "conn")
But why split that? Looks fine to me like it is.
 
Share this answer
 
v2
Comments
Valentine1993 17-Apr-13 8:45am    
1) Because in order to manipulate any data from a database, first and last we need to establish and close connection. And it's repeating task. I don't want to see repeated stuff again and again in my code. Extending a base class works? Because manipulation of data is different on every task. Hmmmm.....

2) Is it common for application to frequently connect and close connection just to perform a single task. Example: Select* FROM state. Or do programmer, open a connection on the moment the program is executed then close only when the program has quit?
TorstenH. 17-Apr-13 8:55am    
depends. Most programms do keep the connection but init an own session due for each request due to thread-handling the request.

I would make own methods and keep it in one class.

If you have repeating reuqests, you can also make up some predefined request methods. That would help you and make the code better maintainable.
Valentine1993 17-Apr-13 9:32am    
ok. thanks for the reply. Inheritance seems working for me. (:
In java, the only way to split source code in different files is if the code exists in different classes. You can write a base class with certain behavior that is extended in a derived class (or classes).
 
Share this answer
 
Comments
Valentine1993 18-Apr-13 0:44am    
Ya ya. I just did it with inheritance. Thanks guys!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900