Unit 5 Notes On Ajp 22517 Part 2
Unit 5 Notes On Ajp 22517 Part 2
class Mydatabase {
public static void main(String[] args) {
try {
// Create a statement
Statement stmt = con.createStatement();
// Execute a query
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
OUTPUT :
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
The key methods of the Driver interface include:
public Connection connect(String url, Properties info):
o This method attempts to establish a connection to the database specified by the
URL and the provided properties.
public boolean acceptsURL(String url):
o This method checks whether the driver can connect to the given database URL.
public int getMajorVersion():
o Returns the driver's major version number.
public int getMinorVersion():
o Returns the driver's minor version number.
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
The getXXX methods retrieve column values for the current row. You can retrieve values
either using the index number of the column, or by using the name of the column. In
general using the column index will be more efficient. Columns are numbered from 1.
By default, ResultSet object can be moved forward only and it is not updatable.
But we can make this object to move forward and backward direction by passing either
TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int)
method as well as we can make this object as updatable by:
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
Methods:
public boolean next() throws SQLException
o A ResultSet is initially positioned before its first row; the first call to next move the
cursor one row next from the current position. It returns true if there is a row
available in the resultset; otherwise returns false.
public boolean previous() throws SQLException
o It is used to move the cursor to the one row previous from the current position. It
returns true if there is a row available in the resultset; otherwise returns false.
o This method is usable only if the ResultSet is either TYPE_SCROLL_SENSITIVE or
TYPE_SCROLL_INSENSITIVE.
public boolean first() throws SQLException
o It is used to move the cursor to the first row in result set object. It returns true if
there is a row available in the resultset; otherwise returns false.
o This method is usable only if the ResultSet is either TYPE_SCROLL_SENSITIVE or
TYPE_SCROLL_INSENSITIVE.
public boolean last() throws SQLException
o It is used to move the cursor to the last row in result set object. It returns true if
there is a row available in the resultset; otherwise returns false.
o This method is usable only if the ResultSet is either TYPE_SCROLL_SENSITIVE or
TYPE_SCROLL_INSENSITIVE.
public boolean absolute(int row) throws SQLException
o It is used to move the cursor to the specified row number in the ResultSet object.
It returns true if there is a row available in the resultset; otherwise returns false.
o This method is usable only if the ResultSet is either TYPE_SCROLL_SENSITIVE or
TYPE_SCROLL_INSENSITIVE.
public boolean relative(int row) throws SQLException
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
o It is used to move the cursor to the relative row number in the ResultSet object, it
may be positive or negative. It returns true if there is a row available in the
resultset; otherwise returns false..
o This method is usable only if the ResultSet is either TYPE_SCROLL_SENSITIVE or
TYPE_SCROLL_INSENSITIVE.
public void beforeFirst() throws SQLException
o Moves the cursor to the front of this ResultSet object, just before the first row.
This method has no effect if the result set contains no rows.
public void afterLast() throws SQLException
o Moves the cursor to the end of this ResultSet object, just after the last row. This
method has no effect if the result set contains no rows.
public void moveToInsertRow() throws SQLException
o Moves the cursor to the insert row. The current cursor position is remembered
while the cursor is positioned on the insert row. The insert row is a special row
associated with an updatable result set.
public void moveToCurrentRow() throws SQLException
o Moves the cursor to the remembered cursor position, usually the current row.
This method has no effect if the cursor is not on the insert row.
public void insertRow() throws SQLException
o Inserts the contents of the insert row into this ResultSet object and into the
database. The cursor must be on the insert row when this method is called.
public void updateRow() throws SQLException
o Updates the underlying database with the new contents of the current row of this
ResultSet object. This method cannot be called when the cursor is on the insert
row.
public int getInt(int columnIndex)
o It is used to return the data of specified column index of the current row as int.
public int getInt(String columnName)
o It is used to return the data of specified column name of the current row as int.
public String getString(int columnIndex)
o It is used to return the data of specified column index of the current row as String.
public String getString(String columnName)
o It is used to return the data of specified column name of the current row as String.
public Type getType(int columnIndex)
o It is used to return the data of specified column index of the current row as Type.
public Type getType(String columnName)
o It is used to return the data of specified column name of the current row as Type.
public void updateInt(int columnIndex, int x) throws SQLException
o Updates the designated column with an int value.
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
public void updateInt(String columnName, int x) throws SQLException
o Updates the designated column with an int value.
public void updateString(int columnIndex, String x) throws SQLException
o Updates the designated column with a String value.
public void updateString(String columnName, String x) throws SQLException
o Updates the designated column with a String value.
public void updateType(int columnIndex, Type x) throws SQLException
o Updates the designated column with a Type value.
public void updateType(String columnName, Type x) throws SQLException
o Updates the designated column with a Type value.
public ResultSetMetaData getMetaData() throws SQLException
o Retrieves the number, types and properties of this ResultSet object's columns.
================================================================
Program to execute DDL statements (Create Table, Alter Table and Drop Table ) using
MySQL and Java
================================================================
Aim : To create employee={eno,ename,salary} table in college database in MySQL
import java.sql.*;
class Mydatabase {
public static void main(String[] args) {
try {
// Create a statement
Statement stmt = con.createStatement();
// Execute a query
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
boolean result = stmt.execute("CREATE TABLE employee(eno int, ename
varchar(30), salary int)");
OUTPUT:
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
================================================
Aim : To alter employee table in college database in MySQL to add new column
designation.
import java.sql.*;
class Mydatabase {
public static void main(String[] args) {
try {
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
// Establish the connection with college database
String url = "jdbc:mysql://localhost:3306/college";
String username = "root";
String password = "Admin@123";
Connection con = DriverManager.getConnection(url, username, password);
// Create a statement
Statement stmt = con.createStatement();
// Execute a query
boolean result = stmt.execute("ALTER TABLE employee ADD(designation
varchar(30))");
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
=======================================================
Aim : To delete employee table from college database in MySQL.
import java.sql.*;
class Mydatabase {
public static void main(String[] args) {
try {
// create driver and register it
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
String password = "Admin@123";
Connection con = DriverManager.getConnection(url, username, password);
// Create a statement
Statement stmt = con.createStatement();
// Execute a query
boolean result = stmt.execute("DROP TABLE employee");
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate