One of the common tasks for every programmer is to compare two files and find out the difference between them. You would do this while comparing the same file from different release versions or from the different environments to find out exactly what has changed. Though there are a lot of good tools already exists to compare two files e.g. diff command in UNIX, Win Merge, and Beyond compare in Windows, they are external tools. Beyond compare is not even free, but to be frank it's worth money. I would like to compare two files directly from Eclipse to avoid switching to another program for a simple task.
Tuesday, August 3, 2021
How to Connect to MySQL database in Java with Example
In this tutorial, You will learn how to connect to MySQL database from Java program and running SELECT and INSERT queries to retrieve and update data with step by step guide. In order to connect and access the MySQL database from Java, you can use JDBC (Java Database Connectivity) API, which is bundled in JDK itself. JDBC allows you to connect to any database like Oracle, SQL Server, or MySQL, provided you have the vendor's implementation of the JDBC driver interface, which is required to connect the database. You can connect to MySQL from Java by using MySQL's Type 4 JDBC driver which is bundled in mysql-connector-java-5.1.23-bin.jar. It's a type 4, pure Java driver, which means you don't need any native library or JDBC ODBC bridge, all you need is to put this JAR in your classpath.
Labels:
JDBC
How to Generate Random Numbers in Java Between Range - Example Tutorial
In software development and programming world, we often need to generate random numbers, sometimes random integers in a range e.g. 1 to 100, etc. Thankfully, Random number generation in Java is easy as Java API provides good support for random numbers via java.util.Random class, Math.random() utility method, and recently ThreadLocalRandom class in Java 7, along with more popular features like String in Switch and ARM blocks. While random() method seems the most convenient way of generating randoms in Java it only returns random doubles, on the other hand by using Random, you can generate pseudo-random integer, floating-point numbers e.g. double and even random boolean values.
Labels:
coding
,
core java
,
programming
Eclipse Not Able To Connect Internet, Market Place - Configure Connection Proxy Settings - Example
If you are using Eclipse in your company, you might have faced issues, where Eclipse is not able to connect to the internet. Since most companies use a proxy for connecting to the Internet, it's important to configure proxy settings in Eclipse to allow Eclipse to access the Internet behind the organization's firewall. If your company regularly changes its internet proxy settings then this also needs to be updated whenever your Proxy server changes. Since Eclipse needs an internet connection for connecting to Eclipse Marketplace, installing and updating plugins, downloading Maven dependencies from the remote maven repository, it's really frustrating when Eclipse just hung up, shows connecting, and time out after a long time.
Labels:
core java
,
Eclipse
,
Eclipse Tips
,
programming
How to connect to Microsoft SQL Server database using Eclipse - Example Tutorial
Though I prefer SQL Server Management Studio to access and work with the Microsoft SQL Server database, Sometimes, it's better to connect the MSSQL database directly from Eclipse. This will save a lot of time which is wasted on switching between two applications, Eclipse and SSMS. It will also keep your PC fast enough because less application means less overhead. Eclipse IDE allows you to connect to almost all the databases you have heard e.g. Oracle, MySQL, PostgreSQL, DB2, etc.
Labels:
core java
,
Eclipse
,
JDBC
,
Microsoft SQL Server
Monday, August 2, 2021
How to use Java 1. 7 Multiple Catch Block with example - Tutorial
As the release of JDK 7 approaching General Availability (GA) on 2011/07/28, I thought to have a gorftlook on language enhancement as part of project coin, also called as Small language enhancements or JSR 334. Though there are not any major changes like Enum or Generics of Java 1.5, but they are still very useful, in terms of simplifying your day to day programming task. Some of the interesting changes are allowing String in Switch cases, inclusion of fork-join framework in JDK itself , type inference using a diamond operator, automatic resource management using try with resource feature, and ability to catch multiple Exception in the single catch block .
Fixing Unsupported major.minor version 52.0 Error in Java
Unsupported major.minor version 52.0 comes when you are trying to run a class compiled using Java 1.8 compiler into a lower JRE version e.g. JRE 1.7 or JRE 1.6. Simplest way to fix this error is install the latest Java release i.e. Java 8 and run your program. If upgrading to Java 8 is not an option then make sure you use Java compiler's cross compilation feature and compile source code for lower Java version by using -target option of javac command. If you have multiple JRE installed make sure that the latest one comes first in PATH environment variable.
Labels:
error and exception
How to Fix java.sql.SQLException: No suitable driver found for jdbc:jtds:sqlserver? Solution
java.sql.SQLException: No suitable driver found for
There are two ways to connect Microsoft SQL Server from Java program, either by using Microsoft's official JDBC driver (sqljdbc4.jar) or by using jTDS driver (jtds.jar). This error comes when your supplied database URL didn't match with the JDBC driver present in the CLASSPATH. Many programmers who usually use jtds.jar, make a mistake while using sqljdbc4.jar by adding "microsoft" in JDBC URL. That makes URL invalid and JDBC API throws "java.sql.SQLException: No suitable driver: sqljdbc4.jar" error.
There are two ways to connect Microsoft SQL Server from Java program, either by using Microsoft's official JDBC driver (sqljdbc4.jar) or by using jTDS driver (jtds.jar). This error comes when your supplied database URL didn't match with the JDBC driver present in the CLASSPATH. Many programmers who usually use jtds.jar, make a mistake while using sqljdbc4.jar by adding "microsoft" in JDBC URL. That makes URL invalid and JDBC API throws "java.sql.SQLException: No suitable driver: sqljdbc4.jar" error.
Labels:
error and exception
,
JDBC
Top 10 MQ Series Interview Questions Answers - WebSphere and Active MQ
MQ Series Interview Questions and Answers are collections of some of the questions asked on various MQ implementations e.g. IBM WebSphere MQ, Active MQ or Sonic MQ from different core Java and Enterprise Java (JEE) interviews. Most of these questions are from WebSphere MQ as it's the one that is most popular in large organizations, especially in the finance domain. By the way messaging is a key aspect of any enterprise application and you will always see some form of messaging between different systems in an organization. For example, In the finance world, most of the middle office to back office communication happens over messaging middle-ware like TIBCO or MQ Series.
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver - cause and solution
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver Exception comes when you try to connect the Oracle database from Java program and Oracle driver is not available in Classpath. We have already seen How to connect Oracle database from Java program and found that, In order to connect to Oracle database, we load and register driver using Class.forName("oracle.jdbc.driver.OracleDriver") and this code loads a class at runtime using Reflection which throws ClassNotFoundException if the class in question e.g. "oracle.jdbc.driver.OracleDriver" is not found.
Labels:
core java
,
error and exception
,
JDBC
2 ways to solve Unsupported major.minor version 51.0 error in Java
Unsupported major.minor version 51.0 error comes when you run a class file created using Java 1.7 (major version 51.0) into a lower JRE version e.g. JRE 6, 5, or 4. There are two ways to solve this problem, first make sure you run your Java program in the same or higher version of JRE, on which it has compiled, and second use the cross-compilation option to create a class file compatible to a lower JRE. You can use javac -target option to create a class file for all JRE up-to-the JDK versions you are using for compilation.
Labels:
error and exception
Sunday, August 1, 2021
How to Fix java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Java Spring MVC? Solution
The java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet error comes when you deploy a Spring MVC application into Tomcat or Jetty and Servlet container not able to found this class, which usually found in the spring-webmvc.jar file. The DispatcherServlet is the core of Spring MVC framework, it implements the FrontController pattern and intercepts all HTTP requests sent to your Web application and subsequently routes it to the correct controller based upon URL mapping.
Labels:
error and exception
,
spring
How to format JSON String in Java - Jackson Example Tutorial
You can format JSON String in Java using Jackson API's pretty print feature. As you might have noticed in my previous JSON tutorials that the output of the programs is not properly formatted, which makes them hard to read, especially in large log files where there are so many other texts, both JSON and normal text is there. That's why it's advised to print JSON String properly formatted because then it will stand out in log file or console. Whenever we print JSON String from Java Programs by using method writeValueAsString(), it usually comes in one line, as shown in the following example :
Labels:
Jackson
,
Java JSON tutorial
,
JSON
How to get JAR files of Jackson Libary for JSON Processing
If you are using Jackson to parse a JSON String in Java first time and wondering where can you get the Jackson-related JARs, or which JAR files are really need then you have come to the right place. It can be really confusing for a new user to set up or include Jackson library in their Java projects sometimes, especially, if you are not very familiar with the structure of Jackson projects, various JAR files, and in particular Maven. Since Jackson is the most popular open-source library for processing JSON responses e.g. generating Java objects from JSON received from RESTful Web services or generating JSON String from Plain Old Java Objects (POJO). It is the go-to library for any JSON need because it offers a lot of features without compromising on performance.
Labels:
Jackson
,
Java JSON tutorial
How to Escape JSON String in Java- Eclipse IDE Tips and Example
While working or JSON parsing in Java application it's quite common to just copy-paste a JSON String from some resources e.g. a RESTful web service and then use the Jackson library to parse the JSON. This is the quickest way to test and learn parsing JSON string in Java, but the main problem with this approach is that the JSON String also includes double quotes "" which is also used for enclosing String literals in Java. Since JSON String itself contains double quotes, when you just copy-paste them in your IDE or Java source file, the didn't work as expected. If you remember, if your String contains double quotes then those need to be escaped when you are pasting them as String literals in your Java code.
Labels:
Eclipse
,
Eclipse Tips
,
Java JSON tutorial
Subscribe to:
Posts
(
Atom
)