在 Java 中使用 java.sql 包中的 SQL
在一次讲座中,我的教授给出了涉及数据库和 java.sql 包的几个操作的示例。 这些示例本应以 pdf 文件形式在线上传,但由于某种原因,我的 pdf 阅读器未显示所有函数的名称和类名称。
我想知道以下 PHP 函数在 Java 中的等效项:
mysql_connect
mysql_query
mysql_fetch_row
mysql_fetch_assoc
mysql_close
谢谢!
during a lecture my professor gave examples of several actions involving databases and the java.sql package. These examples were supposed to be uploaded online in a pdf file, but for some reason the names of all functions and class names aren't displaying with my pdf reader.
I would like to know the equilavents of the following PHP functions in Java:
mysql_connect
mysql_query
mysql_fetch_row
mysql_fetch_assoc
mysql_close
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您查阅适合您所使用版本的 Java API 文档(我使用的是 JDK 1.5,因此它是 http://java.sun.com/j2se/1.5.0/docs/api/)然后点击java.sql,可以看到所有Java JDBC访问的类。
基本上,您使用 DriverManager 创建到数据库的新连接,并使用 Connection.prepareStatement、PreparedStatement.execute() 和PreparedStatement.executeQuery() 执行查询,并使用 ResultSet.next() 循环生成的 ResultSet 并提取结果使用 ResultSet.getXXXXX。
If you consult the Java API docs appropriate for the version you're using (I'm using JDK 1.5, so it's http://java.sun.com/j2se/1.5.0/docs/api/) and click on java.sql, you can see all the classes for Java JDBC access.
Basically, you create a new Connection to a database with DriverManager, and do a query with Connection.prepareStatement, PreparedStatement.execute() and PreparedStatement.executeQuery() and loop through the resultant ResultSet with ResultSet.next() and pull the results out with ResultSet.getXXXXX.
如果您刚刚开始使用 JDBC,请考虑学习 Sun 的教程:http://java.sun.com/docs/books/tutorial/jdbc/basics/
If you're just getting started with JDBC, consider working your way through Sun's tutorial at: http://java.sun.com/docs/books/tutorial/jdbc/basics/
直接使用 JDBC (java.sql) 非常冗长且容易出错,尤其是对于初学者来说,因为您需要手动执行非常重复的步骤,并“最终”关闭如此多的数据库对象(连接、语句、结果集)。
如果您不介意引入额外的依赖项,Apache Commons 有一个不错的小包装程序包,名为 DbUtils这使得运行查询和更新变得容易(同时仍然停留在 SQL 级别,而不是进入更高抽象级别的对象关系映射器)。
Working directly with JDBC (java.sql) is verbose and error-prone, especially for beginners, because you need to manually do very repetitive steps, and "finally" close so many database objects (Connections, Statements, ResultSets).
If you do not mind pulling in an extra dependency, Apache Commons have a nice little wrapper package called DbUtils that makes it easy to run queries and updates (while still staying at the SQL level, as opposed to object-relational mappers that go to a higher level of abstraction).