Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : J2EE : JDBC : Creating JDBC SQL Statements. Displaying Records from a MySQL Database
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

Creating JDBC SQL Statements. Displaying Records from a MySQL Database

by Faisal Khan.

Overview
In this tutorial following topics will be covered:

  • What are JDBC SQL Statements?
  • What are different types of JDBC SQL Statements?
  • How to create and execute JDBC SQL Statements?
  • Basics of creating databases and tables in MySQL.
  • How to display records from a table in MySQL?

This tutorial assumes that you've got MySQL and Connector/J JDBC driver properly setup on your system. If not have a look at following introductory articles/tutorials:

In the previous articles we learned that we obtain a java.sql.Connection object which represents a database connection from java.sql.DriverManager class. We learned that to properly connect to a database we need to have a properly installed JDBC driver, and we did that by installing Connector/J JDBC driver for MySQL. We also learned that after loading JDBC driver we have to correctly specify a database URL to DriverManager to obtain a connection.

We'll now move on to learn how to create and execute JDBC SQL Statements.

What are JDBC SQL Statements?
SQL stands for "Structured Query Language". It is a standard query language supported by almost all RDBMS vendors. You can use SQL to display (SELECT), add (INSERT), edit (UPDATE) and remove (DELETE) records from a table in a database.

A database server like MySQL contains 0 or more databases. A database then contains 0 or more different tables. The tables in turn contain 1 more fields. You add a record to a table by using the INSERT SQL statement. To display records from a table you use SELECT SQL query.

Following are some of the introductory articles/tutorials on SQL at Stardeveloper:

java.sql.Statement

Java is an object-oriented language. Everything in the World of Java is represented by an object. So the SQL queries that we learned above are also represented by a generic interface of type java.sql.Statement. All SQL statements are represented by this interface.

You use different methods provided by java.sql.Statement interface to retrieve (SELECT) or update (INSERT, UPDATE, DELETE) records in a database.

What are different types of JDBC SQL Statements?
There are 3 types of JDBC SQL Statements:

  • java.sql.Statement - Top most interface which provides basic methods useful for executing SELECT, INSERT, UPDATE and DELETE SQL statements.
  • java.sql.PreparedStatement - An enhanced verion of java.sql.Statement which allows precompiled queries with parameters. It is more efficient to use java.sql.PreparedStatement if you have to specify parameters to your SQL queries.
  • java.sql.CallableStatement - Allows you to execute stored procedures within a RDBMS which supports stored procedures (MySQL doesn't support stored procedures at the moment).

In this tutorial we'll be focusing our attention only on the java.sql.Statement object.

How to create and execute JDBC SQL Statements?
To create a JDBC SQL statement, we make use of java.sql.Connection.createConnection() method like this:

Connection con = null;
Statement st = null;

// Obtain connection here
st = con.createStatement();

Calling Connection.createStatement() gives you an empty JDBC Statement object. So how do you specify which query to use? Here is how you'd do it:

ResultSet rs = null;

rs = st.executeQuery("SELECT * FROM users");

We execute a SELECT SQL statement to retrieve all the records from the "users" table.

The Statement.executeQuery() method is used when you want to retrieve records from a database i.e. you are executing SELECT SQL statements. But what if you want to INSERT, UPDATE or DELETE records from a table? You then have to make use of Statement.executeUpdate() method like this:

int recordsUpdated;

recordsUpdated = st.executeUpdate("DELETE FROM users WHERE user_id = 1");

Above query deletes a user from the "users" tables where the user_id field's value is '1'. The number of records affected are returned by this method which we save in the recordsUpdated variable.


 ( 2 Remaining ) Next

Comments/Questions ( Threads: 9, Comments: 13 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Update statement in Java ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  2. mysql and java database connectivity
  3. about displaying records ( 1 Reply )
  4. null pointer exception ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  5. DriverManager
  6. Please help this case ( 1 Reply )
  7. JSP
  8. How to insert a jpeg
  9. How to insert a jpeg
  10. Error preverifying class javax.sql.ConnectionEvent ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.