mysql and jdbc tutorial ect 7130 hong cheng. supplement on mysql ement/supplement4bmysql.pdf

9
MySQL and JDBC Tutorial ECT 7130 Hong Cheng

Upload: brianne-sharp

Post on 04-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MySQL and JDBC Tutorial ECT 7130 Hong Cheng. Supplement on MySQL  ement/Supplement4bMySQL.pdf

MySQL and JDBC Tutorial

ECT 7130

Hong Cheng

Page 2: MySQL and JDBC Tutorial ECT 7130 Hong Cheng. Supplement on MySQL  ement/Supplement4bMySQL.pdf

Supplement on MySQL

• http://cs.armstrong.edu/liang/intro7e/supplement/Supplement4bMySQL.pdf

Page 3: MySQL and JDBC Tutorial ECT 7130 Hong Cheng. Supplement on MySQL  ement/Supplement4bMySQL.pdf

MySQL Download and Install

• http://dev.mysql.com/downloads/mysql/5.4.html

• When you install the software, you can set the root password.

Page 4: MySQL and JDBC Tutorial ECT 7130 Hong Cheng. Supplement on MySQL  ement/Supplement4bMySQL.pdf

Starting and Stopping MySQL Server

• Starting and stopping the server– See Item 1 in supplement

• Login– mysql –u root –p– Type your password

• Create an ordinary user– See Item 3 in supplement to create a user with name

“scott” and password “tiger”

Page 5: MySQL and JDBC Tutorial ECT 7130 Hong Cheng. Supplement on MySQL  ement/Supplement4bMySQL.pdf

Databases in MySQL

• show databases;

• use test;

• create database javabook;

• use javabook;

Page 6: MySQL and JDBC Tutorial ECT 7130 Hong Cheng. Supplement on MySQL  ement/Supplement4bMySQL.pdf

Tablescreate table Course ( courseId char(5), subjectId char(4) not null, courseNumber integer, title varchar(50) not null, numOfCredits integer, primary key (courseId));

create table Student ( ssn char(9), firstName varchar(25), mi char(1), lastName varchar(25), birthDate date, street varchar(25), phone char(11), zipCode char(5), deptId char(4), primary key (ssn)); create table Enrollment (

ssn char(9), courseId char(5), dateRegistered date, grade char(1), primary key (ssn, courseId), foreign key (ssn) references Student (ssn), foreign key (courseId) references Course (courseId) );

Page 7: MySQL and JDBC Tutorial ECT 7130 Hong Cheng. Supplement on MySQL  ement/Supplement4bMySQL.pdf

Load from a File

• Put the above commands in a file, e.g., test.sql and put in D:\book\test.sql.

• Then in MySQL command line window, type: source D:\book\test.sql

Page 8: MySQL and JDBC Tutorial ECT 7130 Hong Cheng. Supplement on MySQL  ement/Supplement4bMySQL.pdf

Insert Tuples

• insert into Course (courseId, subjectId, courseNumber, title, numOfCredits) values ('11113', 'CSCI', '3720', 'Database Systems', 3);

• insert into Student (ssn, firstName, mi, lastName, birthDate, street, phone, zipCode, deptId) values ('123456789', 'John', 'M', 'Smith', '1990-01-02', 'main', '222-333-444', '61801', 'SEEM');

• insert into Enrollment (ssn, courseId, dateRegistered, grade) values ('123456789', '11113', '2009-9-1', 'A');

• select * from Enrollment;

Page 9: MySQL and JDBC Tutorial ECT 7130 Hong Cheng. Supplement on MySQL  ement/Supplement4bMySQL.pdf

Set MySQL JDBC Driver

• An easy way– Copy slide\book\mysqljdbc.jar to C:\Program Files\

Java\jre1.6.0_03\lib\ext– Try to run the program

• An alternative way– In command line window– set classpath=%classpath%;c:\slide\book\

mysqljdbc.jar;– cd slide\book– java FindGrade