Answered step by step
Verified Expert Solution
Link Copied!

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 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.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

3. Use the childs name.

Answered: 1 week ago

Question

4 Name four appraisal methods.

Answered: 1 week ago

Question

8 What problems can occur with appraisal?

Answered: 1 week ago