USING JDBC TO MODIFY A DATABASE
Modifying a database is just as simple as querying a database. However, instead of
using executeQuery(), you use executeUpdate() and you don’t have to worry about a
result set. Consider the following example:using executeQuery(), you use executeUpdate() and you don’t have to worry about a
import java.sql.*;
public class JdbcExample1
{
public static void main(String args[])
{
Connection con = null;
public class JdbcExample1
{
public static void main(String args[])
{
Connection con = null;
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
Connection Conn = DriverManager.getConnection (jdbc:odbc:Access);
Statement Stmt = Conn.createStatement();
// We have already seen all the above steps
String sql = "INSERT INTO CUSTOMERS +
“ (CustomerID, Firstname, LastName, Email)” +
“ VALUES (004, ‘Selena’, ‘Sol’ “ + “selena@extropia.com’)”;
// Now submit the SQL
// We have already seen all the above steps
String sql = "INSERT INTO CUSTOMERS +
“ (CustomerID, Firstname, LastName, Email)” +
“ VALUES (004, ‘Selena’, ‘Sol’ “ + “selena@extropia.com’)”;
// Now submit the SQL
try
{
Stmt.executeUpdate(sql);
}catch (Exception e)
{
System.out.println(“Problem with Sending Query: ” + e);
}
finally
{
result.close();
stmt.close();
Conn.close();
{
Stmt.executeUpdate(sql);
}catch (Exception e)
{
System.out.println(“Problem with Sending Query: ” + e);
}
finally
{
result.close();
stmt.close();
Conn.close();
}
} // end of main method
} // end of class
As you can see, there is not much to it. Add, modify and delete are all handled by the
executeUpdate() method or executeUpdate(String str) where str is a SQL Insert,
Update or Delete Statement
} // end of main method
} // end of class
As you can see, there is not much to it. Add, modify and delete are all handled by the
executeUpdate() method or executeUpdate(String str) where str is a SQL Insert,
Update or Delete Statement
No comments:
Post a Comment