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();
}
}