Database query Java Exercise - Java Programming Course

 Exercise

Database query

 Objetive

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

 Example 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();
	}
}


Juan A. Ripoll - Programming Tutorials and Courses © 2024 All rights reserved.  Legal Conditions.