Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining Java with SQL?

Tags:

java

sql

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.

like image 957
OVERTONE Avatar asked Oct 13 '09 10:10

OVERTONE


People also ask

Can you use SQL and Java together?

Most Java developers interested in employing SQL in their work will do so through JDBC, which allows Java to connect to SQL databases.

What is relation between Java and SQL?

Java is a high-level programming language that is preferred by most of the developers to develop different programs that can run on windows. On the other side, SQL is the query language that deals with databases such as update, delete, manage are some features for which programmers use the SQL.


2 Answers

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.

like image 80
Thomas Owens Avatar answered Oct 19 '22 18:10

Thomas Owens


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

like image 6
Alberto Zaccagni Avatar answered Oct 19 '22 18:10

Alberto Zaccagni