Home Jdbc JDBC Execute Statement

Related Tutorials


 
 

Share on Google+Share on Google+

JDBC Execute Statement

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 17, 2008

Related Tutorials

Discuss: JDBC Execute Statement  

Post your Comment


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