Database query Learn programming Java

Lesson:

Access To Relational Databases


Exercise:

Database query 46


Objetive:

Create a program to display the data about books which your previous prorgram has stored.


Code:

package ReadSQL;
public class Main
{
	static void main(String[] args)
	{

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

		conexion.Open();

		String consulta = "select * from books";
		SQLiteCommand cmd = new SQLiteCommand(consulta, conexion);
		SQLiteDataReader datos = cmd.ExecuteReader();

		while (datos.Read())
		{
			String name = String.valueOf(datos[0]);
			String autor = String.valueOf(datos[1]);
			String genre = String.valueOf(datos[2]);
			String summary = String.valueOf(datos[3]);

			System.out.printf("Name: %1$s," + " Autor: %2$s, Genre: %3$s, Summary: %4$s" + "\r\n", name, autor, genre, summary);
		}

		conexion.Close();
	}
}