Question

JAVA We are learning about java databases in my highschool java class. I Need some help...

JAVA

We are learning about java databases in my highschool java class.

I Need some help with it.

Create a Java program (feel free to do it all in a main method) that works with a database that contains your 'Contractors' table (see the last exercise).

  • Id (integer, primary key)
  • CompanyName (varchar, 30 characters)
  • Phone(varchar, 12 characters)
  • ContactName(varchar, 30 characters)
  • Rating (integer)
  • OutOfStateService (boolean)
# company name phone # Name rating Out of state service
1 Joe's Brewery 1111111111 Joe 100 true
2 Mary's Brewery 2222222222 Mary 100 false
3 Bob's Brewery 3333333333 Bob 99 true
4 Tim's Brewery 4444444444 Tim 95 true
5 Kim's Bakery 5555555555 Kim 2 true

Your program should have a menu (and associated code) that allows for the following features:

Can display all of the data from the table.

Can read a user-provided field from the table (example, the user asks for names and gets a list of all the names).

Can delete a record. Can add a record.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code


import java.sql.*;

import java.util.Scanner;
public class ContractorsDatabase {
public static Connection con;
public static Statement st;
private static ResultSet rs;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull","root","");
st=con.createStatement();
String companyName,cName,phone,query;
  
int id,choice,choiceColumn,rating,service;
Scanner input=new Scanner(System.in);
while(true)
{
menu();
System.out.print(" Your choice: ");
choice=input.nextInt();
if(choice==1)
{
query="select * from Contractor";
rs=st.executeQuery(query);
System.out.printf("%s%20s%20s%20s%20s%20s ","Id","Company Name","Phone","Contact Name","Rating","Out of state service");
while(rs.next())
{
id=rs.getInt("id");
companyName=rs.getString("CompanyName");
phone=rs.getString("Phone");
cName=rs.getString("ContactName");
rating=rs.getInt("Rating");
service=rs.getInt("OutOfStateService");
String sc;
if(service==1)
sc="true";
else
sc="false";   
System.out.printf("%d%20s%20s%20s%20d%20s ",id,companyName,phone,cName,rating,sc);
}
System.out.println();
}
else if(choice==2)
{
columnMenu();
System.out.print(" Your choice: ");
choiceColumn=input.nextInt();
if(choiceColumn==1)
{
query="select CompanyName from Contractor";
rs=st.executeQuery(query);
System.out.println("Company Names: ");
while(rs.next())
{
companyName=rs.getString("CompanyName");
System.out.println(companyName);
}
}
else if(choiceColumn==2)
{
query="select ContactName from Contractor";
rs=st.executeQuery(query);
System.out.println("Contact Names: ");
while(rs.next())
{
companyName=rs.getString("ContactName");
System.out.println(companyName);
}
}
else if(choiceColumn==3)
{
query="select Phone from Contractor";
rs=st.executeQuery(query);
System.out.println("Phone numbers: ");
while(rs.next())
{
companyName=rs.getString("Phone");
System.out.println(companyName);
}
}
else
{
System.out.println("Invalid choice!!!");
}
System.out.println();
}
else if(choice==3)
{
System.out.println("Enter ID: ");
id=input.nextInt();
System.out.println("Enter name of the company: ");
companyName=input.next();
System.out.println("Enter name Puone number: ");
phone=input.next();
System.out.println("Enter contacnt name: ");
cName=input.next();
System.out.println("Enter the rating: ");
rating=input.nextInt();
System.out.println("Out of state service? (1 for Yes 0 for No)");
service=input.nextInt();
query="insert into Contractor values('"+id+"','"+companyName+"','"+phone+"','"+cName+"','"+rating+"','"+service+"');";
st.executeUpdate(query);
System.out.println(" Record inserted successfully!! ");
}
else if(choice==4)
{
System.out.println("Enter ID of the record you want to delete: ");
id=input.nextInt();
query="DELETE FROM Contractor WHERE id='"+id+"'";
st.executeUpdate(query);
System.out.println(" Record deleted successfully!! ");
}
else if(choice==5)
{
break;
}
else
System.out.println("Invalid choice!!! ");
}
  
}
public static void menu()
{
System.out.println("1. Display All the data 2. Display Perticular Column 3. Add New Record 4. Delete Record 5. Exit");
}
public static void columnMenu()
{
System.out.println("1. Company Name 2. Contact Name 3 Phone Number ");
}
}

