Question: 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 in-memory database
// Returns Connection object
public static Connection createConnection(){
Connection conn = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite::memory:");
} catch (ClassNotFoundException | SQLException e){
e.printStackTrace();
}
return conn;
}
// Create Horse table
// Parameter conn is database connection created in createConnection()
public static void createTable(Connection 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();
stmt.execute(sql);
} catch (SQLException e){
e.printStackTrace();
}
}
// 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 insertHorse(Connection conn, int id, String name, String breed, double height, String birthDate){
String sql = "INSERT INTO Horse(Id, Name, Breed, Height, BirthDate) VALUES(?,?,?,?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setString(3, breed);
pstmt.setDouble(4, height);
pstmt.setString(5, birthDate);
pstmt.executeUpdate();
} catch (SQLException e){
e.printStackTrace();
}
}
// Select and print all rows of Horse table
// Parameter conn is database connection created in createConnection()
public static void selectAllHorses(Connection conn){
String sql = "SELECT * FROM Horse";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("All horses:");
while (rs.next()){
int id = rs.getInt("Id");
String name = rs.getString("Name");
String breed = rs.getString("Breed");
double height = rs.getDouble("Height");
String birthDate = rs.getString("BirthDate");
System.out.println("("+ id +",'"+ name +"','"+ breed +"',"+ height +",'"+ birthDate +"')");
}
} catch (SQLException e){
e.printStackTrace();
}
}
// DO NOT MODIFY main
public static void main(String[] args){
// Create connection to sqlite in-memory database
Connection conn = createConnection();
// Create Horse table
createTable(conn);
// Insert row into Horse table
insertHorse(conn,1, "Babe", "Quarter Horse", 15.3,"2015-02-10");
// Select and print all Horse table rows
selectAllHorses(conn);
}
} Your test produced
no output.
2:Unit test
Test createTable() creates Horse table
Your test produced
no output.
3:Unit test
Test insertHorse() inserts row into Horse table
Your test produced
no output.
4:Compare output ???
Output is nearly correct, but whitespace differs. See highlights below.
 import java.sql.*; public class LabProgram { // Create a connection to

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!