Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java program to create a table named FoodItem that contains the following columns : ID (number), Name(varchar), calories(number). Now create an insert statement

Write a Java program to create a table named FoodItem that contains the following columns : ID (number), Name(varchar), calories(number).

Now create an insert statement to insert a record into this database table.

Finally using a select statement print the record that you entered into the database.\

or explain how to use that two lines that make it outcome

import java.util.*; import java.sql.*;

class FoodDetails { public static void main(String args[]) { try { Scanner sc=new Scanner(System.in); int id,calories; String name;

/* If you want MySql : then use following two lines: Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/food,"root",""); //food - database name */

//Using JAVA(JDBC) & MS ACCESS(Database)

Connection con=DriverManager.getConnection("jdbc:ucanaccess://D:/food.accdb"); Statement smt=con.createStatement(); smt.executeUpdate("create table FoodItem (ID number,Name varchar(30),Calories number)"); System.out.println("Table created Successfully!!");

System.out.print("Enter ID : "); id=sc.nextInt(); System.out.print("Enter Name : "); name=sc.next(); System.out.print("Enter Calories : "); calories=sc.nextInt();

smt.executeUpdate("insert into FoodItem values('"+id+"','"+name+"'),'"+calories+"'"); System.out.println("Record Inserted successfully!!");

ResultSet rs=smt.executeQuery("select * from FoodItem");

while(rs.next()) { System.out.println("ID : "+rs.getInt("ID")); System.out.println("Name : "+rs.getString("Name")); System.out.println("Calories : "+rs.getInt("Calories")); } rs.close(); smt.close(); con.close(); } catch(Exception e1) { System.out.println("Exception is "+e1); } } }

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

Introduction To Constraint Databases

Authors: Peter Revesz

1st Edition

1441931554, 978-1441931559

More Books

Students also viewed these Databases questions

Question

Are some psychological functions more localized than others?

Answered: 1 week ago