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)...

  • emp table in bellow: 5. Query data from emp table for each non-deptno 20 employees to...

    emp table in bellow: 5. Query data from emp table for each non-deptno 20 employees to display the ename and calculate the number of months between today and the date the employee was hired. Label the column heading to Ename and Months_Worked. Order your results by the number of months employed. Round the number of months to a whole number. Months _Worked - - - - - - - - Ename MILLER KING JAMES MARTIN TURNER CLARK BLAKE WARD ALLEN...

  • CREATE TABLE person ( pid INTEGER NOT NULL ,pname VARCHAR(30) NOT NULL ,PRIMARY KEY (pid) );...

    CREATE TABLE person ( pid INTEGER NOT NULL ,pname VARCHAR(30) NOT NULL ,PRIMARY KEY (pid) ); CREATE TABLE organization ( oid INTEGER NOT NULL ,oname VARCHAR(30) NOT NULL ,PRIMARY KEY (oid) ); CREATE TABLE venue ( vid INTEGER NOT NULL ,area CHAR(1) NOT NULL ,capacity INTEGER NOT NULL ,PRIMARY KEY (vid) ); CREATE TABLE calendar ( vid INTEGER NOT NULL ,date DATE NOT NULL ,price NUMERIC(6,2) NOT NULL ,PRIMARY KEY(vid,date) ,FOREIGN KEY(vid) REFERENCES venue(vid) ); CREATE TABLE event ( eid...

  • 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...

  • CREATE TABLE actor( id INTEGER NOT NULL, name VARCHAR(100), PRIMARY KEY (id) ); SELECT name FROM...

    CREATE TABLE actor( id INTEGER NOT NULL, name VARCHAR(100), PRIMARY KEY (id) ); SELECT name FROM actor WHERE name LIKE 'M%'; Q. How to run explain plan on the above query and display its output?

  • 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...

  • CREATE TABLE Bill( bill_id char(15) PRIMARY KEY, bill_name VARCHAR(150) NOT NULL, dateOfVoteCommittee date, dateOfVote date, isPassed...

    CREATE TABLE Bill( bill_id char(15) PRIMARY KEY, bill_name VARCHAR(150) NOT NULL, dateOfVoteCommittee date, dateOfVote date, isPassed tinyint(1) CHECK (isPassed IN (0,1)), date_signed date, isVeto tinyint(1) CHECK (isVeto IN (0,1)), dateOfVote_override date, proposed_person_id INT, committee_id INT, foreign key(proposed_person_id) REFERENCES Congressman(person_id), foreign key(committee_id) REFERENCES Committee(committee_id) ); INSERT INTO Bill (bill_id, bill_name, dateOfVote, isPassed, date_signed, isVeto, dateOfVote_override, proposed_person_id, commitee_id) VALUES(1,'SB2 Florida Statutes',DATE'2019-03-27',1,DATE'2019-03-27',0,23,);   *I am getting this error - mysql Mysql doesn't like the date entries in the insert statement. Schema above for reference.

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