views:

97

answers:

5

Since Connection and Statements are interfaces, interface methods will be abstract by default. How can we create Connection and Statement interfaces in our Java program while connecting to the database?

+2  A: 

Perhaps start at this JDBC tutorial.

Adam Goode
+2  A: 

Actually the implementation is provided by JDBC drivers. And the API give you a way to use the thing. Look at this example.

Adeel Ansari
+2  A: 

The implementations are provided by the JDBC driver for your database. e.g. for MySql you download the JDBC Driver for MySQL (Connector/J) and place the jar file on the classpath for your application.

Then you make a call to DriverManager (e.g.) this gives you a instance of a class which implements the Connection interface.

Connection c = DriverManager.getConnection("jdbc:mysql://[yourhost]/[nameofyourdb]?user=[username]&password=[password]");

Now you can get an instance of a class which implements the Statement interface from the Connection instance.

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM [yourtablename]");
....

For more examples just google around a bit (JDBC, java, [nameofyourdb])

For Connector/J there is a nice documentation herer

MySQL Connector/J

Connector/J Examples

jitter
A: 

Actually, the interfaces are given by Sun and as everyone know interfaces are set of rules that will be used for a specific nature of programming and for a consistent structure. So the database vendors take that interface and will result the API which doesn't ruin the prototype. As a result every vendors implemented with same names as that interface has, irrespective of its logic.

So developers need not to worry about its implementation and must know only about the interface names.

i2ijeya
A: 

I have worked on an abstraction for databases at work. Here is some example code

Database database = new Database(new SQL("host", "port","database", "username","password"));
List<tStd_M_lt> bases =  database.select(tStd_M_lt.class, new Where(tStd_M_lt.FIELDS.ITEM_ID.getDatabaseFieldName(), OPERATOR.LIKE, "?"), "%Base%");
for (tStd_M_lt base : bases)
{
 List<tShower_Base_Kit> kits = database.selectByForiegnKey(tShower_Base_Kit.class, base);
}

The project is called azirt.

Milhous