Blog

Update records from database using JDBC

Posted by Satyapriya Nayak Blogs | Java Sep 24, 2011
In this blog we will know how to update records from database using JDBC (Java Database Connectivity) in console window.
Download Files: Update.rar

In this blog we will know how to update records from database using 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

 

Table Creation

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

 

Example:- To update record in a table

 

/*To update record in a table by using Statement*/

import java.sql.*;

import java.util.*;

public class update

{

public static void main(String args[]) throws Exception

{

Scanner sc=new Scanner(System.in);

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");

Statement stmt=con.createStatement();

System.out.print("Enter the Employee number: ");

int x=sc.nextInt();

System.out.print("Enter the new salary : ");

int z=sc.nextInt();

System.out.print("Enter the Employee name: ");

String y=sc.next();

String sql="update employee set sal="+z+",empname='"+y+"' where empno="+x;

int no=stmt.executeUpdate(sql);

if(no>0)

System.out.println(no+" Records Successfully Updated");

else

System.out.println("Invalid employee number");

con.close();

}

}

 

Compile

Javac update.java

Java update

 

post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts