0

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();
            }
}
2
  • It's not clear what your question is. Whether that's a good approach? That's quite subjective, but at the least there's no need to call Class.forName every time you need a connection (once is sufficient), and it appears to eat exceptions which makes using it a pain. Whether the code is correct? No, it won't even compile. Can you clean things up a bit and clarify what you're asking? Commented Dec 21, 2009 at 11:03
  • where can I put the Class.forName so would not need to call eat each time (I'm going to create many instances of this SqlConnection) Commented Dec 21, 2009 at 11:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
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.
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.