Get Column Name from ResultSet in Java with MySQL

Samual Sam
Updated on 30-Jul-2019 22:30:24

720 Views

To get the column name on the result set, you need to use getMetaData() method. The prototype of getMetadata() is as follows −ResultSetMetaData getMetaData throws SQLException;Create a MySQL table with 5 column names. The query to create a table is as follows −mysql> create table javagetallcolumnnames    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> Age int,    -> Salary float,    -> Address varchar(100),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (1.34 sec)The following is the Java code that gets the column name on ResultSet. The code ... Read More

Round Number to Fewer Decimals in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

154 Views

Let’s say we need to round number to 3 decimal places, therefore use the following format −DecimalFormat decFormat = new DecimalFormat("0.000");Now, format the number −decFormat.format(37878.8989)Since we have used the DecimalFloat class above, therefore do import the following package −import java.text.DecimalFormat;The following is an example that rounds a number to 3 decimal places −Example Live Demoimport java.text.DecimalFormat; public class Demo {    public static void main(String[] args) {       DecimalFormat decFormat = new DecimalFormat("0.000"); System.out.println("Result = " + decFormat.format(37878.8989)); } }OutputResult = 37878.899Let us see another example that rounds a number ... Read More

Display Year with SimpleDateFormat yyyy in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

558 Views

Using the SimpleDateFormat(“yyyy”) to display year −// displaying year Format f = new SimpleDateFormat("yyyy"); String strYear = f.format(new Date()); System.out.println("Year = "+strYear);Since, we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date −import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();       SimpleDateFormat simpleformat = new SimpleDateFormat("E, ... Read More

Generate Row Count and Serial Number of Records in MySQL Query

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

5K+ Views

To generate serial number i.e. row count in MySQL query, use the following syntax.SELECT @yourVariableName − = @yourVariableName+1 anyAliasName,    yourColumnName1, yourColumnName2, yourColumnName3, ....N from yourTableName ,    (select @yourVariableName − = 0) as yourVariableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table tblStudentInformation    -> (    -> StudentName varchar(20),    -> StudentAge int,    -> StudentMathMarks int    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into tblStudentInformation values('Carol', ... Read More

Trees Used for Furniture and Long-Lasting Wood Types

Ridhi Arora
Updated on 30-Jul-2019 22:30:24

4K+ Views

There are various trees used for furniture, depending on their desired characteristics. For example, in order to get the durable material, some particular type of wood is used, while in order to get a luster and shine in the product, a different wood is used.Different Trees Used for FurnitureOak Trees: These trees are famous for hard-grade wood red, or white color. Its property of absorbing any color is evident from the fact that it has large annual rings inside.Maple Trees: The good thing about Maple is that it is less expensive than most of the alternatives of the same hardness ... Read More

Is Null a Literal in Java?

Arushi
Updated on 30-Jul-2019 22:30:24

821 Views

The null in Java is a literal of the null type. It cannot be cast to a primitive type such as int. float etc. but can be cast to a reference type. Also, null does not have the value 0 necessarily.A program that demonstrates null in Java is given as follows:Example Live Demopublic class Demo {    public static void main(String args[]) {       String str = null;       System.out.println("Value of str is: " + str);    } }OutputValue of str is: nullNow let us understand the above program.In the main() method, the String str is initialized ... Read More

The extends Keyword in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

5K+ Views

An object can acquire the properties and behaviour of another object using Inheritance. In Java, the extends keyword is used to indicate that a new class is derived from the base class using inheritance. So basically, extends keyword is used to extend the functionality of the class.A program that demonstrates the extends keyword in Java is given as follows:Example Live Democlass A {    int a = 9; } class B extends A {    int b = 4; } public class Demo {    public static void main(String args[]) {       B obj = new B();     ... Read More

MySQL CONCAT to Create Column Names for Queries

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

818 Views

To create column names to be used in a query, you need to use a user-defined variable with the set command. The syntax is as follows −SET @anyVariableName := (    SELECT CONCAT    (       "SELECT",    GROUP_CONCAT(CONCAT(" 1 as ", COLUMN_NAME) SEPARATOR ', '), " FROM DUAL")    FROM INFORMATION_SCHEMA_COLUMNS    WHERE TABLE_NAME= ‘yourTableName’ );Now prepare the statement using the PREPARE command. The syntax is as follows −PREPARE anyVariableName from @anyVariableName;Execute statement using EXECUTE command. The syntax is as follows −EXECUTE anyVariableName;Deallocate the prepared statement using DEALLOCATE command. The syntax is as follows −DEALLOCATE PREPARE anyVariableName; ... Read More

Copy Array from Specified Source Array in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

174 Views

Use arraycopy() method in Java to copy an array from the specified source array.Here, we have two arrays −int arr1[] = { 10, 20, 30, 40}; int arr2[] = { 3, 7, 20, 30};Now, we will use the arraycopy() method to copy the first two elements of the 1st array to the 2nd array −System.arraycopy(arr1, 0, arr2, 2, 2);The following is an example −Example Live Demoimport java.lang.*; public class Demo { public static void main(String[] args) { int arr1[] = { 10, 20, 30, 40}; int arr2[] = { 3, 7, 20, 30}; System.arraycopy(arr1, 0, arr2, 2, 2); System.out.print("New Array = "); System.out.print(arr2[0] + " "); System.out.print(arr2[1] + " "); System.out.print(arr2[2] + " "); System.out.print(arr2[3] + " "); } }OutputNew Array = 3 7 10 20

Display Three-Letter Month Value with SimpleDateFormat in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

4K+ Views

Using the SimpleDateFormat(“MMM”) to display three-letter month −Format f = new SimpleDateFormat("MMM"); String strMonth = f.format(new Date()); System.out.println("Month (Three-letter format) = "+strMonth);Since, we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date −import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();       SimpleDateFormat simpleformat = new SimpleDateFormat("E, ... Read More

Advertisements