database snaps

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
JAVA We are learning about java databases in my highschool java class. I Need some help...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA sql Code an sql file that creates a 'Contractors' table. See the exercise below for...

    JAVA sql Code an sql file that creates a 'Contractors' table. See the exercise below for an example. Give your 'Contractors' table the following fields Id (integer, primary key) CompanyName (varchar, 30 characters) Phone(varchar, 12 characters) ContactName(varchar, 30 characters) Rating (integer) OutOfStateService (boolean) Also have the sql file create at least five records for the "Contractors" table. Example record: 1, Tom's Contracting, 555-555-5555, Tom Thumb, 81, true import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestDB...

  • In Java, define classes for a LikeYelp class that manages reviews about stores. You need to...

    In Java, define classes for a LikeYelp class that manages reviews about stores. You need to fill in the LikeYelp class and outline supporting classes. For the supporting classes, you need to provide the signatures for the methods and enough instance variables to support the methods, but you do not need to write the code for the methods. You need to write the entire LikeYelp class so it supports the following Add a store, which has a name and an...

  • I need the following written in Java please, thank you ' We want you to implement...

    I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...

  • Hello I need some help with my CC+ project. My current project erasing the first two...

    Hello I need some help with my CC+ project. My current project erasing the first two phone numbers and only printing the 3rd phone number out in the list. Any idea on how to correct. Here is the error. Contacts.h #ifndef Contact_H #define Contact_H #include<string> using namespace std; class ContactNode{ public: ContactNode(); ContactNode(string name, string phone); void InsertAfter(ContactNode*); string GetName(); string GetPhoneNumber(); ContactNode* GetNext(); void PrintContactNode(); private: string contactName; string contactPhoneNum; ContactNode* nextNodePtr; }; #endif Contacts.cpp #include <iostream> #include "Contacts.h"...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • Hello, I need help answering all 8 of these questions for my BIS 422 class. I...

    Hello, I need help answering all 8 of these questions for my BIS 422 class. I need the appropriate MySQL script to use for these questions Provide the SQL for the following data requests. Your SQL should be written as a single script that can be pasted into MySQL and run without edits. Make sure you use the proper notation for your comments (see the practice solution for an example of the format). There will be a 5% deduction for...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Looking for some help with this Java program I am totally lost on how to go about this. You have been approached by a local grocery store to write a program that will track the progress of their sale...

    Looking for some help with this Java program I am totally lost on how to go about this. You have been approached by a local grocery store to write a program that will track the progress of their sales people (SalesPerson.java) The information needed are: D Number integer Month 1 Sales Amount-Double Month 2 Sales Amount-Double Month 3 Sales Amount-Double Write a Java program that allows you to store the information of any salesperson up to 20 While the user...

  • Java 1 Some help Please...... 1. Before You Begin Anticipate where things can go wrong Consider...

    Java 1 Some help Please...... 1. Before You Begin Anticipate where things can go wrong Consider how to gracefully shut down the program and save the users data and close files properly. Typically there are two scenarios to make a distinction between: The first is one that which you have absolutely no control over; such as if all the files are where they should be, and that the user has not deleted one or accidentally moved it rather than copied...

  • C++ Hey, I really need help with this lab. I don't understand the function part at...

    C++ Hey, I really need help with this lab. I don't understand the function part at all. It'll be great way for me to understand. Than you Functions and loops: Write a program that will ask the user which type of triangle they would like to draw on the characters. The choices are screen with left-top-big left-bottom-big right-top-big right-bottom-big Get the user's choice, ask how big it should be, and then draw the shape. After each successful drawing, ask the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT