Skip to main content

Posts

How to Connect MySQL Database in Java

Connect MySQL Database in Java Since Java is one of the more popular programming languages, it naturally offers strong support for databases. Using JDBC (Java Database Connectivity), we are able to establish connections to numerous databases. Here, you'll learn how to use JDBC to communicate with a database while running queries. Introduction JDBC is the default Java API for establishing connections to common relational databases. Here, you'll learn how to utilize Java as well as JDBC to connect to MySQL databases hosted on Azure.   In this post, we'll cover two different kinds of authentication: those based on Azure Active Directory (Azure AD) as well as those based on MySQL. To see how Azure Active Directory authentication works, click the Passwordless tab, while the Password tab displays MySQL authentication.   Linking to Azure Database for MySQL with an Azure Active Directory account is possible via the Azure AD authentication mechanism. Using Azure Active Directory (A

Queen's Attack II HackerRank Solution in Java with Explanation

Queen's Attack II Problem's Solution in Java (Chessboard Problem)   Problem Description : You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.  A queen is standing on an n * n chessboard. The chess board's rows are numbered from 1 to n, going from bottom to top. Its columns are numbered from 1 to n, going from left to right. Each square is referenced by a tuple, (r, c), describing the row r and column c, where the square is located. The queen is standing at position (r_q, c_q). In a single move, queen can attack any square in any of the eight directions The queen can move: Horizontally (left, right) Vertically (up, down) Diagonally (four directions: up-left, up-right, down-left, down-right) The queen can move any number of squares in any of these directions, but it cannot move through obstacles. Input Format : n : The size of the chessboard ( n x n ). k : The number of obstacles.

Find Non-Divisible Subset from Given Array | BlogOnCode

Java Solution for finding Non Divisible Subset in Given Array Problem Description : Given a set of distinct integers, print the size of a maximal subset of S where the sum of any 2 numbers in S' is not evenly divisible by k. Example 1 : S = [19, 10, 12, 10, 24, 25, 22], k = 4 Output = 3 One of the arrays that can be created is S'[0] = [10, 12, 25] Another is S'[1] = [19, 22, 24]. After testing all permutations, the maximum length solution array has 3 elements. Example 2 : S = [1, 7, 2, 4], k = 3 Output = 3 [1, 7, 4] Example 3 : S = [278, 576, 496, 727, 410, 124, 338, 149, 209, 702, 282, 718, 771, 575, 436], k = 7 Output = 11 In simple word, we have to find maximum subset of current array where it can not be divisible by given k. Lets jump on code. Solution 1 : Java Solution for finding Non Divisible Subset in Given Array import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.funct

Want to Crack Java Coding Interview? Here is 4 Steps to follow - BlogonCode

Coding Interview in Java - Top four things to follow As a Developer, we all have to go through coding interview for any level job. So here we will seen how we can crack coding interviews. Point to prepare before going to coding interview in Java : Java in-built classes and its methods Java collection framework and its hierarchy Data structure and algorithms Follow coding standards 1. Java in-built classes and its methods Java have so many in-built classes and its method like, String, StringBuffer, StringBuilder, Array, etc. These classes and its methods is so useful in our every coding life. So before coding interview, we must have to gain knowledge about classes and its frequently used methods. String : length(), split(), charAt(), indexOf(), equals(), trim(). Array : sort(), length (not a method). Math : abs(), pow(), max(), min(). System   Scanner These are just a few examples of Java in-built classes and their methods. Java has a rich set of classes and libraries to help us to per

Check if two strings after processing backspace character are equal or not

Java Solution for Backspace String Compare Problem Description : Given two strings s1 and s2, in which backspaces are represented by #. The task is to determine whether the resultant strings after processing the backspace character would be equal or not. Example 1 : Input : s1 = "abcd#" s2 = "abc" Output : true Explanation : s1 become "abc" after backspace d character and s2 is already "abc" Example 2 : Input : s1 = "abc#" s2 = "abc" Output : false Explanation : s1 become "ab" after backspace c character and s2 is "abc" Example 3 : Input : s1 = "ab##c" s2 = "ac" Output : false Explanation : s1 become "c" after backspace and s2 is "ac" Example 4 : Input : s1 = "abcdefg" s2 = "abcdefi#g" Output : false Here we have to delete last character if '#' (backspace) present in given String s1 or String s2. After that compare both string and if s1 and

Java Program for Find Minimum Element in Rotated Sorted Array

Search Minimum number in Rotated and Sorted Array in Java using Binary Search Problem Description : Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [5, 6, 7, 0, 1, 2, 4] if it was rotated 3 times. [2, 4, 5, 6, 7, 0, 1] if it was rotated 5 times. Example 1 : Input : [5, 6, 7, 0, 1, 2, 4] Output : 0 Example 2 : Input : [5, 8, 10, 15, 20, 50, 100, 200] Output : 5 Example 3 : Input : [100, 50, 25, 5, 1] Output : 1 We will use Binary Search for finding solution. Solution 1 : Find Minimum Element in Rotated Sorted Array in Java   import   java . util . Scanner ;   public   class   FindMinimumInRotatedSortedArray   {      public   static   void   main ( String []   args )   {                   Scanner   sc   =   new   Scanner ( System . in );          System . out . println ( "Enter array size : " );          int   size   =   sc . nextInt ();          int []   array   =   new  

How to Implement One to Many and Many to One Mapping in Spring Boot using JPA

Spring Boot CRUD example using One-to-Many and Many to One mapping | With Thymeleaf User Interface In this tutorial, we will learn how to use @OneToMany and @ManyToOne annotation using JPA (Java Persistent API) in Spring Boot. We also attach Thymeleaf for User Interface. In past tutorial, we already created Spring Boot CRUD with Rest API, JPA and MySql. Please refer that one first, we will continue from there. Spring Boot application with Thymeleaf, Rest API, JPA and MySql Database    For applying One to Many relationship, we need another POJO class. In past we already created Book class, now we will create new class Author . As we know Author have multiple Books, so we can easily apply One to Many operation. Lets create POJO class for Author and apply @OneToMany on Book .  Define List of Book and apply @OneToMany annotation on field. We are using mappedBy property, so Author table does not create new column.  We already learn about mappedBy property in One-to-One annotation. Please r