将 Java 与 SQL 结合使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1559440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combining Java with SQL?
提问by OVERTONE
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.
基本上,我的讲师告诉我,我们下学期有一个项目要让我们使用相互交织的 Java 和 SQL。
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.
我认为最合乎逻辑的事情是因为我没有组合经验,所以在 SQL 中创建表也是因为它也在数据库中使用,并通过 Java 调用它们。
Can anyone explain to me how this is possible or just the jist of how languages combine.
任何人都可以向我解释这是如何可能的,或者只是语言组合的方式。
采纳答案by Thomas Owens
What you will probably be doing is using JDBCto 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.
您可能要做的是使用JDBC来允许 Java 连接到 SQL 数据库。还有一些持久层,例如Hibernate,您可以使用它们在使用 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.
我认为 JDBC 教程应该足以让您入门。只是不要太早进入太远。慢慢来,并在出现问题时提出问题。
回答by user151019
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
这样做的方法涉及在 JDBC 上的Java Sun Java doc 中使用 JDBC(Java 数据库连接)
The way is as you say "create tables in SQL due too its use in databases and call them through java."
方法就像你说的“在 SQL 中创建表,因为它也在数据库中使用,并通过 java 调用它们”。
So you will need to start learing relational datbase theory - see books by e.g. C. Date - inluding "An Intorduction to Database Systems"
所以你需要开始学习关系数据库理论 - 参见例如 C. Date 的书籍 - 包括“数据库系统简介”
回答by Alberto Zaccagni
- 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:
您可以从这里开始:http: //java.sun.com/docs/books/tutorial/jdbc/index.html
遵循从链接中获取的简短示例,因此您可以大致了解这是关于什么的:
//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.
正如其他人指出的那样,添加 ORM 可能与此相去甚远,但我认为了解 JDBC 是一个好的开始。
回答by Jesper
The standard API to work with databases in Java is JDBC.
在 Java 中处理数据库的标准 API 是 JDBC。
See Sun's Java Tutorials: JDBC Database Access.
请参阅 Sun 的 Java 教程:JDBC 数据库访问。
回答by robertnl
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).
在 Internet 上查找“嵌入式 SQL”。然后你会发现这个主题很常见。您还将看到 SQL 可以与许多不同的语言(例如 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.
请注意,附加层(例如,作为 SQLJ 的 Java 类库)可能需要稍微不同的语法。我的建议是从 JDBC 上的普通 SQL 开始。
回答by Lukas Eder
This has probably been THEbig 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...
- JDBC(其他一切都建立在 JDBC 之上。几乎)
- SQLJ
- 可怕的 EJB 1.x 和 EJB 2.x EntityBeans
- 上联
- 联合开发署
- 休眠
- JPA(及其所有实现,包括 TopLink 和 Hibernate)
- MyBatis
- 约克
- 许多其他工具...
I agree with others. Before learning anything else, you should learn about JDBC. Here's an authoritative tutorial by Oracle:
我同意其他人的看法。在学习其他任何东西之前,您应该了解 JDBC。这是Oracle的权威教程: