Using Select Statements in JDBC

The SELECT statement is used to select data from a table. For terminating a statement, semicolon is used.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

Using Select Statements in JDBC

  

The SELECT statement is used to select data from a table. For terminating a statement, semicolon is used. When we want to fetch the data from any table then we have to use SELECT statement. The Query can be written as below:

SELECT * FROM employee;

This Example SelectState.java is concern with Select Statements. You have to just create the connection with database and execute the Select Statement.  

SelectState.java

import java.sql.*;
public class SelectState {
 
public static void main(String a[])
  
{
  
Connection con = null;
  String url =
"jdbc:mysql://localhost:3306/";
  String dbName =
"vineej";
  String driver =
"com.mysql.jdbc.Driver";
  String userName =
"vineej";
  String password =
"no";
  
try {
 
Class.forName(driver).newInstance();
  con = DriverManager.getConnection(url+dbName,userName,password);
  Statement st = con.createStatement();
  ResultSet rs = st.executeQuery("select Name from Employee");
  
System.out.println("Results");
  
while( rs.next() ) {
   
String data = rs.getString(1);
    System.out.println( data );
  }
  st.close();
  }
 
catch( Exception e ) {
  
System.out.println(e.getMessage());
   e.printStackTrace();
   }
   }
}

Description of the program
In this program, we will learn how to retrieve the data from any table with the JDBC. Employee is the name of a table and Name is one of the column of table. By the SELECT Statement we can retrieve the data.

Description of Code
1.First in the program we import the java.sql package then define the SelectState class.

import java.sql.*;

2.Create a connection with MySql database.

con = DriverManager.getConnection(url+dbName,userName,password);

3.Create statement and then execute query with the SELECT statement.

Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select Name from Employee");

4.Finally got the result by the getString() method.

while( rs.next() ) {
String data = rs.getString(1);
System.out.println( data );
}

5. At the end, connection should be disconnected.

con.close();

6. printStackTrace() method

The method is used to show error messages. If there is any problem while execution then it throws the exception and prints the message.

Download Source Code

Advertisements

Ads
Share on Google+Share on Google+

Using Select Statements in JDBC

Posted on: June 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Using Select Statements in JDBC   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads