Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Using Select Statements in JDBC 
 

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

 

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

                         

» View all related tutorials
Related Tags: sql mysql c ant jdbc help tutorial ria this for to drive e des in sta m nt wan j

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

dunna

Posted by banda on Thursday, 12.4.08 @ 23:51pm | #82417

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.