Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.sql . * ; public class LabProgram { / / Create a connection to a sqlite in - memory database / / Returns Connection
import java.sql;
public class LabProgram
Create a connection to a sqlite inmemory database
Returns Connection object
public static Connection createConnection
Connection conn null;
try
Class.forNameorgsqlite.JDBC;
conn DriverManager.getConnectionjdbc:sqlite::memory:";
catch ClassNotFoundException SQLException e
eprintStackTrace;
return conn;
Create Horse table
Parameter conn is database connection created in createConnection
public static void createTableConnection conn
String sql "CREATE TABLE IF NOT EXISTS Horse
Id INTEGER PRIMARY KEY NOT NULL,
Name TEXT,
Breed TEXT,
Height DOUBLE,
BirthDate TEXT
;;
try
Statement stmt conn.createStatement;
stmtexecutesql;
catch SQLException e
eprintStackTrace;
Insert row into Horse table using a parameterized query
Parameter conn is database connection created in createConnection
Parameters id name, breed, height, and birthDate contain values to be inserted
public static void insertHorseConnection conn, int id String name, String breed, double height, String birthDate
String sql "INSERT INTO HorseId Name, Breed, Height, BirthDate VALUES;
try
PreparedStatement pstmt conn.prepareStatementsql;
pstmtsetInt id;
pstmtsetString name;
pstmtsetString breed;
pstmtsetDouble height;
pstmtsetString birthDate;
pstmtexecuteUpdate;
catch SQLException e
eprintStackTrace;
Select and print all rows of Horse table
Parameter conn is database connection created in createConnection
public static void selectAllHorsesConnection conn
String sql "SELECT FROM Horse";
try
Statement stmt conn.createStatement;
ResultSet rs stmtexecuteQuerysql;
System.out.printlnAll horses:";
while rsnext
int id rsgetIntId;
String name rsgetStringName;
String breed rsgetStringBreed;
double height rsgetDoubleHeight;
String birthDate rsgetStringBirthDate;
System.out.println id name breed height birthDate ;
catch SQLException e
eprintStackTrace;
DO NOT MODIFY main
public static void mainString args
Create connection to sqlite inmemory database
Connection conn createConnection;
Create Horse table
createTableconn;
Insert row into Horse table
insertHorseconn "Babe", "Quarter Horse", ;
Select and print all Horse table rows
selectAllHorsesconn;
Your test produced
no output.
:Unit test
Test createTable creates Horse table
Your test produced
no output.
:Unit test
Test insertHorse inserts row into Horse table
Your test produced
no output.
:Compare output
Output is nearly correct, but whitespace differs. See highlights below.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started