Practice Exercises Java - Learn to program performing exercises with Java

Practice Exercises Java

Learn to program performing exercises with Java

12 Lessons Java with the Solutions - 228 Exercises Java with the solutions
For Beginners, Intermediates and Advanceds

App Practice Exercises Java

The human knowledge belongs to the world
¡The infomation should be free!

Full acces to a database - Practice Exercises Java

Lesson 10:

Access to relational databases

Exercise 10.3:

Full acces to a database


Create a program to allow the user both entering data about books and surfing the existing data. It must behave correctly if the data file does not exit on start.

Source Code:

import java.util.*;
public class Main
	static void main(String[] args)
		boolean finish = false;
		String option;
		String title, author, genere, sumary;
		SQLiteCommand cmd;

		SQLiteConnection conection = new SQLiteConnection("Data Source=ejemplo01.sqlite;Version=3;New=True;Compress=True;");

		if (!(new"ejemplo01.sqlite")).isFile())

			System.out.println("Creando la base de datos...");

		String creacion = "CREATE TABLE IF NOT EXISTS books(title varchar(20)," +
		" author varchar(20),genere varchar(20), sumary varchar(20));";
		cmd = new SQLiteCommand(creacion, conection);

			System.out.println("Books database");
			System.out.println("1.- Add a new Book");
			System.out.println("2.- View all Books");
			System.out.println("0.- Exit");
			System.out.print("Choose an Option: ");
			option = new Scanner(;

			switch (option)
				case "0":
					finish = true;
				case "1":
					String continueOption;
						System.out.print("Enter the title: ");
						title = new Scanner(;

						System.out.print("Enter the author: ");
						author = new Scanner(;

						System.out.print("Enter the genere: ");
						genere = new Scanner(;

						System.out.print("Enter the sumary: ");
						sumary = new Scanner(;

						String insercion = "insert into books values ('" + title + "'," +
						" '" + author + "', '" + genere + "', '" + sumary + "');";
						cmd = new SQLiteCommand(insercion, conection);

						int cantidad = cmd.ExecuteNonQuery();
						if (cantidad < 1)
							System.out.println("Insert Fails");

						System.out.print("Enter another book (y/n): ");
						continueOption = new Scanner(;
					} while (continueOption.toString().toLowerCase().equals("y"));


				case "2":
					String consulta = "select * from books";
					cmd = new SQLiteCommand(consulta, conection);
					SQLiteDataReader datos = cmd.ExecuteReader();
					int rowCount = 1;
					while (datos.Read())
						title = String.valueOf(datos[0]);
						author = String.valueOf(datos[1]);
						genere = String.valueOf(datos[2]);
						sumary = String.valueOf(datos[3]);

						System.out.printf("%1$s- Title: %2$s," + " Author: %3$s, Genere: %4$s, Sumary: %5$s" + "\r\n", rowCount, title, author, genere, sumary);
		} while (!finish);

Exercisey 10.3

Privacy Policy:

Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.


This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.