.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Insert record into a table by using PreparedStatement

Posted By:satyapriyanayak       Posted Date: October 02, 2014    Points: 40    Category: Javascript    URL: http://www.dotnetspark.com  

In this Code Snippets we will know how to insert records in a table using PreparedStatement of JDBC (Java Database Connectivity) in console window.

Here we use Type-1 driver (JDBC-ODBC bridge)

Creation of dsn(database source name) for Oracle
Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system dsn tab-click add button-select a driver for which you want to set up data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name textbox-then click ok button.

Note: - Here Username=system, Password=pintu and Dsn name=dsn1

Ex:- To insert record into a table by using PreparedStatement

Table Creation

create table employee(empno int,empname varchar(50),sal int)

/*To insert record into a table by using PreparedStatement*/
import java.sql.*;
import java.util.*;
public class prepareDemo
public static void main(String args[]) throws Exception
Connection con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");
//step-1 -Reference creation of PreparedStatement
PreparedStatement pstmt=con.prepareStatement("insert into employee(empno,empname,sal) values(?,?,?)");
//step -2 reading from console and providing into sql
Scanner sc=new Scanner(System.in);
System.out.print("Enter the Employee Number : ");
int empno=sc.nextInt();
System.out.print("Enter the Employee Name : ");
String empname=sc.next();
System.out.print("Enter the Employee's salary : ");
int sal=sc.nextInt();
System.out.println("record inserted");


Javac prepareDemo.java
Java prepareDemo


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend