Question


This lab asks you to write simple JDBC programs, and execute an XPath query. What you will need A MySQL server installation A

CREATE TABLE DEPT (

DEPTNO INTEGER NOT NULL,

DNAME VARCHAR(14),

LOC VARCHAR(13),

PRIMARY KEY (DEPTNO));

INSERT INTO DEPT VALUES (10,'SPORTS','NEW YORK');

INSERT INTO DEPT VALUES (20,'HOME','DALLAS');

INSERT INTO DEPT VALUES (30,'OUTDOOR','CHICAGO');

INSERT INTO DEPT VALUES (40,'CLOTHING','BOSTON');

CREATE TABLE EMP (

EMPNO INTEGER NOT NULL,

ENAME VARCHAR(10),

JOB VARCHAR(9),

MGR INTEGER,

SAL FLOAT,

COMM FLOAT,

DEPTNO INTEGER NOT NULL,

FOREIGN KEY (DEPTNO) REFERENCES DEPT (DEPTNO),

FOREIGN KEY (MGR) REFERENCES EMP(EMPNO),

PRIMARY KEY (EMPNO));

INSERT INTO EMP VALUES (7839,'KING','PRESIDENT',NULL, 5000,NULL,10);

INSERT INTO EMP VALUES (7698,'BLAKE','MANAGER',7839,2850,NULL,30);

INSERT INTO EMP VALUES (7782,'CLARK','MANAGER',7839,2450,NULL,10);

INSERT INTO EMP VALUES (7566,'JONES','MANAGER',7839,2975,NULL,20);

INSERT INTO EMP VALUES (7654,'MARTIN','SALESMAN',7698,1250,1400,30);

INSERT INTO EMP VALUES (7499,'ALLEN','SALESMAN',7698,1600,300,30);

INSERT INTO EMP VALUES (7844,'TURNER','SALESMAN',7698,1500,0,30);

INSERT INTO EMP VALUES (7900,'JAMES','CLERK',7698,950,NULL,30);

INSERT INTO EMP VALUES (7521,'WARD','SALESMAN',7698,1250,500,30);

INSERT INTO EMP VALUES (7902,'FORD','ANALYST',7566,3000,NULL,20);

INSERT INTO EMP VALUES (7369,'SMITH','CLERK',7902,800,NULL,20);

INSERT INTO EMP VALUES (7788,'SCOTT','ANALYST',7566,3000,NULL,20);

INSERT INTO EMP VALUES (7876,'ADAMS','CLERK',7788,1100,NULL,20);

INSERT INTO EMP VALUES (7934,'MILLER','CLERK',7782,1300,NULL,10);

CREATE TABLE PRODUCT (

PRODID INTEGER,

PRICE FLOAT,

MADE_BY INTEGER NOT NULL,

DESCRIP VARCHAR(30),

PRIMARY KEY (PRODID),

FOREIGN KEY (MADE_BY) REFERENCES DEPT (DEPTNO));

INSERT INTO PRODUCT VALUES ('60', 39.99, 10, 'ACE TENNIS RACKET I');

INSERT INTO PRODUCT VALUES ('61', 49.99, 10, 'ACE TENNIS RACKET II');

INSERT INTO PRODUCT VALUES ('70', 9.99, 30, 'TENT STAKES');

INSERT INTO PRODUCT VALUES ('71', 24.49, 20, 'BATH TOWELS');

INSERT INTO PRODUCT VALUES ('90', 19.99, 40, 'TENNIS SHIRT');

INSERT INTO PRODUCT VALUES ('80', 70.19, 30, 'HIKING BOOTS');

INSERT INTO PRODUCT VALUES ('63', 8.99, 20, 'WELCOME MAT');

INSERT INTO PRODUCT VALUES ('30', 199.37, 10, 'CALLAWAY IRONS');

INSERT INTO PRODUCT VALUES ('76', 14.99, 30, 'SB ENERGY BAR-6 PACK');

INSERT INTO PRODUCT VALUES ('81', 12.99, 30, 'SB VITA SNACK-6 PACK');

CREATE TABLE CUSTOMER (

CUSTID INTEGER NOT NULL,

PID INTEGER NOT NULL,

NAME VARCHAR(45),

QUANTITY INTEGER,

FOREIGN KEY (PID) REFERENCES PRODUCT (PRODID),

PRIMARY KEY (CUSTID));

INSERT INTO CUSTOMER VALUES (96711, 60, 'Joe Sith', 3);

INSERT INTO CUSTOMER VALUES (94061, 61, 'Luke Moonwalker', 1);

