tags:

views:

173

answers:

7

This isn't a code question for once, but it definitely has me confused.

Basically, my lecturer has told me that we have a project due next semester that involves us to use Java and SQL intertwined with each other.

I had no idea the combining of languages was even possible!

So my mind's really blown.

I've been searching around looking for examples of such code but no luck. So I thought I'd ask you guys.

I think the most logical thing to do since I have no experience with combining would be too create tables in SQL due too its use in databases and call them through Java.

Can anyone explain to me how this is possible or just the jist of how languages combine.

+8  A: 

What you will probably be doing is using JDBC to allow Java to connect to SQL databases. There are also persistence layers, such as Hibernate, that you can use to store and retrieve data in a database using Java.

I think the JDBC tutorials should be enough to get you started. Just don't get in too far over your head too early. Take your time and ask questions as they come up.

Thomas Owens
+1. Yes definitely start with JDBC so you gain an appreciation of how things work under the covers before looking into things like Spring or Hibernate. One tip - I'd advise writing a static utility method: void closeQuietly(ResultSet, Statement, Connection) to do any required tidy-up, as you have to do this yourself with JDBC.
Adamski
+2  A: 

Surely your course will have provided reading on this. Start there.

The way of doing it involves using JDBC (Java database connectivity) in Java Sun Java doc on JDBC

The way is as you say "create tables in SQL due too its use in databases and call them through java."

So you will need to start learing relational datbase theory - see books by e.g. C. Date - inluding "An Intorduction to Database Systems"

Mark
+2  A: 
  • Connect to a database
  • Do something interesting with it

You could start from here: http://java.sun.com/docs/books/tutorial/jdbc/index.html
Follows a brief example took from the link, so you can get a general grasp of what this is about:

//connect to the database
Connection con = DriverManager.getConnection("jdbc:myDriver:wombat","myLogin","myPassword");  
Statement stmt = con.createStatement();
//here is the query you will execute
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
    //rs contains the result of the query
    //with getters you can obtain column values
    int x = rs.getInt("a");
    String s = rs.getString("b");
    float f = rs.getFloat("c");
}

As others pointed out this could get far from this, adding ORM, but I think knowing what JDBC is is a good start.

Alberto Zaccagni
+2  A: 

The standard API to work with databases in Java is JDBC.

See Sun's Java Tutorials: JDBC Database Access.

Jesper
+1  A: 

I most definitely hope for you that your lecturer isn't thinking of this...

fvu
+1 Actually SQLJ is what I had in mind too at first... But I don't share the same dislike for it, I think it's more readable that standard JDBC, just never took off
Andrew from NZSG
@Andrew, the idea itself is quite OK, it's the lack of integration with common tools and the rest of the Java world that bothers me. Like you say, it just never took off.
fvu
A: 

Oracle Database can run java code http://www.developer.com/db/article.php/3337411/Oracle-and-Java-Stored-Procedures.htm

A: 

Look on the internet for "embedded SQL". Then you'll see that this subject is quite common. Also you'll see that SQL can be combined with many different languages (e.g. Python).

Please, notice that additional layers (e.g. a java class library as SQLJ) may require a slightly diffent syntax. My advice is to start with plain SQL over JDBC.

robertnl