Insert Records Example Using JDBC


/*
Entering a New Row into a Table
This example enters a row containing a string, an integer, and a floating point number into the table called "mytable"
*/


package com.javaStuff.jdbc;


import java.sql.*;
/**
 * @author JavaStuff
 */
public class InsertNewRow
{
public static void main(String args[])throws SQLException, ClassNotFoundException {


//Get Connection
Connection con=prepareConnection(); 


// Obtain a Statement
Statement st=con.createStatement();

String query = "insert into mytable values ('JavaStuff', '36', '158.5')";

//Execute the Query
int count=st.executeUpdate(query);


System.out.println("Number of Rows Effected by this query = "+count);
}//main


public static Connection prepareConnection()throws SQLException, ClassNotFoundException {


String driverClassName="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@mysys:1521:JavaStuff";
String username="scott";
String password="tiger";

//Load driver class
Class.forName(driverClassName);


// Obtain a connection
return DriverManager.getConnection(url,username,password);
}//prepareConnection
}//class

Note : To execute the following example you can replace username and password with your actual user name and password.


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email