Found 9150 Articles for Object Oriented Programming

What is the importance of Math.trunc() method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

163 Views

Math.trunc()Unlike Math.floor(), Math.ceil() and Math.round(), Math.trunc() method removes fractional part and gives out only integer part. It doesn't care about rounding the number to the nearest integer. It doesn't even notice whether the value is positive or negative. Its function is only to chop up the fractional part.syntaxMath.trunc(x);parameters - Math.trunc() takes a number as an argument and truncates the fractional part.In the following example, Math.trunc() method truncated the fractional values of the provided positive numbers.ExampleLive Demo var val1 = Math.trunc("1.414"); var val2 = Math.trunc("0.49"); var val3 = Math.trunc("8.9"); document.write(val1); ... Read More

What happens if we define a concrete method in an interface in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:44:28

3K+ Views

Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since all the methods are abstract you cannot instantiate it. To use it, you need to implement this interface using a class and provide body to all the abstract methods int it.Concrete methods in an interfaceAll the methods in an interface must be abstract, you cannot have a concrete method (the one which has body) if you try to do so, it gives you a compile time error saying “interface abstract methods cannot have body”.ExampleIn the following Java program, we are ... Read More

How many ways are there to register a driver in Java?

Smita Kapse
Updated on 11-Apr-2025 13:56:25

2K+ Views

In this article, we will learn different ways to register a driver in Java. JDBC (Java Database Connectivity) is a standard API used to connect Java applications with databases. Before interacting with a database, you must register the JDBC driver so that the DriverManager can recognize and load it.To connect with a database using JDBC, you need to select the driver for the respective database and register the driver. You can register a database driver in two ways: Using Class.forName() method Using the registerDriver() method Register Driver Using Class.forName() Method The ... Read More

JDBC Class.forName vs DriverManager.registerDriver

Anvi Jain
Updated on 30-Jul-2019 22:30:26

5K+ Views

To connect with a database using JDBC you need to select get the driver for the respective database and register the driver. You can register a database driver in two ways −Using Class.forName() method − The forName() method of the class named Class accepts a class name as a String parameter and loads it into the memory, Soon the is loaded into the memory it gets registered automatically.Class.forName("com.mysql.jdbc.Driver");ExampleFollowing JDBC program establishes connection with MySQL database. Here, we are trying to register the MySQL driver using the forName() method.import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class RegisterDriverExample {    public static ... Read More

How do I check to see if a column name exists in a CachedRowSet in JDBC?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

3K+ Views

CachedRowSet interface does not provide any methods to determine whether a particular column exists.Therefore, to find whether a RowSet contains a specific column, you need to compare the name of each column in the RowSet with the required name. To do so −Retrieve the ResultSetMetaData object from the RowSet using the getMetaData() method.ResultSetMetaData meta = rowSet.getMetaData();Get the number of columns in the RowSet using the getColumnCount() method.int columnCount = meta.getColumnCount();The getColumnName() method returns the name of the column of the specified index. Using this method retrieve the column names of the RowSet from index 1 to column count and compare ... Read More

How to determine database type (name) for a given JDBC connection?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

4K+ Views

One way to get the name of the underlying database you have connected with is by invoking the getDatabaseProductName() method of the DatabaseMetaData interface. This method returns the name of the underlying database in String format.Therefore, to retrieve the name of your current database using Java code −Retrieve the DatabaseMetaData object of the current Connection using the getMetaData() method.//Retrieving the meta data object DatabaseMetaData metaData = con.getMetaData();Then, get the product name of the underlying database you have connected to using the getDatabaseProductName() method of the DatabaseMetaData interface as −//retrieving the name of the database String product_name = metaData.getDatabaseProductName();ExampleFollowing JDBC program ... Read More

What are the different ways to print an exception message in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:27:07

9K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Printing the Exception messageYou can print the exception message in Java using one of the following methods which are inherited from Throwable class.printStackTrace() − This method prints the backtrace to the standard error stream.getMessage() − This method returns the detail message string of the current throwable object.toString() − This message prints the short description of the current throwable object.Example Live Demoimport java.util.Scanner;    public class ... Read More

Batch Inserts Using JDBC Statements

Anvi Jain
Updated on 30-Jul-2019 22:30:26

385 Views

Grouping a set of INSERT Statements and executing them at once is known as batch insert.Batch inserts using Statement objectTo execute a batch of insert statements using the Statement object −Add statements to the batch − Prepare the INSERT quires one by one and add them to batch using the addBatch() method of the Statement Interface as shown below −String insert1 = Insert into table_name values(value1, value2, value3, ......); stmt.addBatch(insert1); String insert2 = Insert into table_name values(value1, value2, value3, ......); stmt.addBatch(insert2); String insert3 = Insert into table_name values(value1, value2, value3, ......); stmt.addBatch(insert3);Execute the batch − After adding the required statements, ... Read More

Batch Inserts Using JDBC Prepared Statements

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

5K+ Views

Grouping a set of INSERT Statements and executing them at once is known as batch insert.Batch inserts using PreparedStatement objectTo execute a batch of insert statements using the PreparedStatement object −Create a PreparedStatement − Create a PreparedStatement object using the prepareStatement() method. Pass the Insert query with place holders “?” instead of values as a parameter to this method.PreparedStatement pstmt = con.prepareStatement("INSERT INTO Sales VALUES (?, ?, ?, ?, ?)");Set the values to the place holders − Using the setXXX() methods (setInt(). SetString(), setFloat() etc…) set the values to the place holders in the PrepareStatement as −pstmt.setString(1, "KeyBoard"); pstmt.setString(2, "Amith"); ... Read More

How to store decimal values in a table using PreparedStatement in JDBC?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

To insert records into a table that contains a decimal value using PreparedStatement you need to −Register the driver − Register the driver class using the registerDriver() method of the DriverManager class. Pass the driver class name to it, as parameter.Establish a connection − Connect ot the database using the getConnection() method of the DriverManager class. Passing URL (String), username (String), password (String) as parameters to it.Create Statement − Create a PreparedStatement object using the prepareStatement() method of the Connection interface. Pass the INSERT query with place holders to this method in String format as a parameter.PreparedStatement pstmt = con.prepareStatement("INSERT ... Read More

Advertisements