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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JDBC Execute Statement 
 

In this Tutorial we want to describe you a code that help you in understand to explain JDBC Execute Statement.

 

JDBC Execute Statement

                         

JDBC Execute Statement is used to execute SQL Statement, The Execute Statement accept SQL object as parameter and return you the result set from a database. In this Tutorial, the code illustrates an example from JDBC Execute Statement. The driver searches a table in the backend. Incase the we try to create a new table on the name of existing table. The output of code show you an Sql Exception, that table of this name already exist in the database of your backend.

The code include a class JdbcExecuteStatement.The class include a main method. Inside the main method, the list of steps to be carried out Loading  a driver by calling a class.forName( ) by passing driver class as argument. Once your driver is loaded, you connect your front end application in java to the backend database.

   DriverManager.getConnection ( ) : The Driver Manager Class call getConnection ( ),return you an connection object This built a connection between the url and database.con.createStatement ( ) : This provides you an sql object. This is done when you built a connection between Url and database, send  sql object to execute the query in the backend database.

   st.execute ( ) : This method is used to execute the sql query. This may return you a set of row into your table of database.

If there exists an exception in try block, the catch block subsequently catch and handle the exception.

On execution of the code, the println in program show you an exception indicates that created table already exist in database.

JdbcExecuteStatement.java

import java.sql.*;



public class JdbcExecuteStatement {



    public static void main(String args[]) {
~~`


        Connection con = null;

        Statement st = null;



        String url = "jdbc:mysql://localhost:3306/";

        String db = "komal";

        String driver = "com.mysql.jdbc.Driver";

        String user = "root";

        String pass = "root";



        try {

            Class.forName(driver);

            con = DriverManager.getConnection(url + db, user, pass);



            st = con.createStatement();



            String sql = "create table Abc(no integer,name varchar(10))";

            st.execute(sql);



        catch (Exception e) {

            System.out.println(e);

        }

    }

}
 
   

Output

java.sql.SQLException: Table 'abc' already exists

Download code

                         

» View all related tutorials
Related Tags: java sql mysql c database api ide jdbc data batch application io get vi exec state int bat this id

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 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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.