As expected, the utilSetConnection() sets the mysql connection. Internally, the database user name, password and database name are hard coded. The return from utilSetConnection is a Connection handle. Once defined in static void main () the Connection handle can be passed to other methods, such as the utilDisConnect(conn).
The utilDisConnect() simply closes the Connection handle as conn.close(). That's it. Well other than the conn.close() has to be in a try / catch exception type processing.
Am I happy with the method names, well, not that much. The names are easily changeable to become more consistant. Also, the consideration to having the utilSetConnection() accept three arguments. These would be three String datatypes, name, password and database name.
As other methods are coded, these would be an insert, delete, select methods, the Connection handle will be passed in along with a table name, particular mysql statement. In the case of the select * from statement, the data will need to be returned into a vector list or to a text file. Since that will require additional thinking, that is on hold for a bit.
This is the disconnection method.
public int utilDisConnect(Connection conn) {
try{
conn.close();
}
catch (Exception e){
System.err.println ("Cannot disconnect to server");
return (1);
}
System.out.println ("Disconnected");
return (0);
}
The test driver needs to include this statement: import com.mysql.jdbc.Connection;
Then to use:
Connection lconn;
lconn=(Connection) myu.utilSetConnection("frank");
myu.utilDisConnect(lconn);
Netbeans took care of casting the (Connection) as shown above. Not sure if that is standard JAVA practice. Having to review lots of source code, these cast's could get out of hand.
No comments:
Post a Comment