Results 1 to 5 of 5
Thread: Database declaration
- 10-23-2009, 02:23 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
- 10-23-2009, 02:26 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Google DAO layer.
- 12-07-2009, 07:10 PM #3
Member
- Join Date
- Dec 2009
- Location
- Edmonton, Alberta, Canada
- Posts
- 1
- Rep Power
- 0
These 3 classes from book JDBC: Practical Guide for Java Programmers
Book is by Gregory D. Speegle .. very good .. very flexible
import java.sql.*;
public class ConnectionJDBC {
public Connection makeConnection()
throws SQLException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
throw new SQLException("Unable to load driver class");
}
return DriverManager.getConnection("jdbc:odbc:jdbc_book") ;
}
public Connection makeConnection(String URL)
throws SQLException {
return DriverManager.getConnection(URL);
}
public Connection makeConnection(String DriverName, String URL)
throws SQLException {
try {
Class.forName(DriverName);
} catch (ClassNotFoundException e) {
throw new SQLException("Unable to load driver class");
}
return DriverManager.getConnection(URL);
}
public Connection makeConnection(String URL, String username,
String password)
throws SQLException {
return DriverManager.getConnection(URL,username,password) ;
}
public Connection makeConnection(String DriverName, String URL,
String username, String password)
throws SQLException {
try {
Class.forName(DriverName);
} catch (ClassNotFoundException e) {
throw new SQLException("Unable to load driver class");
}
return DriverManager.getConnection(URL,username,password) ;
}
public void closeConnection(Connection c, Statement s)
{
try {
if (s != null) s.close();
if (c != null) c.close();
} catch (SQLException sqlex) {}
}
public static void main(String args[]) {
ConnectionJDBC CJ = new ConnectionJDBC();
Connection dbConnect = null;
Statement dbStatement = null;
try {
switch (args.length) {
case 0 : dbConnect = CJ.makeConnection();
break;
case 1 : dbConnect = CJ.makeConnection(args[0]);
break;
case 2 : dbConnect = CJ.makeConnection(args[0],args[1]);
break;
case 3 : dbConnect = CJ.makeConnection(args[0],args[1],args[2]);
break;
case 4 : dbConnect = CJ.makeConnection(args[0],args[1],args[2],
args[3]);
break;
default :
System.out.println("Using the default driver");
dbConnect = CJ.makeConnection();
}
System.out.println("Made a connection!");
dbStatement = dbConnect.createStatement();
System.out.println("Made a statement!");
} catch (SQLException sqlex) {
System.out.println(sqlex.getMessage());
}
finally {
CJ.closeConnection(dbConnect,dbStatement);
System.out.println("Closed the connection.");
}
}
}
import java.sql.*;
public class SimpleJDBC extends ConnectionJDBC {
public static void main(String args[]) {
if (args.length != 1) {
System.out.println("Usage: java SimpleJDBC custid");
System.exit(1);
}
String query = "Select Title, Type " +
"from Orders O, Titles T, Tapes V " +
"where V.TapeId=O.TapeId and T.TitleId=V.TitleId and " +
"Status = 'O' and " +
"O.CustomerID= " + args[0];
SimpleJDBC J = new SimpleJDBC();
Connection dbConnect = null;
Statement dbStatement = null;
ResultSet dbRS = null;
try {
dbConnect = J.makeConnection(); //from ConnectionJDBC
dbStatement = dbConnect.createStatement();
dbRS = dbStatement.executeQuery(query);
J.presentResultSet(dbRS);
} catch (SQLException sqlex) {
System.out.println(sqlex.getMessage());
} finally {
J.closeConnection(dbConnect,dbStatement); //from ConnectionJDBC
}
}
public void presentResultSet(ResultSet rs)
throws SQLException {
if (!rs.next()) System.out.println("No records for customer");
else {
do {
System.out.println(rs.getString("Title") + ": " +
rs.getString("Type"));
} while (rs.next());
}
}
}
- 12-08-2009, 11:24 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,452
- Rep Power
- 16
Not terribly impressed.
He doesn't use bind variables (ie a PreparedStatement), and even worse he doesn't close the result set in the finally block. Assuming the result set will be closed on closing the statement is poor practice.
- 12-08-2009, 04:40 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 37
- Rep Power
- 0
Similar Threads
-
Declaration
By asifahmed in forum New To JavaReplies: 1Last Post: 04-05-2008, 05:38 AM -
public declaration ('undeclaring')
By Jadellll in forum New To JavaReplies: 2Last Post: 03-17-2008, 08:50 PM -
Array size declaration
By JT4NK3D in forum New To JavaReplies: 3Last Post: 01-18-2008, 10:37 PM -
JSP Declaration Directive
By Java Tip in forum Java TipReplies: 0Last Post: 12-10-2007, 05:42 PM -
Function declaration problem.
By snooze-g in forum Advanced JavaReplies: 3Last Post: 07-18-2007, 09:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks