Step 1 :Open Control Panel -à settings-àselect Administrative tools.
Step 2 : Select ODBC Datasource.
Step 3 : Select System DSN then click on Add button.
Step 4 : Select Driver for Microsoft Access Database Then click on Finish
button.
Step 5: Select the Datasource name from local directory.
Step 6 : Select the Table.
Step 7 : Driver is Register for your Table.
Example
1 :Create Table into database.
packagejdbc_database;
importjava.sql.*;
public class CreateTable
{
public static void main(String[] args)
{
try {
//Load Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Create Connection with Data Source Name : HOD_DATA
Connection con =
DriverManager.getConnection("jdbc:odbc:HOD_DATA");
//
Create Statement
Statement s = con.createStatement();
//Create Table into database
String query = "create table stu_data";
// Execute Query
s.execute(query);
System.out.println("Table Created
Successfully...!");
s.close();
con.close();
}
catch (Exception e)
{
System.out.println("Exception : "+e);
}
}
}
Output
:
Table Created Successfully...!
Example 2 :Write a Program to insert data into database using
Jdbc.
packagenewpackage;
importjava.sql.*;
public class newclass
{
public static void main(string args[])
{
try
{
class.forname("sun.jdbc.odbc.jdbcodbcdriver");
connection con=
drivermanager.getconnection("jdbc:odbc:database31");
statement s=con.createstatement();
string query="insert into stu_data(id,name,no) values
(10,'harsh',12433)";
s.execute(query);
string query1="select
* from stu_data";
s.execute(query1);
resultsetrs= s.getresultset();
while(rs.next())
{
system.out.print(" , name
:"+rs.getstring("name"));
system.out.println(" , no :"+rs.getint("no"));
}
s.close();
con.close();
}
catch(exception e)
{
system.out.print("
exception : "+ e);
}
}
}
Output :
No comments:
Post a Comment