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!

SQL to text - Practice Exercises Java

Lesson 8:

File management

Exercise 8.39:

SQL to text


Debes crear un programa en Java capaz de analizar órdenes INSERT de lenguaje SQL y de extraer sus datos a líneas de texto independientes, de la siguiente forma: si el fichero de entrada contuviera estas tres líneas:

insert into personas (nombre, direccion, edad) values ('smith, pedro', 'su calle', 23);
insert into personas (nombre, direccion, edad) values ('juan', 'calle cinco, 6', 24);
insert into ciudades (codigo, nombre) values ('a', 'alicante');

el fichero resultante debería tener en cada línea el nombre de un campo, seguido de “dos puntos” y de su valor. Además, cada registro deberá estar precedido del nombre de la tabla y seguido por una línea en blanco, así:

nombre: smith, pedro
direccion: su calle
edad: 23

nombre: juan
direccion: calle cinco, 6
edad: 24

codigo: a
nombre: alicante

Source Code:

package SQL2text;
import java.util.*;

public class Main
	public static void main(String[] args)
	{ ficheroEntrada = null;
		String linea;
		String nombre;

		System.out.println("Not enough parameters!");
		System.out.print("Enter file name: ");
		nombre = new Scanner(;

			ficheroEntrada = new;
		catch (RuntimeException e)

				linea = ficheroEntrada.ReadLine();
				if (linea != null)
					String tableName = linea.substring(12).split("[ ]", -1)[0];
					String[] campo = tangible.StringHelper.substring(linea, linea.indexOf("(") + 1, linea.indexOf(")") - linea.indexOf("(") - 1).split("[,]", -1);
					String[] valores = tangible.StringHelper.substring(linea, linea.indexOf("values (") + 9, linea.indexOf(");") - linea.indexOf("values (") - 9).split("[,]", -1);

					for (int i = 0; i < campo.length; i++)
						System.out.print(campo[i].trim() + ": ");
						System.out.println(valores[i].trim().replace("'", ""));
			} while (linea != null);
		catch (RuntimeException e)
		new Scanner(;
Exercisey 8.39

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.