|
Home
|
About Me
| Thoughts
| Tutorials | Projects |
Robotics |
out's That
JDBC
| The code for
connecting java interface with Microsoft Access.
//Creating an Access table CS_FINAL using Java code
import java.sql.*;
public class TableCreation
{
public static void main(String args[ ])
{
Connection conn;
//SQL query
String query;
query = "create table CS_FINAL" +
"(S_NAME Text(20), " +
"S_MAIL_ID Text(25))";
// declaration of a statement object called stmt
Statement stmt;
// Loading the Java JDBC:ODBC driver
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
}
// Handling the class not found exception if thrown
catch(ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
// The try block to create a connection to the Access database on the harddisk
try
{
// Creating a Connection called conn
conn = DriverManager.getConnection("jdbc:odbc:dushyant");
//dushyant is the DSN name.
// creating a statement object called stmt
stmt = conn.createStatement( );
// Passing the executeUpdate() method the 'Create Table' SQL
// syntax. The executeUpdate() method belongs to the object stmt
stmt.executeUpdate(query);
System.out.println("The CS_FINAL table has been created succssfully");
}
// Dealing with any SQL exception thrown
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage( ) );
}
} // End of main() method
} // End of class
|
the
code for viewing the record from the database.
import java.sql.*;
public class ShowRecord
{
public static void main(String args[ ])
{
Connection conn;
Statement stmt;
String query = "select S_NAME, S_MAIL_ID from CS_FINAL";
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException clsnotfndExcep)
{
System.out.print("ClassNotFoundException : ");
System.out.println(clsnotfndExcep.getMessage( ));
}
try
{
conn = DriverManager.getConnection("jdbc:odbc:dushyant");
//dushyant is the DSN name.
stmt = conn.createStatement( );
ResultSet rs = stmt.executeQuery(query);
System.out.println("query executed\n");
System.out.println("S_NAME \t\t\t\t\t S_MAIL_ID\n");
// Retrieving records from the RecordSet rs and displaying them at the system prompt
while (rs.next( ) )
{
String S_NAME= rs.getString("S_NAME");
String S_MAIL_ID= rs.getString("S_MAIL_ID");
System.out.println(S_NAME + "\t\t\t" + S_MAIL_ID + "\t" );
}
// Closing the Statement object stmt
stmt.close( );
// Closing the connection object conn
conn.close( );
}
catch(SQLException e)
{
System.out.println("SQLException: " +e.getMessage( ));
}
}
}
|
for any query mail me
|