JDBC-ODBC connectivity in JAVA(DELETE OPERATION)
To connect a
database with java language program we have to follow three steps :-
1)
Creation of database and table in SQL
server
2)
Creation of DSN
3)
Using this DSN in our java Program
1)
Creation of database and table in SQL
server :-
Step 1 :- Click
on start and find sql server management studio, it will ask for the sql server
name
Figure 1
Figure 2
Step 2 :-
Click on connect.
Step 3
:- you will get the following screen, in
which we have to select the database folder for creation of our database. Now
right click on it and get the another screen and select the new database
option.
Figure 3
Figure 4
Step 4 :-
Give the database name as in my example it is
Login_db or anything else what you want.
Step5 :- Select the Login_db folder and right click
on it to create a new table.
Figure 5
Step6 :- Specify the columns as uid and upass as
shown in the above figure and save the table with the name of user1. This is the end of part 1 of this tutorial.
2)
Creation of DSN:- for creation of DSN
select the administrative tool from the control panel and the ODBC Data Source
as in the following figure.
Figure 6
Now click on add and select the sql server from the list of
drivers.
Figure 7
Click on finish and provide the name of DSN and Servername
which is our sql server name.
Figure 8
Click on next --> next --> and then specify the database name which we had juts
created in the above step.(Login_db)
Figure 9
Click on next and finish. This is the end of Part 2 of this tutorial
and now the final step is using DSN in our java Program.
3)
Using this DSN in our java Program
with the name of delete.java
Program to insert new record in the
database table:-
Program to Delete a record from sql
server table:-
/*To delete record from a table by
using Statement*/
import java.sql.*;
import java.util.*;
public class delete
{
public static void main(String
args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Scanner sc=new Scanner(System.in);
Connection
con=DriverManager.getConnection("jdbc:odbc:Login_DSN","","");
Statement stmt=con.createStatement();
System.out.print("Enter user id
");
String lb=sc.nextLine();
String sql="delete from user1
where uid='"+lb+"'";
int no=stmt.executeUpdate(sql);
System.out.println(no +" Records
Successfully Deleted...");
con.close();
}}
Now compile the above program with
javac and execute it with java
Javac delete.java
Java delete
When we open
our table in the sql server, we can find the above records are added in the table.
No comments:
Post a Comment