INSERT INTO CUSTOMER VALUES (95133, 70, 'Darth Marth', 7);

INSERT INTO CUSTOMER VALUES (97544, 71, 'Yoda Yoga', 1);

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

Answer)

1. JBDC Code :

import java.sql.*;
public class MyJDBC {
//url
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/test";
static final String UNAME = "uname";
static final String PWD = "pwd";

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//connection
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, UNAME, PWD);
stmt = conn.createStatement();

//query
   String sql = "SELECT EMPNO, ENAME, DNAME FROM EMP E, DEPT D on E.DEPTNO=E.DEPTNO";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
String empId = rs.getInt("EMPNO");
String empName = rs.getInt("ENAME");
String deptName = rs.getString("DNAME");

//printing result
System.out.print("EMP ID: " + empId);
System.out.print(", EMP NAME: " + empName);
System.out.print(", DEPT NAME: " + deptName);
}
rs.close();
}
catch(Exception e){
       e.printStackTrace();
}
//closing statement and connection
finally{
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
CREATE TABLE DEPT ( DEPTNO INTEGER NOT NULL, DNAME VARCHAR(14), LOC VARCHAR(13), PRIMARY KEY (DEPTNO)); INSERT...
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
  • -- echo Building demo tables. Please wait. DROP TABLE EMP CASCADE CONSTRAINTS; DROP TABLE DEPT; DROP...

    -- echo Building demo tables. Please wait. DROP TABLE EMP CASCADE CONSTRAINTS; DROP TABLE DEPT; DROP TABLE SALGRADE; DROP TABLE Prod CASCADE CONSTRAINTS; DROP TABLE Vend; CREATE TABLE DEPT ( DEPTNO NUMBER(2) NOT NULL, DNAME CHAR(14), LOC CHAR(13), CONSTRAINT DEPT_PRIMARY_KEY PRIMARY KEY (DEPTNO)); INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK'); INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS'); INSERT INTO DEPT VALUES (30,'SALES','CHICAGO'); INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON'); CREATE TABLE EMP ( EMPNO NUMBER(4) NOT NULL, ENAME CHAR(10), JOB CHAR(9), MGR NUMBER(4) CONSTRAINT EMP_SELF_KEY...

  • SQL I have a database CREATE TABLE vendor ( vid CHAR(2) NOT NULL, vname VARCHAR(25) NOT...

    SQL I have a database CREATE TABLE vendor ( vid CHAR(2) NOT NULL, vname VARCHAR(25) NOT NULL, PRIMARY KEY (vid) ); CREATE TABLE category ( catid CHAR(2) NOT NULL, catname VARCHAR(25) NOT NULL, PRIMARY KEY (catid) ); CREATE TABLE product ( pid CHAR(3) NOT NULL, pname VARCHAR(25) NOT NULL, price NUMERIC (7,2) NOT NULL, vid CHAR(2) NOT NULL, categoryid CHAR(2) NOT NULL, PRIMARY KEY (pid)); CREATE TABLE region ( rid CHAR NOT NULL, rname VARCHAR(25) NOT NULL, PRIMARY KEY (rid)...

  • create table candidate ( cand_id   varchar(12) primary key,   -- cand_id name       varchar(40)           --...

    create table candidate ( cand_id   varchar(12) primary key,   -- cand_id name       varchar(40)           -- cand_nm ); create table contributor ( contbr_id   integer primary key, name       varchar(40),           -- contbr_nm city     varchar(40),           -- contbr_city state       varchar(40),           -- contbr_st zip       varchar(20),           -- contbr_zip employer   varchar(60),           -- contbr_employer occupation   varchar(40)           -- contbr_occupation ); create table contribution ( contb_id   integer primary key, cand_id   varchar(12),           --...

  • Create an XML document for the Oracle Starter Database Tables (i.e., emp, dept, ord, product, price)....

    Create an XML document for the Oracle Starter Database Tables (i.e., emp, dept, ord, product, price). Each XML document must be in the format as described in the "Mapping tables to XML" section on pg. 1161 (any column that does not have a value can be displayed as empty, e.g., <DEGREE></DEGREE> ). The name for each document will be the name of the table followed by ".xml" as the file extension (e.g., "emp.xml"). Data for dept. table CREATE TABLE DEPT...

  • Database Management 6. [5] Create the following table: CREATE TABLE customer ( cust_name VARCHAR(30) NOT NULL,...

    Database Management 6. [5] Create the following table: CREATE TABLE customer ( cust_name VARCHAR(30) NOT NULL, address VARCHAR(60), UNIQUE (cust name, address)); A. Run the following inserts and explain why both work and how will you prevent it INSERT INTO customer VALUES ('Alex Doe', NULL); INSERT INTO customer VALUES ('Alex Doe', NULL); 7. [5] Modify the following table definition to ensure that all employees have a minimum wage of $10 CREATE TABLE employee ( empId INT PRIMARY KEY, empName VARCHAR(40)...

  • Given: Create table Book ( Book_id integer, Book_title varchar(50), Author varchar(50), Publish_date date, Type varchar(30), Edition...

    Given: Create table Book ( Book_id integer, Book_title varchar(50), Author varchar(50), Publish_date date, Type varchar(30), Edition number, Quantity number, Primary key (Book_id) ); insert into Book values (1,'The Old Man and the Sea','Hemingway' ,date '1978-1-1','hardcopy',3,2); insert into Book values (2,'The Old Man and the Sea','Hemingway' ,date '1979-1-1','hardcopy',4,1); insert into Book values (3,'The Old Man and the Sea','Hemingway' ,date '1980-1-1', 'hardcopy',5,10); insert into Book values (4,'A Farewell to Arms','Hemingway' ,date '1986-1-1','hardcopy',2,18); insert into Book values (5,'For Whom the Bell Tolls','Hemingway' ,date...

  • please answer 56789 CREATE TABLE ALLDRINKS(   /* All legal drinks */ DRINK       VARCHAR(30)   NOT NULL,  ...

    please answer 56789 CREATE TABLE ALLDRINKS(   /* All legal drinks */ DRINK       VARCHAR(30)   NOT NULL,   /* Drink name   */    CONSTRAINT DRINKNAME_PKEY PRIMARY KEY(DRINK) ); CREATE TABLE DRINKERS ( /* All drinkers */ DRINKER   VARCHAR(30)   NOT NULL,    CONSTRAINT DRINKERS_PKEY PRIMARY KEY (DRINKER)); CREATE TABLE LOCATED(   /* Pubs have locations */ PUB           VARCHAR(30)   NOT NULL,   /* Pub name   */ STREET       VARCHAR(30)   NOT NULL,   /* Street name   */ BLDG_NO       DECIMAL(4)   NOT NULL,   /* Building number   */...

  • drop table department cascade constraints; create table department ( Dname   varchar2(15)   not null, Dnumber int   not...

    drop table department cascade constraints; create table department ( Dname   varchar2(15)   not null, Dnumber int   not null, Mgr_ssn   char(9)   not null, mgr_start_date   Date, primary key (Dnumber), Unique    (Dname)); insert into DEPARTMENT values ('Research', '5', '333445555', '22-May-1988'); insert into DEPARTMENT values ('Administration', '4', '987654321', '01-Jan-1995'); insert into DEPARTMENT values ('Headquarters', '1', '888665555', '19-Jun-1981'); drop table employee cascade constraints; create table employee ( Fname   varchar2(15) not null, Minit   char(1), Lname   varchar2(15) not null, Ssn   char(9), Bdate   date, Address   varchar2(30), Sex   char(1),...

  • The database(or the tables) are follow: CREATE TABLE employees ( id SERIAL NOT NULL PRIMARY KEY,...

    The database(or the tables) are follow: CREATE TABLE employees ( id SERIAL NOT NULL PRIMARY KEY, name TEXT NOT NULL, salary REAL NOT NULL DEFAULT 25000.0 ); CREATE TABLE employee_audit_log ( employee_id INTEGER NOT NULL, occurred_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); INSERT INTO employees(name, salary) VALUES ('Arnold Schwarznegger', 35000), ('Yuri Gargarin', 27000), ('Anakin Skywalker', 450000), ('Said Faroghi', 15000), ('Zino Holwerda', 8500); 1. Create a trigger for the employees table so that a new row is inserted in employee audit_log...

  • CREATE TABLE customer (    id INTEGER PRIMARY KEY,    customer_name TEXT,    contact_number TEXT );...

    CREATE TABLE customer (    id INTEGER PRIMARY KEY,    customer_name TEXT,    contact_number TEXT ); CREATE TABLE supplier (    id INTEGER PRIMARY KEY,    supplier_name TEXT UNIQUE,    contact_number TEXT ); CREATE TABLE product (    id INTEGER PRIMARY KEY,    supplier_id INTEGER REFERENCES supplier(id),    product_name TEXT,    product_price INTEGER,    UNIQUE(supplier_id, product_name) ); CREATE TABLE purchase (    id INTEGER PRIMARY KEY,    customer_id INTEGER REFERENCES customer(id),    purchase_date REAL,    store_id INTEGER REFERENCES store(id) );...

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