开发者

Combining Java with 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 throu开发者_如何学运维gh Java.

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


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.


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


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

See Sun's Java Tutorials: JDBC Database Access.


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"


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:

  • JDBC (everything else is built upon JDBC. Almost)
  • SQLJ
  • The dreaded EJB 1.x and EJB 2.x EntityBeans
  • TopLink
  • JDO
  • Hibernate
  • JPA (and all of its implementations, including TopLink and Hibernate)
  • MyBatis
  • jOOQ
  • Lots of other tools...

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/


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