Q1 What Are the Jdbc Classes Used in Providing the Connection?
Total Page:16
File Type:pdf, Size:1020Kb
Q1 What are the jdbc classes used in providing the connection? Ans (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. Q2 What is Class.forName() used for? Ans 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). Q3 What are the methods used for executing an query of sql using JAVA API? Ans executeQuery, execute, executeUpdate Q4 What is the work of DriverManager? Ans 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. Q5 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() Ans 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 Q6 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. Q7 What do you understand by ODBC ? Ans 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. Q8 What do you understand by JDBC? Ans 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 What are JDBC statement object? How can you create it? Ans 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);
For any SELECT query we use “executeQuery()” method. For any updation or modification we use “executeUpdate()” method. Q10 What is JDBC connection object? How can you create it? Ans 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”); Q11 What is ResultSet? How can you retrieve data from ResultSet? Ans 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;”); Q12 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. Ans 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. Q13