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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

                         

» View all related tutorials
Related Tags: c jsp class constructor object io struct method sed const value name return declaration ai like js define example constructors

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.