Question

In: Computer Science

Create a table called product containing a product number, company name, model number, product name. What...

Create a table called product containing a product number, company name, model number, product name. What is my primary key? Which datatypes should I use? Please submit a printout of the commands used for creating the above table and the results of your queries/commands.

PLEASE USE JAVA & H2DATABASE

Solutions

Expert Solution

Solution:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Product {

static final String driver = "org.h2.Driver";   
static final String url = "jdbc:h2:~/test";

static final String user = "sa";
static final String pass = "";
  
public static void main(String[] args) {
  
Connection con = null;
Statement s1 = null;
  
try {

Class.forName(driver);
con = DriverManager.getConnection(url,user,pass);

s1 = con.createStatement();

String sql = "CREATE TABLE product " + "(productnumber INTEGER not NULL, " + " companyname VARCHAR(255), " + " modelnumber VARCHAR(255), " + " productname VARCHAR(255), " + " PRIMARY KEY ( productnumber ))";
  
s1.executeUpdate(sql);
s1.close();
con.close();

}
  
catch(Exception e) {
e.printStackTrace();
}
  
catch(SQLException e1) {
e1.printStackTrace();
}
  
finally {

try{
if(s1!=null)
s1.close();
}

catch(SQLException se2) {
}

try {
if(con!=null)
con.close();
}

catch(SQLException sqe){
sqe.printStackTrace();
}
}
}
}

----------------------------------------------------------------------------------------------------------------------------------------

This is the code using Java and H2 database

The datatypes which I've used are as follows:

product number: Integer as it'll be easy and I am declaring that as a primary key so it'll be not null and unique

company name, model number, product name: VARCHAR as it might contain alphanumeric values.

Code in which the QUERY is given for the creation of table:

String sql = "CREATE TABLE product " + "(productnumber INTEGER not NULL, " + " companyname VARCHAR(255), " + " modelnumber VARCHAR(255), " + " productname VARCHAR(255), " + " PRIMARY KEY ( productnumber ))";

In the String sql, the query is stored.


Related Solutions

IN SQL Create a table called product containing a product no, company name, model_no,product name. What...
IN SQL Create a table called product containing a product no, company name, model_no,product name. What is my primary key? Which datatypes should I use?Pleasesubmit a printout of the commands used for creating the above table and the resultsof your queries/commands
In a Package called characterArray, ********JAVA CODE********** 1) Create a char array containing your full name...
In a Package called characterArray, ********JAVA CODE********** 1) Create a char array containing your full name and print it out. 2) Create an uninitialized char array and copy into it      the char array containing your full name and print it out. 3) Create a Character array and copy into it the char array containing      your full name and print it out. 4) Into the Character array, use toUpperCase() to copy the char array containing     your full name...
create a model of buyer behavior for the product. Next, create a model for an organizational...
create a model of buyer behavior for the product. Next, create a model for an organizational buyer vs. an individual consumer.
Create a table with two columns. Name the table First Initial _ Last Name (e.g. John...
Create a table with two columns. Name the table First Initial _ Last Name (e.g. John Dow will create table j_dow). You have to audit all DML statements on your table. To do this you write two triggers: 1. To log any DML statements that users might run on this table. The results must be stored in the First Initial _ Last Name _ Log table (e.g. John Dow will create table j_dow_log). The table should have unique event ID,...
Create following table. CREATE TABLE Registration (Reg_ID number(5), Name Varchar2(20), Address Varchar2(20), create_date date, created_by varchar2(10)...
Create following table. CREATE TABLE Registration (Reg_ID number(5), Name Varchar2(20), Address Varchar2(20), create_date date, created_by varchar2(10) ); Create an audit trial report on Employee table for all insert, update and delete operations on given table. You have to create audit table first with Current Date, Operation and User to record the information.
Create a struct MenuItem containing fields for name (a string) and price (a float) of a...
Create a struct MenuItem containing fields for name (a string) and price (a float) of a menu item for a diner. Create a ReadItem() function that takes an istream and a MenuItem (both by reference) and prompts the user for the fields of the MenuItem, loading the values into the struct's fields. Create a PrintItem() function that takes an output stream (by reference) and a MenuItem (by value) and prints the MenuItem fields to the stream in a reasonable one-line...
Create a Word file containing the following: The full name of the “test subject” The test...
Create a Word file containing the following: The full name of the “test subject” The test subject is a person, not yourself, who can say the names of the products. The best test subject is one who is adamant about being an expert who can distinguish brand A from B. You cannot be the test subject because you generate the sequence, and the test subject cannot see it for an unbiased test. Pets and babies are not allowed as they...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
The ABC Company builds a computer model called ICU2. The product structure diagram of the ICU2...
The ABC Company builds a computer model called ICU2. The product structure diagram of the ICU2 model is shown in the figure below. Suppose that the forecasted demands of the computer over weeks 6 to 11 are 220, 165, 180, 120, 75, 300. The starting inventory of assembled computers in week 6 will be 75, and the production managers anticipated returns of 30 in week 8 and 10 in week 10. ICU2 Computer (end item)     - Disk Drives (2)...
1) Create a table “college” that has as attributes name, city name, state/province/region name, country name,...
1) Create a table “college” that has as attributes name, city name, state/province/region name, country name, and year in which the college was established as well as an ID as primary key. Insert records for 5 colleges of your choice, including one or more you have attended. 2) Create a table “student” that has as attributes first name, last names, and college ID for students, and insert the names of yourself and friends who attended one or more colleges together...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT