9

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.

Thomas Owens
117k100 gold badges322 silver badges439 bronze badges
asked Oct 13, 2009 at 10:42
3
  • excuse me for not being as literate with tags as you. Commented Oct 13, 2009 at 10:54
  • If Java cant talk SQL why in the world would anyone in business use it ? Commented Oct 13, 2009 at 11:41
  • @pstanton That link now redirects to some virus scareware, FYI. Commented Jun 13, 2014 at 2:31

6 Answers 6

19

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.

answered Oct 13, 2009 at 10:48
Sign up to request clarification or add additional context in comments.

1 Comment

+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.
6
  • 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.

answered Oct 13, 2009 at 10:49

Comments

3

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

See Sun's Java Tutorials: JDBC Database Access.

answered Oct 13, 2009 at 10:51

Comments

2

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"

answered Oct 13, 2009 at 10:49

Comments

2

This has probably been THE big middleware problem anyone has tried to solve in that industry in the recent past. Without any preference, more or less in the order of appearance, a couple of attempts to combine the two:

I agree with others. Before learning anything else, you should learn about JDBC. Here's an authoritative tutorial by Oracle:

http://docs.oracle.com/javase/tutorial/jdbc/

answered Sep 7, 2013 at 14:11

Comments

0

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.

answered Nov 11, 2009 at 14:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.