{"id":238,"date":"2018-07-27T05:57:59","date_gmt":"2018-07-27T05:57:59","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=238"},"modified":"2018-08-02T08:28:14","modified_gmt":"2018-08-02T08:28:14","slug":"jdbc-part-ii","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/jdbc-part-ii\/","title":{"rendered":"JDBC \u2013Part II"},"content":{"raw":"<strong>\u00a0 \u00a0SQL<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">It stands for Structured Query Language. SQL means Standardized syntax for \u201cquerying\u201d or accessing a Relational database.<\/p>\r\n&nbsp;\r\n\r\n<strong>SQL Syntax<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Before we study about working JDBC with SQL, let us have a basic understanding of how to write SQL queries. Some of the SQL statements which would appear in the example programs are given below.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">This command can insert a new record into the named table.<\/p>\r\n<p style=\"text-align: justify\">INSERT INTO table name( field1, field2 ) VALUES ( value1, value2 )<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">This command can change an existing record or records.<\/p>\r\n<p style=\"text-align: justify\">UPDAT E tablename SET( field1 = value1, field2 = value2 ) WHERE condition<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">This command can remove all records that match condition.<\/p>\r\n<p style=\"text-align: justify\">DELET E FROM tablename WHERE condition<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">This command can retrieve all records that matches the condition.<\/p>\r\n<p style=\"text-align: justify\">SELECT field1, field2 FROM tablename WHERE condition<\/p>\r\n<p style=\"text-align: justify\">Install Mysql<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us now see the steps of installing MySQL to work with JDBC.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 1: We need to Install Mysql Database from MYSQL official website dev.mysql.com. Figure 27.1 shows the Google search for Download of MySQL database.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-239 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-137.png\" alt=\"\" width=\"518\" height=\"350\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Step 2: <\/strong>Select the Platform type (i.e. Windows or Mac) and download the Installer. Figure 27.1 shows selecting the platform for Windows OS.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-240 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-138.png\" alt=\"\" width=\"603\" height=\"311\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Step 3: Create Database<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Once MySQL is installed in your machine, Open MySQL Comand Line and connect to MySQL server by specifying the user name and password in the command line arguments to <em>mysql<\/em> command.Then create a Database by using the below SQLstatement.<\/p>\r\n&nbsp;\r\n\r\n<strong>Syntax<\/strong>\r\n\r\n&nbsp;\r\n\r\nCREAT E DATABASE databasename;\r\n\r\nFigure 27.3 shows creating the database in the MySQL command line using the above statement.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-241 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-139.png\" alt=\"\" width=\"604\" height=\"313\" \/><\/p>\r\n<strong>Step 4: <\/strong><strong>USE Database<\/strong>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nFrom the list of database created, to use a particular database we use the command 'USE database name'.\r\n\r\n&nbsp;\r\n\r\n<strong>Syntax<\/strong>\r\n\r\n&nbsp;\r\n\r\nUSE databasename;\r\n\r\nFigure 27.4 shows how to use the database using the above statement.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-242 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-140.png\" alt=\"\" width=\"604\" height=\"302\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Step 5: <\/strong><strong>Create Table<\/strong>\r\n\r\n&nbsp;\r\n\r\nNow we can create Tables in Database using the create statement.\r\n\r\n&nbsp;\r\n\r\n<strong>Syntax<\/strong>\r\n\r\n&nbsp;\r\n\r\nCREAT E TABLE tablename(attribute name datatypes);\r\n\r\nFigure 27.5 shows how to create tables using the above statement.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-243 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-141.png\" alt=\"\" width=\"530\" height=\"317\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Step 6: <\/strong><strong>Insert Query in Table<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Once the table is created we can now Insert the values in the table. <em>Note: <\/em>For varchar data type provide the values within quotes . Syntax<\/p>\r\n&nbsp;\r\n\r\nINSERT INTO tablename VALUES(data);\r\n\r\nFigure 27.6 shows how to insert records into the table using the above statement.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-244 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-142.png\" alt=\"\" width=\"530\" height=\"308\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Step 7: <\/strong><strong>Select Query in Table<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe Select statement can be used now to list all the values entered into the Table.\r\n\r\n&nbsp;\r\n\r\n<strong>Syntax<\/strong>\r\n\r\n&nbsp;\r\n\r\nSELECT * FROM tablename;\r\n\r\nFigure 27.7 shows how to select the records from the table using the above statement.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-245 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-143.png\" alt=\"\" width=\"588\" height=\"364\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Step 8: <\/strong><strong>Delete Query in Table<\/strong>\r\n\r\n&nbsp;\r\n\r\nTo delete a particular Row from the table.\r\n\r\n&nbsp;\r\n\r\n<strong>Syntax<\/strong>\r\n\r\n&nbsp;\r\n\r\nDELET E FROM tablename WHERE attributename=\u2018value\u2019;\r\n\r\nFigure 27.8 shows how to delete a record from the table using the above statement.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-246 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-144.png\" alt=\"\" width=\"547\" height=\"311\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Step 9: <\/strong><strong>Update Query in Table<\/strong>\r\n\r\n&nbsp;\r\n\r\nTo update values in a Row.\r\n\r\n&nbsp;\r\n\r\n<strong>Syntax<\/strong>\r\n\r\n&nbsp;\r\n\r\nUPDAT E tablename SET attributename=newvalue WHERE attributename=value;\r\n\r\nFigure 27.9 shows how to update records in the table using the above statement.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-247 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-145.png\" alt=\"\" width=\"525\" height=\"315\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Transactions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Transaction means more than one statement which must all succeed (or all fail) together. If one fails, the system must reverse all previous actions. Also a transaction cannot leave database in an inconsistent state halfway through a transaction. The operation COMMIT is done for a complete transaction and ROLLBACK is done for an aborted transaction.<\/p>\r\n&nbsp;\r\n\r\n<strong>Mapping Java Types to SQL Types<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-248 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-146.png\" alt=\"\" width=\"543\" height=\"271\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n<p style=\"text-align: center\">Table27.1 shows the existence of Java types for each SQL types.<\/p>\r\n&nbsp;\r\n\r\n<strong>Database Time<\/strong>\r\n\r\n&nbsp;\r\n\r\nTimes in SQL are notoriously not standard.\r\n\r\n&nbsp;\r\n\r\nJava defines three classes to represent Date and Time.\r\n<ul>\r\n \t<li>java.sql.Date<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nThis class includes year, month, day.\r\n<ul>\r\n \t<li>java.sql.Time<\/li>\r\n<\/ul>\r\nThis class includes hours, minutes, seconds.\r\n<ul>\r\n \t<li>java.sql.Timestamp<\/li>\r\n<\/ul>\r\nThis class includes year, month, day, hours, minutes, seconds and nanoseconds.\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0JDBC Interface classes<\/strong>\r\n<div>\r\n\r\n\u00a0 \u00a0 JDBC Interface classes are contained in java.sql.* package. This consists of the following classes.\r\n<ul>\r\n \t<li>DriverManager<\/li>\r\n \t<li>Connection<\/li>\r\n \t<li>Statement<\/li>\r\n \t<li>CallableStatement<\/li>\r\n \t<li>PreparedStatement<\/li>\r\n \t<li>Resultset<\/li>\r\n \t<li>ResultSetMetaData<\/li>\r\n \t<li>DatabaseMetaData<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0The JDBC Steps<\/strong>\r\n\r\n&nbsp;\r\n\r\n1. Importing Packages.\r\n\r\n2. Registering the JDBC Drivers.\r\n\r\n3. Opening a Connection to a Database.\r\n\r\n4. Creating a Statement Object.\r\n\r\n5. Executing a Query and Returning a Result Set Object.\r\n\r\n6. Processing the Result Set.\r\n\r\n7. Closing the Result Set and Statement Objects.\r\n\r\n8. Closing the Connection.\r\n\r\n&nbsp;\r\n\r\n<strong>Simple Example<\/strong>\r\n\r\n&nbsp;\r\n\r\nimport java.sql;\r\n\r\nclass SimpleExam ple {\r\n\r\npublic static void main(String args[]) {\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">String url = \u201cjdbc:odbc:jnf\u201d;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">try {<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Class.forName(\u201csun.jdbc.odbc.JdbcOdbcDriver\u201d); Connection myConnection = DriverManager.getConnection(url,\u201dIT\u201d,\u201dit@123\u201d); myConnection.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(Exception e) {<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">e.printStackTrace();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">This example code explains about registering for the driver an loading the driver using the URL, user name and password.<\/p>\r\n&nbsp;\r\n\r\n<strong>Sending SQL statements<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2026\u2026\r\n\r\n&nbsp;\r\n\r\nString query= \u201cSelect name,id,salary FROM employees ORDER By salary\u201d; Connection myConnection = DriverManager.getConnection(\u2026..);\r\n\r\n&nbsp;\r\n\r\nStatement myStatement = myConnection.createStatement();\r\n\r\nResultSet rs = myStatement.executeQuery(query);\r\n\r\nwhile(rs.next)\r\n\r\n{\r\n\r\nString empName = rs.getString(1);\r\n\r\nString empId = rs.getString(2);\r\n\r\nString empSalary = rs.getString(3);\r\n\r\nSystem.out.println(\u201cEm ployee \u201d + empName + \u201c with id \u201d + empId + \u201c earns \u201d + empSalary);\r\n\r\n}\r\n\r\nmyStatement.close();\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">myConne ction.close ();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u2026\u2026.<\/span>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0Once Connection is established, Statement object is created. This Statement object is used to execute the query. The SQL query that has been tried in this example is an 'Select' query. Hence the retrieved records are read sequentially and displayed. Then the connection is closed.<\/p>\r\n&nbsp;\r\n\r\n<strong>Simple Program<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe complete executing code for the example is given below.\r\n\r\n&nbsp;\r\n\r\nimport java.sql.*;\r\n\r\npublic class JDBC\r\n\r\n{\r\n\r\npublic static void main(String []args)throws Exception\r\n\r\n{\r\n\r\nClass.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");\r\n\r\nConnection\r\n\r\ncon=DriverManager.getConnection(\"jdbc:odbc:sample\");\r\n\r\nStatement st=con.createStatement();\r\n\r\nString query=\"select * from Student\";\r\n\r\nResultSet rs=st.executeQuery(query);\r\n\r\nwhile(rs.next())\r\n\r\n{\r\n\r\nSystem.out.println(rs.getString(\"name\"));\r\n\r\nSystem.out.println(rs.getInt(\"rollno\"));\r\n\r\nSystem.out.println(rs.getInt(\"mark1\"));\r\n\r\nSystem.out.println(rs.getInt(\"mark2\"));\r\n\r\n}\r\n\r\ncon.close();\r\n\r\n}\r\n\r\n}\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Example Program<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This is another example program where the user is allowed to do all database operations such as select, insert, update, delete and clearing the screen. A connection is established to the URL jdbc :odbc:jnf2. The database name is jnf2 and the table name is Student. The user is displayed with a menu of operations. Upon selecting the choice, its corresponding function is called. Functions like display(), clrscr(), insert(), modify() and delete() have been coded.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the function display() the method executeQuery() is called with the Statement object by passing the SQL SELECT statement which displays the records from the table.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the function insert() the method executeUpdate() is called with the Statement object by passing the SQL INSERT statement which inserts a record into the table.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the function modify() the method executeUpdate() is called with the Statement object by passing the SQL UPDATE statement which updates a single record in the table.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the function delete() the method executeUpdate() is called with the Statement object by passing the SQL DELETE statement which deletes a record from the table.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the function clrscr(), a for loop is executed which displays blank for 25 lines thereby it clears the screen.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Within each function a Connection object is created and closed at the end of the function. This is avoid connection errors while executing.<\/p>\r\n&nbsp;\r\n\r\nimport java.sql.*;\r\n\r\nimport java.io.*;\r\n\r\npublic class JDBC2\r\n\r\n{\r\n\r\npublic static void main(String []args)throws Exception\r\n\r\n{\r\n\r\nint ch;\r\n\r\nClass.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">InputStreamReader i = new InputStreamReader(System.in); BufferedReader b = new BufferedReader(i); try<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Connection c;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">c = DriverManager.getConnection(\"jdbc:odbc:jnf2\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">do<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"\\t\\t\\t\\t Menu\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">S<\/span><span style=\"text-align: initial;font-size: 1em\">ystem.out.println(\"\\t\\t\\t\\t 1.View\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"\\t\\t\\t\\t 2.Clear\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"\\t\\t\\t\\t 3.Insert\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"\\t\\t\\t\\t 4.Modify\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"\\t\\t\\t\\t 5.Delete\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"\\t\\t\\t\\t 6.Exit\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Enter your choice:\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ch= Integer.parseInt(b.readLine());<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">switch(ch)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">case 1:<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">display();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">break;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">case 2:<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">clrscr();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">break;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">case 3:<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{\r\n<\/span><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0insert();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">display();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">break;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">case 4:<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">modify();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">display();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">break;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">case 5:<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">delete();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">display();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">break;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">case 6:<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.exit(0);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">break;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">default:<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"invalid input\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}}while(ch!=6);}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(Exception e){}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">static void display()<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{\u00a0 try<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ResultSet rs;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">int row=0;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Connection c1;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">c1 = DriverManager.getConnection(\"jdbc:odbc:jnf2\"); Statement st=c1.createStatement();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">rs=st.executeQuery(\"select * from student\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"\\nRegno\\tName\\tClass\\tMark\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">while(rs.next())<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(rs.getInt(1)+\"\\t\"<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">+rs.getString(2)+\"\\t\"+rs.getString(3)+\"\\t\"+rs.getInt(4); c1.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(Exception e){}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">static void insert()<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">try<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">int row1;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Statement insst;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Connection c2;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">c2 = DriverManager.getConnection(\"jdbc:odbc:jnf2\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">insst=c2.createStatement();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"9,jnf,vbj,30\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">row1=insst.executeUpdate(\"insert into Student\" + \" values (9,'jnf','vbj',100)\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">c2.commit();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"No of rows inserted = \" + row1);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(SQLException e){System.out.println(\"error\" + e);}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">static void modify()<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">try<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"6,jnf,vbj,change 30 to 80\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">int row2;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Statement modst;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Connection c3;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">c3 = DriverManager.getConnection(\"jdbc:odbc:jnf2\"); modst=c3.createStatement();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">row2=modst.executeUpdate(\"update student set mark=90 where regno=6\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">c3.commit();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"No of rows update d = \" + row2);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(SQLException e){System.out.println(\"error\" + e);}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">static void delete()<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">try<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"6,jnf,vbj,90\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">int row3;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Statement delst;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Connection c4;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">c4 = DriverManager.getConnection(\"jdbc:odbc:jnf2\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">delst=c4.createStatement();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">row3=delst.executeUpdate(\"delete from student where regno=6\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(row3&gt;0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">c4.commit();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"No of rows update d = \" + row3);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{ System.out.println(\"record not found\"); }<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(SQLException e){System.out.println(\"error\" + e);}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">static void clrscr()<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">for(int k=1;k&lt;25;k++)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">The output of this code is shown in the below Figure 27.10 and Figure 27.11.<\/span>\r\n\r\n<\/div>\r\n<strong>\u00a0 Results<\/strong>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-249 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-147.png\" alt=\"\" width=\"584\" height=\"391\" \/><\/p>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-250 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-148.png\" alt=\"\" width=\"629\" height=\"392\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 Transaction Management<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Transactions are not explicitly opened and closed. Instead, the connection has a state called AutoCommit mode. If AutoCommit is true, then every statement is automatically committed. The default case is true.<\/p>\r\n&nbsp;\r\n\r\n<strong>setAutoCommit<\/strong>\r\n\r\n&nbsp;\r\n\r\nConnection.setAutoCommit(boolean)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">If AutoCommit is false, then every statement is added to an ongoing transaction. It must explicitly commit or rollback the transaction using Connection.commit() and Connection.rollback().<\/p>\r\n&nbsp;\r\n\r\n<strong>Statements<\/strong>\r\n\r\n&nbsp;\r\n\r\nThere are three types of statements called,\r\n<ul>\r\n \t<li>Statement<\/li>\r\n \t<li>PreparedStatement<\/li>\r\n \t<li>CallableStatement<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">The Statement object is used for executing a static SQL statement and obtaining the results produced by it as we have seen earlier.<\/p>\r\n&nbsp;\r\n\r\n<strong>Prepared Statements<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Prepared Statements are used for queries that are executed many times. They are parsed (compiled) by the DBMS only once. The column values can be set after compilation. Instead of values, \u2018?\u2019 is used and it can be substituted later with actual values.<\/p>\r\n&nbsp;\r\n\r\n<strong>Querying with PreparedStatement<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Example<\/strong>\r\n\r\n&nbsp;\r\n\r\nString queryStr =\r\n\r\n\"SELECT * FROM employee \" +\r\n\r\n\"WHERE id = ? and salary &gt; ?\";\r\n\r\nPreparedStatement pstmt = con.pre pareStatement(queryStr);\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">pstmt.setString(1, \u201cE2001\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">pstmt.setInt(2, 48000);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ResultSet rs = pstmt.executeQuery();<\/span>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">\u00a0 In this example code, the SELECT statement is given with '?' for the two attributes 'id' and 'salary'. PreparedStatement object is created by passing this query string. Then we use methods like setString() and setInt() to pass values for id and salary. Then the query is executed using executeQuery() method.<\/p>\r\n&nbsp;\r\n\r\n<strong>Updating with PreparedStatement<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Example<\/strong>\r\n\r\n&nbsp;\r\n\r\nString deleteStr =\r\n\r\n\u201cDELET E FROM employee \" +\r\n\r\n\"WHERE id = ? and salary &gt; ?\";\r\n\r\nPreparedStatement pstmt = con.pre pareStatement(deleteStr); pstmt.setString(1, \u201cE2001\");\r\n\r\npstmt.setDouble(2, 48000);\r\n\r\nint delnum = pstmt.executeUpdate();\r\n\r\n<strong>Statements vs. PreparedStatements<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Example<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe difference in syntax is given with an example in the below Table 27.2.\r\n\r\n<strong>Table 27.2 Difference between Statement and PreparedStatement<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-251 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-149.png\" alt=\"\" width=\"646\" height=\"170\" \/>\r\n\r\n&nbsp;\r\n<div>\r\n<p style=\"text-align: justify\"><strong>\u00a0 \u00a0 CallableStatement<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A CallableStatement is used to access the database stored procedures. The CallableStatement interface can also accept runtime input parameters.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A CallableStatement object is created using the Connection.prepareCall() method.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example<\/strong>\r\n\r\n&nbsp;\r\n\r\nCallableStatement cstmt = null;\r\n\r\ntry {\r\n\r\nString SQL = \"{call getEmpName (?, ?)}\";\r\n\r\ncstmt = conn.prepareCall(SQL);\r\n\r\n. . .\r\n\r\n}\r\n\r\ncatch (SQLException e)\u00a0 { . . . }\r\n\r\nfinally { . . . }\r\n\r\n&nbsp;\r\n\r\n<strong>ResultSet Meta-Data<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A ResultSetMetaData is an object that can be used to get information about the properties of the columns in a ResultSet object.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example<\/strong>\r\n\r\n&nbsp;\r\n\r\nResultSetMetaData rsmd = rs.getMetaData();\r\n\r\nint numcols = rsmd.getColumnCount();\r\n\r\nfor (int i = 1 ; i &lt;= numcols; i++)\r\n\r\n{\r\n\r\nSystem.out.print(rsmd.getColumnLabel(i)+\" \");\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The method getMetaData() is used with the ResultSet object which returns a ResultSetMetaData object. With the ResultSetMetaData object, use the method getColumnCount() which returns the number of\u00a0<span style=\"text-align: initial;font-size: 1em\">columns in every record. Then we can use a for loop that executes for the number of columns to display the column label using the method getColumnLabel().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">Summary<\/strong><\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 This module explains about how to work with SQL. It has explored the working of JDBC with simple programming illustrations. This section also discusses about the transaction management in JDBC and its basic operations that can be performed.<\/p>\r\n&nbsp;\r\n\r\n<strong>Web Links<\/strong>\r\n<ul>\r\n \t<li><em>https:\/\/pawangkp.files.wordpress.com\/2009\/10\/deawsj-6_<strong>ppt<\/strong>_1b.<strong>ppt<\/strong><\/em><\/li>\r\n \t<li><em>https:\/\/www.tutorialspoint.com\/jdbc\/jdbc-statements.htm<\/em><\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;","rendered":"<p><strong>\u00a0 \u00a0SQL<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">It stands for Structured Query Language. SQL means Standardized syntax for \u201cquerying\u201d or accessing a Relational database.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>SQL Syntax<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Before we study about working JDBC with SQL, let us have a basic understanding of how to write SQL queries. Some of the SQL statements which would appear in the example programs are given below.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This command can insert a new record into the named table.<\/p>\n<p style=\"text-align: justify\">INSERT INTO table name( field1, field2 ) VALUES ( value1, value2 )<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This command can change an existing record or records.<\/p>\n<p style=\"text-align: justify\">UPDAT E tablename SET( field1 = value1, field2 = value2 ) WHERE condition<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This command can remove all records that match condition.<\/p>\n<p style=\"text-align: justify\">DELET E FROM tablename WHERE condition<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This command can retrieve all records that matches the condition.<\/p>\n<p style=\"text-align: justify\">SELECT field1, field2 FROM tablename WHERE condition<\/p>\n<p style=\"text-align: justify\">Install Mysql<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us now see the steps of installing MySQL to work with JDBC.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 1: We need to Install Mysql Database from MYSQL official website dev.mysql.com. Figure 27.1 shows the Google search for Download of MySQL database.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-239 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-137.png\" alt=\"\" width=\"518\" height=\"350\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-137.png 518w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-137-300x203.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-137-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-137-225x152.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-137-350x236.png 350w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Step 2: <\/strong>Select the Platform type (i.e. Windows or Mac) and download the Installer. Figure 27.1 shows selecting the platform for Windows OS.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-240 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-138.png\" alt=\"\" width=\"603\" height=\"311\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-138.png 603w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-138-300x155.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-138-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-138-225x116.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-138-350x181.png 350w\" sizes=\"auto, (max-width: 603px) 100vw, 603px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 3: Create Database<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Once MySQL is installed in your machine, Open MySQL Comand Line and connect to MySQL server by specifying the user name and password in the command line arguments to <em>mysql<\/em> command.Then create a Database by using the below SQLstatement.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>CREAT E DATABASE databasename;<\/p>\n<p>Figure 27.3 shows creating the database in the MySQL command line using the above statement.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-241 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-139.png\" alt=\"\" width=\"604\" height=\"313\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-139.png 604w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-139-300x155.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-139-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-139-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-139-350x181.png 350w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/p>\n<p><strong>Step 4: <\/strong><strong>USE Database<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>From the list of database created, to use a particular database we use the command &#8216;USE database name&#8217;.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>USE databasename;<\/p>\n<p>Figure 27.4 shows how to use the database using the above statement.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-242 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-140.png\" alt=\"\" width=\"604\" height=\"302\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-140.png 604w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-140-300x150.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-140-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-140-225x113.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-140-350x175.png 350w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 5: <\/strong><strong>Create Table<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Now we can create Tables in Database using the create statement.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>CREAT E TABLE tablename(attribute name datatypes);<\/p>\n<p>Figure 27.5 shows how to create tables using the above statement.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-243 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-141.png\" alt=\"\" width=\"530\" height=\"317\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-141.png 530w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-141-300x179.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-141-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-141-225x135.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-141-350x209.png 350w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 6: <\/strong><strong>Insert Query in Table<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Once the table is created we can now Insert the values in the table. <em>Note: <\/em>For varchar data type provide the values within quotes . Syntax<\/p>\n<p>&nbsp;<\/p>\n<p>INSERT INTO tablename VALUES(data);<\/p>\n<p>Figure 27.6 shows how to insert records into the table using the above statement.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-244 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-142.png\" alt=\"\" width=\"530\" height=\"308\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-142.png 530w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-142-300x174.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-142-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-142-225x131.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-142-350x203.png 350w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 7: <\/strong><strong>Select Query in Table<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The Select statement can be used now to list all the values entered into the Table.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>SELECT * FROM tablename;<\/p>\n<p>Figure 27.7 shows how to select the records from the table using the above statement.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-245 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-143.png\" alt=\"\" width=\"588\" height=\"364\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-143.png 588w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-143-300x186.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-143-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-143-225x139.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-143-350x217.png 350w\" sizes=\"auto, (max-width: 588px) 100vw, 588px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 8: <\/strong><strong>Delete Query in Table<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>To delete a particular Row from the table.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>DELET E FROM tablename WHERE attributename=\u2018value\u2019;<\/p>\n<p>Figure 27.8 shows how to delete a record from the table using the above statement.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-246 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-144.png\" alt=\"\" width=\"547\" height=\"311\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-144.png 547w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-144-300x171.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-144-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-144-225x128.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-144-350x199.png 350w\" sizes=\"auto, (max-width: 547px) 100vw, 547px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 9: <\/strong><strong>Update Query in Table<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>To update values in a Row.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>UPDAT E tablename SET attributename=newvalue WHERE attributename=value;<\/p>\n<p>Figure 27.9 shows how to update records in the table using the above statement.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-247 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-145.png\" alt=\"\" width=\"525\" height=\"315\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-145.png 525w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-145-300x180.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-145-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-145-225x135.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-145-350x210.png 350w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Transactions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Transaction means more than one statement which must all succeed (or all fail) together. If one fails, the system must reverse all previous actions. Also a transaction cannot leave database in an inconsistent state halfway through a transaction. The operation COMMIT is done for a complete transaction and ROLLBACK is done for an aborted transaction.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Mapping Java Types to SQL Types<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-248 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-146.png\" alt=\"\" width=\"543\" height=\"271\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-146.png 543w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-146-300x150.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-146-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-146-225x112.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-146-350x175.png 350w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p style=\"text-align: center\">Table27.1 shows the existence of Java types for each SQL types.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Database Time<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Times in SQL are notoriously not standard.<\/p>\n<p>&nbsp;<\/p>\n<p>Java defines three classes to represent Date and Time.<\/p>\n<ul>\n<li>java.sql.Date<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>This class includes year, month, day.<\/p>\n<ul>\n<li>java.sql.Time<\/li>\n<\/ul>\n<p>This class includes hours, minutes, seconds.<\/p>\n<ul>\n<li>java.sql.Timestamp<\/li>\n<\/ul>\n<p>This class includes year, month, day, hours, minutes, seconds and nanoseconds.<\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0JDBC Interface classes<\/strong><\/p>\n<div>\n<p>\u00a0 \u00a0 JDBC Interface classes are contained in java.sql.* package. This consists of the following classes.<\/p>\n<ul>\n<li>DriverManager<\/li>\n<li>Connection<\/li>\n<li>Statement<\/li>\n<li>CallableStatement<\/li>\n<li>PreparedStatement<\/li>\n<li>Resultset<\/li>\n<li>ResultSetMetaData<\/li>\n<li>DatabaseMetaData<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0The JDBC Steps<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1. Importing Packages.<\/p>\n<p>2. Registering the JDBC Drivers.<\/p>\n<p>3. Opening a Connection to a Database.<\/p>\n<p>4. Creating a Statement Object.<\/p>\n<p>5. Executing a Query and Returning a Result Set Object.<\/p>\n<p>6. Processing the Result Set.<\/p>\n<p>7. Closing the Result Set and Statement Objects.<\/p>\n<p>8. Closing the Connection.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Simple Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>import java.sql;<\/p>\n<p>class SimpleExam ple {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">String url = \u201cjdbc:odbc:jnf\u201d;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">try {<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Class.forName(\u201csun.jdbc.odbc.JdbcOdbcDriver\u201d); Connection myConnection = DriverManager.getConnection(url,\u201dIT\u201d,\u201dit@123\u201d); myConnection.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(Exception e) {<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">e.printStackTrace();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">This example code explains about registering for the driver an loading the driver using the URL, user name and password.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Sending SQL statements<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2026\u2026<\/p>\n<p>&nbsp;<\/p>\n<p>String query= \u201cSelect name,id,salary FROM employees ORDER By salary\u201d; Connection myConnection = DriverManager.getConnection(\u2026..);<\/p>\n<p>&nbsp;<\/p>\n<p>Statement myStatement = myConnection.createStatement();<\/p>\n<p>ResultSet rs = myStatement.executeQuery(query);<\/p>\n<p>while(rs.next)<\/p>\n<p>{<\/p>\n<p>String empName = rs.getString(1);<\/p>\n<p>String empId = rs.getString(2);<\/p>\n<p>String empSalary = rs.getString(3);<\/p>\n<p>System.out.println(\u201cEm ployee \u201d + empName + \u201c with id \u201d + empId + \u201c earns \u201d + empSalary);<\/p>\n<p>}<\/p>\n<p>myStatement.close();<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">myConne ction.close ();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u2026\u2026.<\/span><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">\u00a0 \u00a0Once Connection is established, Statement object is created. This Statement object is used to execute the query. The SQL query that has been tried in this example is an &#8216;Select&#8217; query. Hence the retrieved records are read sequentially and displayed. Then the connection is closed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Simple Program<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The complete executing code for the example is given below.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.sql.*;<\/p>\n<p>public class JDBC<\/p>\n<p>{<\/p>\n<p>public static void main(String []args)throws Exception<\/p>\n<p>{<\/p>\n<p>Class.forName(&#8220;sun.jdbc.odbc.JdbcOdbcDriver&#8221;);<\/p>\n<p>Connection<\/p>\n<p>con=DriverManager.getConnection(&#8220;jdbc:odbc:sample&#8221;);<\/p>\n<p>Statement st=con.createStatement();<\/p>\n<p>String query=&#8221;select * from Student&#8221;;<\/p>\n<p>ResultSet rs=st.executeQuery(query);<\/p>\n<p>while(rs.next())<\/p>\n<p>{<\/p>\n<p>System.out.println(rs.getString(&#8220;name&#8221;));<\/p>\n<p>System.out.println(rs.getInt(&#8220;rollno&#8221;));<\/p>\n<p>System.out.println(rs.getInt(&#8220;mark1&#8221;));<\/p>\n<p>System.out.println(rs.getInt(&#8220;mark2&#8221;));<\/p>\n<p>}<\/p>\n<p>con.close();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Example Program<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This is another example program where the user is allowed to do all database operations such as select, insert, update, delete and clearing the screen. A connection is established to the URL jdbc :odbc:jnf2. The database name is jnf2 and the table name is Student. The user is displayed with a menu of operations. Upon selecting the choice, its corresponding function is called. Functions like display(), clrscr(), insert(), modify() and delete() have been coded.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the function display() the method executeQuery() is called with the Statement object by passing the SQL SELECT statement which displays the records from the table.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the function insert() the method executeUpdate() is called with the Statement object by passing the SQL INSERT statement which inserts a record into the table.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the function modify() the method executeUpdate() is called with the Statement object by passing the SQL UPDATE statement which updates a single record in the table.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the function delete() the method executeUpdate() is called with the Statement object by passing the SQL DELETE statement which deletes a record from the table.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the function clrscr(), a for loop is executed which displays blank for 25 lines thereby it clears the screen.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Within each function a Connection object is created and closed at the end of the function. This is avoid connection errors while executing.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.sql.*;<\/p>\n<p>import java.io.*;<\/p>\n<p>public class JDBC2<\/p>\n<p>{<\/p>\n<p>public static void main(String []args)throws Exception<\/p>\n<p>{<\/p>\n<p>int ch;<\/p>\n<p>Class.forName(&#8220;sun.jdbc.odbc.JdbcOdbcDriver&#8221;);<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">InputStreamReader i = new InputStreamReader(System.in); BufferedReader b = new BufferedReader(i); try<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Connection c;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">c = DriverManager.getConnection(&#8220;jdbc:odbc:jnf2&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">do<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;\\t\\t\\t\\t Menu&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">S<\/span><span style=\"text-align: initial;font-size: 1em\">ystem.out.println(&#8220;\\t\\t\\t\\t 1.View&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;\\t\\t\\t\\t 2.Clear&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;\\t\\t\\t\\t 3.Insert&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;\\t\\t\\t\\t 4.Modify&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;\\t\\t\\t\\t 5.Delete&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;\\t\\t\\t\\t 6.Exit&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Enter your choice:&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ch= Integer.parseInt(b.readLine());<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">switch(ch)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">case 1:<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">display();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">break;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">case 2:<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">clrscr();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">break;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">case 3:<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<br \/>\n<\/span><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0insert();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">display();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">break;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">case 4:<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">modify();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">display();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">break;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">case 5:<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">delete();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">display();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">break;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">case 6:<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.exit(0);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">break;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">default:<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;invalid input&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}}while(ch!=6);}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(Exception e){}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">static void display()<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{\u00a0 try<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ResultSet rs;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">int row=0;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Connection c1;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">c1 = DriverManager.getConnection(&#8220;jdbc:odbc:jnf2&#8221;); Statement st=c1.createStatement();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">rs=st.executeQuery(&#8220;select * from student&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;\\nRegno\\tName\\tClass\\tMark&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">while(rs.next())<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(rs.getInt(1)+&#8221;\\t&#8221;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">+rs.getString(2)+&#8221;\\t&#8221;+rs.getString(3)+&#8221;\\t&#8221;+rs.getInt(4); c1.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(Exception e){}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">static void insert()<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">try<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">int row1;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Statement insst;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Connection c2;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">c2 = DriverManager.getConnection(&#8220;jdbc:odbc:jnf2&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">insst=c2.createStatement();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;9,jnf,vbj,30&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">row1=insst.executeUpdate(&#8220;insert into Student&#8221; + &#8221; values (9,&#8217;jnf&#8217;,&#8217;vbj&#8217;,100)&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">c2.commit();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;No of rows inserted = &#8221; + row1);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(SQLException e){System.out.println(&#8220;error&#8221; + e);}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">static void modify()<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">try<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;6,jnf,vbj,change 30 to 80&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">int row2;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Statement modst;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Connection c3;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">c3 = DriverManager.getConnection(&#8220;jdbc:odbc:jnf2&#8221;); modst=c3.createStatement();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">row2=modst.executeUpdate(&#8220;update student set mark=90 where regno=6&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">c3.commit();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;No of rows update d = &#8221; + row2);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(SQLException e){System.out.println(&#8220;error&#8221; + e);}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">static void delete()<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">try<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;6,jnf,vbj,90&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">int row3;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Statement delst;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Connection c4;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">c4 = DriverManager.getConnection(&#8220;jdbc:odbc:jnf2&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">delst=c4.createStatement();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">row3=delst.executeUpdate(&#8220;delete from student where regno=6&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(row3&gt;0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">c4.commit();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;No of rows update d = &#8221; + row3);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{ System.out.println(&#8220;record not found&#8221;); }<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(SQLException e){System.out.println(&#8220;error&#8221; + e);}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">static void clrscr()<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">for(int k=1;k&lt;25;k++)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">The output of this code is shown in the below Figure 27.10 and Figure 27.11.<\/span><\/p>\n<\/div>\n<p><strong>\u00a0 Results<\/strong><\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-249 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-147.png\" alt=\"\" width=\"584\" height=\"391\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-147.png 584w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-147-300x201.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-147-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-147-225x151.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-147-350x234.png 350w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-250 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-148.png\" alt=\"\" width=\"629\" height=\"392\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-148.png 629w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-148-300x187.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-148-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-148-225x140.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-148-350x218.png 350w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/p>\n<div>\n<p><strong>\u00a0 \u00a0 Transaction Management<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Transactions are not explicitly opened and closed. Instead, the connection has a state called AutoCommit mode. If AutoCommit is true, then every statement is automatically committed. The default case is true.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>setAutoCommit<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Connection.setAutoCommit(boolean)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If AutoCommit is false, then every statement is added to an ongoing transaction. It must explicitly commit or rollback the transaction using Connection.commit() and Connection.rollback().<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Statements<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>There are three types of statements called,<\/p>\n<ul>\n<li>Statement<\/li>\n<li>PreparedStatement<\/li>\n<li>CallableStatement<\/li>\n<\/ul>\n<p style=\"text-align: justify\">The Statement object is used for executing a static SQL statement and obtaining the results produced by it as we have seen earlier.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Prepared Statements<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Prepared Statements are used for queries that are executed many times. They are parsed (compiled) by the DBMS only once. The column values can be set after compilation. Instead of values, \u2018?\u2019 is used and it can be substituted later with actual values.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Querying with PreparedStatement<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>String queryStr =<\/p>\n<p>&#8220;SELECT * FROM employee &#8221; +<\/p>\n<p>&#8220;WHERE id = ? and salary &gt; ?&#8221;;<\/p>\n<p>PreparedStatement pstmt = con.pre pareStatement(queryStr);<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">pstmt.setString(1, \u201cE2001&#8243;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">pstmt.setInt(2, 48000);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ResultSet rs = pstmt.executeQuery();<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify\">\u00a0 In this example code, the SELECT statement is given with &#8216;?&#8217; for the two attributes &#8216;id&#8217; and &#8216;salary&#8217;. PreparedStatement object is created by passing this query string. Then we use methods like setString() and setInt() to pass values for id and salary. Then the query is executed using executeQuery() method.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Updating with PreparedStatement<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>String deleteStr =<\/p>\n<p>\u201cDELET E FROM employee &#8221; +<\/p>\n<p>&#8220;WHERE id = ? and salary &gt; ?&#8221;;<\/p>\n<p>PreparedStatement pstmt = con.pre pareStatement(deleteStr); pstmt.setString(1, \u201cE2001&#8243;);<\/p>\n<p>pstmt.setDouble(2, 48000);<\/p>\n<p>int delnum = pstmt.executeUpdate();<\/p>\n<p><strong>Statements vs. PreparedStatements<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The difference in syntax is given with an example in the below Table 27.2.<\/p>\n<p><strong>Table 27.2 Difference between Statement and PreparedStatement<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-251 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-149.png\" alt=\"\" width=\"646\" height=\"170\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-149.png 646w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-149-300x79.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-149-65x17.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-149-225x59.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-149-350x92.png 350w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p style=\"text-align: justify\"><strong>\u00a0 \u00a0 CallableStatement<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A CallableStatement is used to access the database stored procedures. The CallableStatement interface can also accept runtime input parameters.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A CallableStatement object is created using the Connection.prepareCall() method.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>CallableStatement cstmt = null;<\/p>\n<p>try {<\/p>\n<p>String SQL = &#8220;{call getEmpName (?, ?)}&#8221;;<\/p>\n<p>cstmt = conn.prepareCall(SQL);<\/p>\n<p>. . .<\/p>\n<p>}<\/p>\n<p>catch (SQLException e)\u00a0 { . . . }<\/p>\n<p>finally { . . . }<\/p>\n<p>&nbsp;<\/p>\n<p><strong>ResultSet Meta-Data<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A ResultSetMetaData is an object that can be used to get information about the properties of the columns in a ResultSet object.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>ResultSetMetaData rsmd = rs.getMetaData();<\/p>\n<p>int numcols = rsmd.getColumnCount();<\/p>\n<p>for (int i = 1 ; i &lt;= numcols; i++)<\/p>\n<p>{<\/p>\n<p>System.out.print(rsmd.getColumnLabel(i)+&#8221; &#8220;);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The method getMetaData() is used with the ResultSet object which returns a ResultSetMetaData object. With the ResultSetMetaData object, use the method getColumnCount() which returns the number of\u00a0<span style=\"text-align: initial;font-size: 1em\">columns in every record. Then we can use a for loop that executes for the number of columns to display the column label using the method getColumnLabel().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">Summary<\/strong><\/p>\n<\/div>\n<p style=\"text-align: justify\">\u00a0 \u00a0 This module explains about how to work with SQL. It has explored the working of JDBC with simple programming illustrations. This section also discusses about the transaction management in JDBC and its basic operations that can be performed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Web Links<\/strong><\/p>\n<ul>\n<li><em>https:\/\/pawangkp.files.wordpress.com\/2009\/10\/deawsj-6_<strong>ppt<\/strong>_1b.<strong>ppt<\/strong><\/em><\/li>\n<li><em>https:\/\/www.tutorialspoint.com\/jdbc\/jdbc-statements.htm<\/em><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":25,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-m-vijayalakshmi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-238","chapter","type-chapter","status-publish","hentry","contributor-dr-m-vijayalakshmi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/238\/revisions"}],"predecessor-version":[{"id":584,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/238\/revisions\/584"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/238\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=238"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=238"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}