MySQL JDBC Tutorial

1,298 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,298
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MySQL JDBC Tutorial

  1. 1. MySQL and JDBC Tutorial ECT 7130 Hong Cheng
  2. 2. Supplement on MySQL <ul><li>http://cs.armstrong.edu/liang/intro7e/supplement/Supplement4bMySQL.pdf </li></ul>
  3. 3. MySQL Download and Install <ul><li>http://dev.mysql.com/downloads/mysql/5.4.html </li></ul><ul><li>When you install the software, you can set the root password. </li></ul>
  4. 4. Starting and Stopping MySQL Server <ul><li>Starting and stopping the server </li></ul><ul><ul><li>See Item 1 in supplement </li></ul></ul><ul><li>Login </li></ul><ul><ul><li>mysql –u root –p </li></ul></ul><ul><ul><li>Type your password </li></ul></ul><ul><li>Create an ordinary user </li></ul><ul><ul><li>See Item 3 in supplement to create a user with name “scott” and password “tiger” </li></ul></ul>
  5. 5. Databases in MySQL <ul><li>show databases; </li></ul><ul><li>use test; </li></ul><ul><li>create database javabook; </li></ul><ul><li>use javabook; </li></ul>
  6. 6. Tables <ul><li>create table Course ( </li></ul><ul><li>courseId char(5), </li></ul><ul><li>subjectId char(4) not null, </li></ul><ul><li>courseNumber integer, </li></ul><ul><li>title varchar(50) not null, </li></ul><ul><li>numOfCredits integer, </li></ul><ul><li>primary key (courseId) </li></ul><ul><li>); </li></ul>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) );
  7. 7. Load from a File <ul><li>Put the above commands in a file, e.g., test.sql and put in D:ook est.sql. </li></ul><ul><li>Then in MySQL command line window, type: source D:ook est.sql </li></ul>
  8. 8. Insert Tuples <ul><li>insert into Course (courseId, subjectId, courseNumber, title, numOfCredits) values ('11113', 'CSCI', '3720', 'Database Systems', 3); </li></ul><ul><li>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'); </li></ul><ul><li>insert into Enrollment (ssn, courseId, dateRegistered, grade) values ('123456789', '11113', '2009-9-1', 'A'); </li></ul><ul><li>select * from Enrollment; </li></ul>
  9. 9. Set MySQL JDBC Driver <ul><li>An easy way </li></ul><ul><ul><li>Copy slideookmysqljdbc.jar to C:Program FilesJavajre1.6.0_03libext </li></ul></ul><ul><ul><li>Try to run the program </li></ul></ul><ul><li>An alternative way </li></ul><ul><ul><li>In command line window </li></ul></ul><ul><ul><li>set classpath=%classpath%;c:slideookmysqljdbc.jar; </li></ul></ul><ul><ul><li>cd slideook </li></ul></ul><ul><ul><li>java FindGrade </li></ul></ul>

×