Add an object to a linked list and a database
I have an assignment due next week and could really do with the help. At the moment i am trying to add an object (book) to a linked list and to the database.
I am not able to work out how to add the object to the database.
Main program - Code asking he user to input the book details. It connects to the database.
Code java:
else if (choice == 2)
{
String userid=" ";
String password=" ";
String host=" ";
String database=" ";
String url = "jdbc:mysql://...";
Connection conn = DriverManager.getConnection(url, userid, password);
Statement stmt = conn.createStatement( );
String query = "SELECT * FROM books";
ResultSet rs = stmt.executeQuery(query);
System.out.print("Enter Book ID: ");
id = in.next();
System.out.print("Enter Book Title: ");
title = in.next();
System.out.print("Enter Author's First Name: ");
firstName = in.next();
System.out.print("Enter Author's Last Name: ");
lastName = in.next();
System.out.print("Enter Price: ");
price = Double.parseDouble(in.next());
System.out.print("Enter Year: ");
year = Integer.parseInt(in.next());
Book b2 = new Book(id, title, firstName, lastName, price, year);
list.add(b2);
System.out.println("");
System.out.println("Added following book");
System.out.println("Title: " + b2.getTitle());
System.out.println("Author's First Name: " + b2.getFirstName());
System.out.println("Author's Last Name: " + b2.getLastName());
System.out.println("Price: " + b2.getPrice());
System.out.println("Year: " + b2.getYear());
}
DoubleLinkedList program - Code that adds book to linked list.
Code java:
public void add(Book b)
{
Node newNode=new Node();
newNode.data=b;
if(current==null)
{
newNode.next=first;
newNode.prev=null;
if(first!=null)
{
first.prev=newNode;
}
else
{
last=newNode;
}
first=newNode;
current=newNode;
}
else if(current!=null)
{
newNode.prev=current;
newNode.next=current.next;
if(current==last)
{
last=newNode;
}
else
{
current.next.prev=newNode;
}
current.next=newNode;
}
numElements++;
}