Home Jsp Execute SQL Queries with Java Application
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Execute SQL Queries with Java Application

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

Execute SQL Queries with Java Application

     

This is detailed java program to connect java application and execute queries like create table in mysql database, insert some values and retrieve values from the table. Before running this java code you need to copy a mysql connector jar file (mysql-connector-java-3.1.6-bin.jar) 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 the table.
  ResultSet rs = null;
  int updateQuery = 0;
  // Load JBBC 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

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: July 8, 2008

Related Tutorials

Ask Questions?    Discuss: Execute SQL Queries with Java Application   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
jenilyn
March 4, 2012
java to sql

i only have jdk1.7.0 and my microsoft sql server 2005 .. can u teach me how to connect java to sql please!
ankit
March 26, 2012
data

very good program
Kifyexs473
May 10, 2012
Thanks

Now I can start to do my software proyect =D Thank you!
Ellis
June 13, 2012
Connection Error

Hi, i tried your code. It could not connect to the database. Any suggestions? Thanks.
Mani kandan
June 29, 2012
feedback

satisfied
alekya
October 20, 2012
html form linking to database using java

i have a html form with four buttons like insert,delete,modify,search.So for insert button i have given submit value so that all those values will be stored in my sql.Now for the other three buttons there should be the same servlet program which i have written for insert button.Now how to change the values based on the button.So please do help me it is very urgent!!!!!
DMCA.com