views:

281

answers:

3

Hello, 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!

+4  A: 

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.

Paul Tomblin
A: 

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

Thilo
+1  A: 

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/

Bryan Pendleton