Hi
friends and student
i was thinking from a long time how can i connect jdk1.3 to database.
Its very exciting and easy... Plz do following steps
1. Make a database in Ms Access
2. My Database name is bsw and table named stu(name,city)
3. Open Control panel>Administor>Data Sources(ODBC)
4.Select Ms Access (*.mdb) > Add
5. Write appropriate name of your ODBC Name
6. Select Database from select button
7. Paste this code,compile run and enjoy your database Connectivity
import java.sql.*;
import java.io.*;
class con2test
{
public static void main(String[] args)
{
// change this to whatever your DSN is
String dataSourceName = "mdbTEST"; //mdbTest is name which u hv mention in odbc data source name window
String dbURL = "jdbc:odbc:" + dataSourceName;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbURL, "","");
String nm;
DataInputStream da=new DataInputStream(System.in);
System.out.print("Enter name ");
nm=da.readLine();
System.out.print("Enter address ");
address=da.readLine();
Statement s = con.createStatement();
s.executeUpdate("insert into user
values('"+nm+"','"+address+"')");
con.close();
System.out.println("Saved");
}
catch (Exception err)
{
System.out.println( "Error: " + err );
}
}
}
wow sir kya bat hai
ReplyDelete