 So now let us go back to, I wanted to do just a little bit more on JDBC, we already saw the basics of yesterday, we saw SQL injections, the last couple of things I want to wrap up on JDBC are the metadata features. So what is metadata? It is data about data, what are the relations, what are the attributes and so forth. So JDBC lets you access metadata through fairly simple interface, there are two kinds of metadata, one is when you have executed a query, I want metadata about the query result, what are the attributes in the query result and so forth. For that, when I execute a query, remember I get back a result set. So on that result set, let us say RS, I can say get metadata and that returns an object of type result set metadata. Now on that object, I can execute various functions to get at actual data. For example, since this is a result of a query, I want to know what are all the columns in the result and what are the types. First I need to know how many columns there are. So this result set metadata object, if I say get column count on that, it returns a number of columns and so I am looping over those columns for i equal to 1 to that and then I am printing get column name i, RSMD.get column name i and RSMD.get column type name of i that prints the column name and the type of it. So that is one. Why is it useful? For example, you have used let us say PgAdmin or Eclipse or NetBeans, it is possible to connect to a database as you have done in PgAdmin, browse what are the relations there, what are the indices, what are the attributes of the relations, all that is enabled through the metadata interface. PgAdmin is probably written in C or so and may be using ODBC but which has exactly the same set of features. If you write it in Java, it is using JDBC metadata features. The second kind of metadata, so this was for the result of a query. The second kind is for what all relations are there in the database, what are the attributes and so forth. For this, when you open the database connection, you can execute connection.getMetadata and the result is an object of type, database metadata. So DBMD is this object. Now on that, I can do various things. Now the key thing to note here is unlike for single result set metadata where there is some number of columns, here there are many relations, they in turn have attributes and so on. So the way JDBC provides access to it is by a function called getColumns which basically gives you various piece of it. So if you go here, the first one null I think is the catalog name which is not used in PostgreSQL I think. The next is the database name Univ DB or whatever else you use when you created the database. The next one is the relation name and the last one is the column name. Now if you notice for column name I have said percent. That means get me all the columns of this relation. I could also say percent for relation name, means gets all the relations in this and then there are other functions for getting indices and other and integrity constraints. So I have not shown all the features here but there are methods on database metadata to access all of those. And the basic way you do it here is you get many results. So what you get back is a result set and that result set has a number of columns including catalog name, relation name, column name and so on. So I am looping this while loop loops over the members of that result set and rs.getString column name gets the name of the column and then rs.getString type name gets the type of that column. Similarly table name and other such fields are available, the other columns. So you can see what all tables there are in the database. Any questions about this? Total columns. It returns a set containing whatever you want. Suppose there are the four columns or four attributes and four attributes will be the return. Suppose I would like to want only two attributes. So can we get with the help of the metadata? I do not know if instead of percent you can give a column prefix or something maybe but you can always go over the result set and extract the columns that you want. So it gives you all the information, you can pick what you want from it. Suppose we are able to get the specified number of the columns but I would like to suppose want the specific columns. So how actually we can get that? Here for the columns I said percent which means get all the columns. If I want a specific column I give a column name there instead of percent and then what I will get is there are extra things in that result set. Here I am getting the type name. So if I give the column what information do I want? Maybe the type, I think there may be length and there are other fields in there. I do not remember the full list but you can get a lot of information about the column. If you want it for just one column, you can choose whether you want to get it for one table all columns, all tables and so forth. Sir in Oracle there is a Describe and Space table name to see the structure of the table. What is there in Postgres to see the structure of the table? In Postgres QL there is I think slash D right. But it is giving all table names not structure of the table that is what all columns are there. So first of all PSQL has a bunch of slash commands which can let you get at the details. Today who uses the command line? Not much. If you go to PGA admin it is all there. PGA admin in turn is using these APIs to get at it. That is the more important thing than the PSQL. I did not focus on PSQL because I do not really expect too many people to use it. You can use it, you are most welcome to learn more. It is useful when I remote SSH to a machine and I cannot get X running. So I cannot see the GUI then I can do this. But it is usually fine to open up PGA admin on my machine and connect to the remote machine. So it is usually not an issue. But sometimes the remote machine may disable remote connections for security. And then SSHing in and running PSQL can be useful. So when I was actually trying to create tables with constraints in PGA admin. It was throwing out problems when I gave the foreign key constraints. That was some typecasting problem. The same thing when it was done in PSQL it was getting created. Why exactly is that? I think I will have to see what the specific thing was. I am not sure. So I have seen this happening when you use a third party tool. If you use NetBeans to connect to Oracle and run commands, you can do that. But I have seen many cases where a valid Oracle command will cause problems in NetBeans. But PGA admin is tied to PostgreSQL so I am not sure why this happened. So if you give the specific thing maybe we can look deeper into it. Sir in Oracle normally if you do not give any name for the constraint it will by default assign some system. Yes all the databases do that. Even in PostgreSQL also it can happen. So how do we find those constraint names that have been given using the same metadata information? So there are other functions on get metadata to go over the constraints. So I am not sure it here but you can look it up offline. Of course PGA admin directly gives you that. But if you want to do it from a program and get access to it, it is available. Okay. I think the last significant thing which I want to mention is transaction control in JDBC. I mentioned this earlier when I said what is a transaction. And I told you that when you run an SQL command most databases will immediately commit it. But if I want to let us say do a transaction which involves multiple steps in the database and have all of them succeed or none of them succeed. Why does it matter? What if there is a power failure in the middle? You could have an inconsistent state. So if I want all the things to be done or none to be done then I have to use a transaction. And JDBC supports API for transactions. By default every single SQL statement is immediately committed. But what you can do is say a connection once you open the JDBC connection to a database. Connection.set auto commit to false. What does that do? After you do that every SQL command which you issue is not committed at that point. Until you do a connection.commit. If you do not want to commit if something went wrong you can do connection.rollback. So if you are building an application which does multiple updates you need to use this wherever there is a single transaction with multiple updates. So there is other stuff on embedded SQL. In this chapter there is stuff on procedural extensions to SQL. I am asking about the type of JDBC driver that we have to use. The type of JDBC driver. Yeah. So generally we won't prefer type one. So either we will go for thin drivers or new drivers. Yeah. So yesterday I tried for the same but the things are not working. If it's possible just give some idea about the same. So if you look at the older stuff on JDBC there is a lot of stuff about the type of the driver and so on. These are details which we don't really need to worry about anymore. So for each database has a current recommended driver more or less. And that generally works without any issues. So for Oracle there is the JDBC thin driver. For PostgreSQL so we have made those available. Other databases have their corresponding drivers. So if you go to their websites just find out which driver they recommend and use that. So there is usually no issue as long as you get one which is compatible to the version of Java. Java itself has been evolving. Generally bytecode is compatible but sometimes the API version which you are using JDBC API you have to get a driver for the corresponding API version or a newer one. So as long as you get that it should not cause any problem. Thank you sir.