JDBC Execute Statement

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

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

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

Advertisements

Ads
Share on Google+Share on Google+

JDBC Execute Statement

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

Advertisements

Ads
 

Discuss: JDBC Execute Statement  

Post your Comment


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

Ads

 

Ads