0

I have created a simple Java connection script in Java to connect to a database shown below.

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class dbconn {

public static void main(String[] args) throws ClassNotFoundException
{

Class.forName("org.sqlite.JDBC");

Connection connection = null;  
try
{
  // create a database connection
  connection = DriverManager.getConnection("jdbc:sqlite:C:/Program Files (x86)/Spiceworks/db/spiceworks_prod.db");
  Statement statement = connection.createStatement();
  statement.setQueryTimeout(30);  // set timeout to 30 sec.                  
}
catch(SQLException e)
{
  // if the error message is "out of memory", 
  // it probably means no database file is found
  System.err.println(e.getMessage());
}
finally
{
  try
  {
    if(connection != null)
        connection.close();

  }
  catch(SQLException e)
  {
    // connection close failed.
    System.err.println(e);
  }
}
}
}

Now I have tested this connection via some SQL queries inside the dbconn script and it works.

However my question is, how would Icall this instance of the database into another form in the same project to preform the same SQL queries which are:

 ResultSet resultSet = null;  

  resultSet = statement.executeQuery("SELECT * FROM users");  
     while (resultSet.next()) 
     {  
         System.out.println("EMPLOYEE Email: " + resultSet.getString("email"));  
     }
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
Silentdarkness
  • 461
  • 1
  • 10
  • 30
  • 2
    Your question isn't clear to me. If you want to use the same Connection object in other class, you can wrap this code inside a `static` function which returns the object and then call it in other class. – Shashank Kadne Jan 16 '13 at 07:27
  • Can ypu, please, give us some more info on your project? At first glance, will it work if you make connection object, a static variable? – py_script Jan 16 '13 at 07:28
  • my project is a ticket management system, using the spiceworks ticketing and management system database and relay it through another interface. The database connection i have used above i want to use across all my classes. is there no way of using the new dbconn(); statement or dbconn conn = new dbconn(); to do this? – Silentdarkness Jan 16 '13 at 07:31

1 Answers1

3

You can retain and reuse the Connection, saving it probably in some static field. If you access it from multiple threads, SQLite must work in the Serialized mode. You must have the code somewhere to re-establish the connection if it has been lost for some reason.

If the use of Connection is not heavy, you can also have some method that opens it and close when no longer needed, better inside the finally block.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • Understood, but that doesn't really help me much with regards to using the database connection in multiple classes so that i don't have to recode it each time causing messy code. That was the purpose of coding the dbconn script externally to each java class. :) – Silentdarkness Jan 16 '13 at 07:37