Learn Fundamentals of Java Programming “Database Connectivity in Java Programming ” Lesson 27

Database Connectivity In this section, we will learn to connect a MySQL database to a Java program and get back the results from executing a SQL query on the database. Connecting a database to a Java program is easy with NetBeans since it allows us to connect directly to a MySQL server. Connecting to the MySQL Server in NetBeans We first need to configure NetBeans to Register and connect a MySQL...
Continue reading »

Learn Fundamentals of Java Programming “Exception Handling in Java Programming ” Lesson 26

Exception Handling By now you may have written quite a few programs. Some of your programs when executed may have terminated unexpectedly with runtime errors. The errors could have occurred because an array index reference was out of range, an attempt was made to divide an integer by zero, there was insufficient computer memory and so on. Such an error situation that is unexpected in the program execution and causes it...
Continue reading »

Learn Fundamentals of Java Programming “String Manipulation in Java Programming ” Lesson 25

String Manipulation In the previous section, we learned to manipulate arrays using Java prebuilt classes. In this section we learn to manipulate Strings using the String class present in the java.lang package. The first method we will use from the String class is the toUpperCase() method. This method converts a string to all uppercase letters. String myString = “Hello World”; System.out.println(“UpperCase: ” + myString.toUpperCase()); The output of the code given above will...
Continue reading »

Learn Fundamentals of Java Programming “Array Manipulation in Java Programming ” Lesson 24

Array Manipulation In the previous section you learned about some useful prebuilt classes for data input. In this section, we will explore the prebuilt Arrays class from the java.util package for array handling. The Array class has a number of useful methods. Let’s start by using the sort() method to sort an array of integers in ascending order. First we import java.util.Arrays class. Then in the main() method, we just call...
Continue reading »

Learn Fundamentals of Java Programming “User Defined Methods in Java Programming ” Lesson 23

Data Input The programs we have written up to now haven’t been interactive. A program is interactive if it is able to take input from the user and respond accordingly. Let us write a program to take user input. To take user input we use the prebuilt Scanner class. This class is available in the java.util package. First we import this class, import java.util.Scanner; Now we create an object of the...
Continue reading »

Learn Fundamentals of Java Programming “Java Libraries in Java Programming ” Lesson 22

Java Libraries The power of Java comes from the hundreds of Java classes that are already prebuilt and can be used in your programs. To use a prebuilt class and associated methods in those class, all you have to do is to use the keyword import to import the class from the package in which it is contained into your space. The import statements must appear before any class definitions in...
Continue reading »

Learn Fundamentals of Java Programming “Getter and Setter Methods in Java Programming ” Lesson 21

Getter and Setter Methods Private data members of a class cannot be accessed outside the class however, you can give controlled access to data members outside the class through getter and setter methods. A getter method returns the value of a data member. For example we could define a getter method in the Book class for the price data member as given below: double getPrice() { return price; } Similarly, we...
Continue reading »

Learn Fundamentals of Java Programming “Access Modifiers in Java Programming ” Lesson 20

Access Modifiers In the previous section, we allowed access to the data members of the class from outside the class. We created Book objects in the BookTest class and were able to set the object’s data members. However, it is generally not good programming practice to allow data members to be accessed outside the class. By allowing objects to change their data members arbitrarily, we lose control over the values being...
Continue reading »