Create table and insert data by sql query

This is detailed java program to connect java application and execute sql query like create table in mysql database, insert some values and retrieve values from the table.

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

Create table and insert data by sql query

     

This is detailed java program to connect java application and execute sql query like create table in mysql database, insert some values and retrieve values from the table.

Before running this java code you need mysql-connector-java-3.1.6-bin.jar file in the jdk1.6.0_01\lib and set class path to this  file.

ExecuteSqlQuery.java

 /**
 * Execute sql queries with java application.
 */
import java.sql.*;
class ExecuteSqlQuery {
 public static void main(String[] args) {
  try {
  /* Create string of connection url within specified 
format with machine name, port number and database
name. Here machine name id localhost and database
 name is usermaster. */
  String connectionURL = "jdbc:mysql://localhost:3306/usermaster";
  // declare a connection by using Connection interface 
  Connection connection = null;
  // declare object of Statement interface that uses for executing
 sql statements.
  Statement statement = null;
  // declare a resultset that uses as a table for output data from 
tha table.
  ResultSet rs = null;
  int updateQuery = 0;
  // Load JDBC driver "com.mysql.jdbc.Driver".
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  /* Create a connection by using getConnection() method that takes
 parameters of string type connection url, user name and password 
to connect to database. */
  connection = DriverManager.getConnection(connectionURL, "root", "root");
  
  /* createStatement() is used for create statement object that is
 used for sending sql statements to the specified database. */
  statement = connection.createStatement();
  // sql query of string type to create a data base.
  String QueryString = "CREATE TABLE user_master1(User_Id INTEGER NOT
 NULL AUTO_INCREMENT, " + "User_Name VARCHAR(25), UserId VARCHAR(20),
 User_Pwd VARCHAR(15), primary key(User_Id))";
  updateQuery = statement.executeUpdate(QueryString);
  // sql query to insert values in the specified table.
  QueryString = "INSERT INTO user_master1(User_Name,UserId,User_Pwd)
 VALUES ('Mahendra',"+ "'mahendra25','1213456')";
  updateQuery = statement.executeUpdate(QueryString);
  if (updateQuery != 0) {
  System.out.println("table is created successfully and " + updateQuery
 + " row is inserted.");
  }
  // sql query to retrieve values from the specified table.  
  QueryString = "SELECT * from user_master1";
  rs = statement.executeQuery(QueryString);
  while (rs.next()) {
 System.out.println(rs.getInt(1) + " " + rs.getString(2) + "  " 
+ rs.getString(3) + "  "+rs.getString(4)+"\n");
 }
 // close all the connections.
  rs.close();
  statement.close();
  connection.close();
  } 
   catch (Exception ex) {
  System.out.println("Unable to connect to batabase.");
  }
  }
}

Output of the program

Download source code

Advertisements

Ads
Share on Google+Share on Google+

Create table and insert data by sql query

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

Advertisements

 

Discuss: Create table and insert data by sql query   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
kamal
June 1, 2012
Thank you

Thanks a Lot for this Code It was really help full to me..
rajhod
December 20, 2012
question

program

Ads

 

Ads