You are on page 1of 3

Programme: Computer Science and Engineering

Course Title & Code: Advanced Java (CS604) Date:


Continuous Internal Evaluation III
Faculty-in-charge: Shobha Chandra K

Solution Ma
Q.No
rks
5 x 1m 5m

import java.util.*; 5m
class Address{
private String name;
private String street;
private String city;
Address(String n, String s, String c)
{
name=n; street=s; city=c;
}
public String toString(){
2
return n+s+c; }}
public static void main(String args[]) {
LinkedList<Address> a1= new LinkedList<Address>();
al.add(new Address(“mce”,”salgame road”, “Hassan”));
al.add(new Address(“aa”,”salgame road”, “Hassan”));
for(Address a:al)
System.out.println(a); System.out.println();
}
}
import java.util.*; 5m
class ArrayListDemo {
public static void main(String args[]) {
// Create an array list.
ArrayList<String> al = new ArrayList<String>();
System.out.println("Initial size of al: " +
al.size());
// Add elements to the array list.
al.add("C");
3
al.add("A");
al.add("E");
al.add(1, "A2");
System.out.println("Size of al after additions: " + al.size());
// Display the array list.
System.out.println("Contents of al: " + al);
}
}

Using URL 1m 5m
4 Using url, id, pwd 2m
Using properties 2m
preparedStatement object is used 5m
A question mark is used as a placeholder for a value that is inserted into the
query after the query is compiled.
setXXX() method of the PreparedStatement object is used to replace the
question mark with the value passed to the setXXX() method.
The setXXX() requires two parameters. 3m
The executeQuery() statement doesn’t require a parameter
try {
String query = "SELECT * FROM Customers WHERE CustNumber = ?";
PreparedStatement pstatement = Db.preparedStatement(query);
5
pstatement.setString(1, "123");
Results = pstatement.executeQuery ();
//Place code here to interact with the ResultSet
pstatement.close();
} 2m
catch ( SQLException error ){
System.err.println("SQL error." + error);
System.exit(3);
}

6 1. loading the JDBC driver 5x1m 5m


Class.forName(“sun:jdbc.odbc.JdbcOdbcDriver”);
2. connecting to the DBMS
Connection con;
con=DriverManager.getConnection(url,userId,password);
3. creating and executing a statement
Statement stmt;
ResultSet rs;
stmt=con.createStatement();
rs=stmt.executeQuery();
4. processing data returned by the DBMS
While(rs.next()){
}
5. terminating the connection with the DBMS
con.close()

You might also like