Please use the exact parameters below for Java (SQLite). I have included code that almost works, to see if someone can h

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Please use the exact parameters below for Java (SQLite). I have included code that almost works, to see if someone can h

Post by answerhappygod »

Please use the exact parameters below for Java (SQLite).I have included code that almost works, to see if someonecan help me with my syntax errors. Would very much like to try andunderstand. Or if you have a cleaner, better way, please let meknow. Just need to insert where it says, "Your codehere".
ONE OF THE ERRORS it is giving me is that the tableis not being shown as created:
Error message:
2: Unit test
0 / 3
Test createTable() creates Horse table
Test feedback
Id column does not exist Name column does not exist Breed columndoes not exist Height column does not exist BirthDate column doesnot exist
THE SECOND ERROR is showing that I have a newline at thevery end of the output. I need it to just end and not havewhitespace.
Error Message:
4: Compare output
0 / 3
Output is nearly correct; but whitespace differs. See highlightsbelow.
Your output
All horses: (1, 'Babe', 'Quarter Horse', 15.3,'2015-02-10')
Expected output
All horses: (1, 'Babe', 'Quarter Horse', 15.3, '2015-02-10')
Thanks in advance!
5.9 LAB - Database programming with Java (SQLite)
Complete the Java program to create a Horse table, insert onerow, and display the row. The main program calls four methods:
Complete all four methods. Method parameters are described inthe template. Do not modify the main program.
The Horse table should have five columns, with the followingnames, data types, constraints, and values:
The program output should be:
This lab uses the SQLite database rather than MySQL. Both SQLiteand MySQL Connector/J implement the JDBC API. Consequently, the APIis as described in the text, with a few exceptions:
SQLite reference information can be found at SQLite JavaTutorial, but is not necessary to complete this lab.
My Attempted Answer:
import java.sql.*;
public class LabProgram { // Create a connection to a sqlite in-memorydatabase // Returns Connection object public static Connection createConnection() {
// YOUR CODE HERE // Use connection string"jdbc:sqlite::memory:" Connection conn = null; try { conn =DriverManager.getConnection("jdbc:sqlite::memory:"); } catch (SQLException e) { System.out.println("Errorcreating connection"); e.printStackTrace(); } return conn;}
// Create Horse table // Parameter conn is database connection created increateConnection() public static void createTable(Connection conn){ // YOUR CODE HERE
String sql = "CREATE TABLE IF NOT EXISTSHorse (id integer primary key, name text, breed text, heightdouble, birthdate text)";
try { Statement stmt =conn.createStatement(); stmt.execute(sql); } catch (SQLException e) { System.out.println(e.getMessage()); } }
// Insert row into Horse table using aparameterized query // Parameter conn is database connection created increateConnection() // Parameters id, name, breed, height, and birthDatecontain values to be inserted public static void insertHorse(Connection conn, intid, String name, String breed, double height, String birthDate){ // YOUR CODE HERE String sql = "INSERT INTO HorseVALUES(?,?,?,?,?)"; try { PreparedStatement stmt =conn.prepareStatement(sql); stmt.setInt(1, id); stmt.setString(2, name); stmt.setString(3, breed); stmt.setDouble(4, height); stmt.setString(5,birthDate); stmt.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } }
// Select and print all rows of Horse table // Parameter conn is database connection created increateConnection() public static void selectAllHorses(Connection conn){ // YOUR CODE HERE String sql = "SELECT Id, Name, Breed, Height,BirthDate FROM Horse";
try { Statement stmt =conn.createStatement(); ResultSet rs =stmt.executeQuery(sql); while (rs.next()) { System.out.println("Allhorses:" + "\n" + "(" + rs.getInt("Id") + ", '"+ rs.getString("Name") +"', '" + rs.getString("Breed") +"', " + rs.getDouble("Height") +", '" + rs.getString("BirthDate")+ "')"); } } catch (SQLException e) { System.out.println(e.getMessage()); } } // DO NOT MODIFY main public static void main(String[] args) {
// Create connection to sqlite in-memorydatabase 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 tablerows selectAllHorses(conn); }}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply