views:

182

answers:

1

I'm trying to work with java data access, so this with what I came up with:
Tell me what do you think about this
(It's a little bit like SqlConnection from C#)

import java.sql.*;
public class SqlConnection {

    Connection connection;

    public Connection GetConnection() {return connection;}

    public void Connect(String cs, String user, String pass)
    {
      try {
       Class.forName("net.sourceforge.jtds.jdbc.Driver");  
       connection = DriverManager.getConnection(cs, user, pass);
       System.out.println("connected");

        } catch (Exception e) {   
       e.printStackTrace();
      } 
    }

    public void Disconnect() 
    {
     if(connection != null){
      try {
       connection.close();
      } catch (SQLException e){
       e.printStackTrace();
      }
      connection = null;
     }
    }
}

I think I'm going to use it like this

public class MyDAL {
public Foo[] GetFoos()
{
SqlConnection conn = new SqlConnection();
PreparedStatement comm = null;
ResultSet rs = null;
      try {
       conn.Connect(cs, user, pass);    
       comm = conn.GetConnection()
       .prepareStatement("SELECT * FROM foos");
       rs = comm.executeQuery();    
       while (rs.next()) {     
        String s = rs.getString("name");
        ...
        }         
       } catch (Exception e) {   
      e.printStackTrace();
       }
       finally
       {
                               DBUtil.Close(rs);
                               DBUtil.Close(comm);
              conn.Disconnect();
       }
}
+1  A: 

If you want to learn java JDBC this is fine. I'd not use capitalized method names (those are typically C#, not java) and use a logger (mostly log4j but pick your favorite flavour) instead of stdout and stderr.

Catching Exception is really bad practice; best catch all specific exceptions first so you can make a sensible error message. You can always catch Exception after that, if you really think you can get your software to function.

In this particular case I'd catch specific exceptions in Connect and throw my home build CannotConnectException(originalException). This exception is not uncommon but also not a normal flow, typical for a checked exception.

If you want to do something scalable then at least use Apache DBCP, a database connection pool in stead.

If you want to learn something used in most Java database-related applications, go for Hibernate, or EJB3 (also done by Hibernate). You really don't see too much raw JDBC applications in the wild anymore.

extraneon
my application needs to execute only 4 stored procedures from the database that are going to return a row with 3 columns of data, but it needs to do it really fast
Omu
How fast is fast? I think that the speed is more dependent on your SQL server than on the java code which calls it. Efficiency of the stored procedure and the indexing of the tables it uses is normally more important. The code you show isn't slow, but neither is hibernate or anything else.
extraneon
well, yes, but why should I bother setting up hibernate or anything else if all i need is to execute 4 procedures that return 1 or 3 strings each
Omu
No reason if that is your only goal. I just read it as "I'm trying to do database stuff with Java, is this the way to go?" and not as a one-off script-like thing. I don't really see why you don't use the command line (mysql?) command or a script (python, perl) for this, which tend to be a lot simpler for the simple stuff.
extraneon