2. Java Connectivity

advertisement
QUESTION BASED ON JAVA CONNECTIVITY
IMPORTANT QUESTIONS
Q1
Ans
Q2
Ans
Q3
Ans
Q4
Ans
Q5
Ans
Q6
Q7
Ans
Q8
Ans
What are the jdbc classes used in providing the connection?
(i) jdbc.sql.DriverManager
(ii) jdbc.sql.Connection
The DriverManager class loads the JDBC driver and opens the connection for data source.
The Connection provides the connectivity and also used for creating, preparing and calling the
statement.
What is Class.forName() used for?
This is used to create an instance of diver and put up with the DriverManager. The
DriverManager instance is available for making a connection with the Database(My Sql).
What are the methods used for executing an query of sql using JAVA API?
executeQuery, execute, executeUpdate
What is the work of DriverManager?
The DiverManager is a class that loads the jdbc driver which is used to access the data source ,
locates it and enter into the database and returns the connection object.
What is the function of the following commands?
a) DriverManager.getConnection()
b) Connection c
c.createStatement()
c) ResultSet r
r.next()
r.first()
r.last()
a) open the connection
c) create a statement
d) move cursor forward by one row in a result set.
e) Place the cursor to the first row in a result set.
f) Place the cursor to the last row in a result set
Write the steps to connect to a database with a java application.
a) Import the packages and classes required for database programming.
b) Register the jdbc driver using Class.forName() method.
c) Open the connection.
d) Execute the query.
e) Process the result in resultset.
What do you understand by ODBC ?
ODBC means Open Database Connectivity; it is provided by Microsoft to connect different
types of databases. This is a type of framework which is language independent as it can work
with any language.
What do you understand by JDBC?
JDBC means Java Database Connectivity; it is provided by Sun Java to connect different types
of databases. This is a type of framework of java which is completely language dependent as it
can work only with java front end application.
Q9
Ans
What are JDBC statement object? How can you create it?
A Statement object is used to send the SQL statement to the database and further get executed
by DBMS. We create a statement object and execute it by providing the exact execute method
with the SQL statement.
Statement st = con.createStatement();
String s = “Select * from emp;”;
ResultSet r = st.executeQuery(s);
Q10
Ans
Q11
Ans
Q12
Ans
For any SELECT query we use “executeQuery()” method.
For any updation or modification we use “executeUpdate()” method.
What is JDBC connection object? How can you create it?
A JDBC Connection object is used to create a connection using appropriate driver , location of
the database and the DriverManager. It is the session between the application program and the
database.
Conection C =
DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,”root”,”123456”);
What is ResultSet? How can you retrieve data from ResultSet?
A ResultSet object is used for storing the data. JDBC returns results and to store this result we
need to declare an instance of the ResultSet class.
ResultSet r = st.executeQuery(“Select * from student;”);
Assuming Student table of test database having following structure.
Stud_id
Varchar(5)
StudName Varchar(20)
StudClass
Integer(1)
StudFees
Integer(5)
Create a form according to the table and buttons required to ADD, DELETE and
MODIFY the records in the table using SQL queries.
FOR ADD
Step 1 – import the required classes and packages.
Step 2 – Create the connection.
Step 3 – Create the Statement and store in the ResultSet.
Step 4 – Store values from the TextField on the form into the variables
String s1 = jTextField1.getText();
String s2 = jTextField2.getText();
String s3 = Integer.parseInt(jTextField3.getText());
String s4 = Integer.parseInt(jTextField4.getText());
Step 5 – Create the insert statement into a String variable.
String Stt = “ insert into Student values („ ”+s1+” ‟ ”+s2+” ‟ ”+s3+” ‟ ”+s4+” ‟);”;
Step 6 – Create the statement to execute the statement, it returns a number of rows get effected.
int r = stmt.executeUpdate(Stt);
Here stmt is the statement object.
FOR UPDATE
Step 1 – import the required classes and packages.
Step 2 – Create the connection.
Step 3 – Create the Statement and store in the ResultSet.
Step 4 – Store values from the TextField on the form into the variables
String s1 = jTextField1.getText();
String s2 = jTextField2.getText();
String s3 = Integer.parseInt(jTextField3.getText());
String s4 = Integer.parseInt(jTextField4.getText());
Step 5 – Create the update statement into a String variable.
String Stt = “ update Student set Stud_id= ”+s1+” , StudName= ”+s2+” , StudClass=
”+s3+” , StudFees= ”+s4+” where Stud_id=”+s1+”;”;
Step 6 – Create the statement to execute the statement, it returns a number of rows get effected.
int r = stmt.executeUpdate(Stt);
Here stmt is the statement object.
FOR DELETE
Step 1 – import the required classes and packages.
Step 2 – Create the connection.
Step 3 – Create the Statement and store in the ResultSet.
Step 4 – Store values from the TextField on the form into the variables
String s1 = jTextField1.getText();
String s2 = jTextField2.getText();
String s3 = Integer.parseInt(jTextField3.getText());
String s4 = Integer.parseInt(jTextField4.getText());
Step 5 – Create the delete statement into a String variable.
String Stt = “ delete from Student where Stud_id= ‟ ”+s1+” „ “;
Step 6 – Create the statement to execute the statement, it returns a number of rows get effected.
int r = stmt.executeUpdate(Stt);
Here stmt is the statement object.
Download