(1)CREATE
TABLE :
create table empa
(id Int,
Name
varchar(200),
Salary
varchar(200)
);
(2)GO TO CPANEL
:
Go to cpanel & click on Administrative tools:
(3)Click on “Data Sources (ODBC)” & SELECT
SQL SERVER:
(3)then click on "add" to add new Data source:
(4)then select sql server:
(5)THEN ENTER DATA SOURCE
NAME & SERVER NAME
OF SQL SERVER 2008 :
(6)CONNECT TO SQL SERVER WITH AUTHENTICITY :
(7)SELECT YOUR DATABSE NAME:
FOR EXAMPLE “MASTER”:
(8)SELECT LANGUAGE
ENGLISH :
(9)CLICK ON FINISH:
(10)now write code for inserting values into "empa" table which we have created:
import java.sql.*;
public class jdbcinsert
{
public
static void main(String []args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:testdata");
Statement
stmt=con.createStatement();
stmt.executeUpdate("insert
into empa values(1,'amy',10000)");
System.out.println("Data
inserted successfully");
}
catch
(Exception e)
{
System.out.println(e);
}
}
}
(11))USING PREPARE STATEMENT TAKING INPUT FROM USERS:
import java.sql.*;
import java.util.*;
class jdbcprepare
{
public
static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:jayalaxmi");
PreparedStatement
pst=con.prepareStatement("insert into empa
values (?,?,?)");
Scanner
s=new Scanner(System.in);
System.out.println("Enter
Employee Id");
int
eid=s.nextInt();
System.out.println("Enter
Employee Name");
String
name=s.next();
System.out.println("Enter
Employee Salary");
int
sal=s.nextInt();
pst.setInt(1,eid);
pst.setString(2,name);
pst.setInt(3,sal);
pst.executeUpdate();
con.close();
}
catch
(Exception e)
{
System.out.println(e);
}
}
}
Sign up here with your email
ConversionConversion EmoticonEmoticon