JDBC-ODBC connectivity in JAVA(INSERT
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 just
created in the above step.(Login_db)
Figure 9
Click on next, 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 Connect.java
Program to Get data from a database
table of sql server:-
import java.*;
import java.sql.*;
import java.util.*;
class Connect{
private java.sql.Connection con =
null;
private ResultSet rs;
// Constructor:
public Connect(){}
private void FindEmployee() throws SQLException, ClassNotFoundException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url =
"jdbc:odbc:Login_DSN";
con =
DriverManager.getConnection(url, "", "");
Statement stmt =
con.createStatement();
rs = stmt.executeQuery("Select
uid, upass from user1");
while (rs.next())
{
//Column names:
System.out.println(rs.getString("uid") + " " +
rs.getString("upass"));
}
if(con != null)
con.close();
con = null;
}
catch (SQLException ex)
{
throw ex;
}
catch (ClassNotFoundException clex)
{
throw clex;
}
}
private void closeConnection(){
try{
if(con != null)
con.close();
con = null;
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
try
{
myDbTest.FindEmployee();
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("Error
Trace in getConnection() : " + ex.getMessage());
}
}
}
Now compile the above program with
javac and execute it with java
Javac Connect.java
Java Connect
When we open
our table in the sql server, we can find the above records are added in the table.
very helpful ma'am .
ReplyDelete