Write a Java program that uses a library database of books and patron data, as d
ID: 3537303 • Letter: W
Question
Write a Java program that uses a library database of books and patron data, as
described in Exercise R22.2. Patrons should be able to check out and return books.
Supply commands to print the books that a patron has checked out and to find who
has checked out a particular book. Create and populate Patron and Book tables
before running the program.
Here is Exercise R22.2:
Write a Java program that creates a Car table with car manufacturers, models, model
years, and fuel efficiency ratings. Insert several cars. Print out the average fuel efficiency.
Use CREATE TABLE, INSERT, and SELECT AVG SQL commands.
Explanation / Answer
Class.forName(...); Connection conn = DriverManager.getConnection(...); Statement stat = conn.createStatement(); stat.execute("create table test(id int primary key, name varchar(255))"); stat.execute("insert into test values(1, 'Hello')"); ResultSet rs; rs = stat.executeQuery("select * from test"); while (rs.next()) { System.out.println(rs.getString("name")); } rs.close(); stat.close(); conn.close();PreparedStatement prep; prep = conn.prepareStatement("insert into account(name) values(?)"); prep.setString(1, "Joe"); prep.execute(); prep.close();String userName = "user name"; String password = "user password"; String url = "jdbc:mysql://Hostname/DatabaseName"; Class.forName ("com.mysql.jdbc.Driver").newInstance (); conn = DriverManager.getConnection (url, userName, password);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.