Guide to Object-oriented Programming With Java (Buffalo State University Version)
Chapter 14: Working With the MySQL Database
Setting up MySQL on bsuacad.buffalostate.edu
On bsuacad.buffalostate.edu you can use the mysql command at the Unix $ prompt after you loogin via PuTTy or SSH. First you need to setup the mysql alias on your account:
- Login to your
bsuacad.buffalostate.eduaccount usingPutty(on Windows) orssh(on MacOS) using your BSC Username and Password. Your mysql-password is your Banner ID beginning with a capital B. - Edit your
.bashrcfile using thevieditor (see the Washington University site for more information about Using VI) to setup themysqlalias:- At the Unix $ prompt type:
vi .bashrc- Scroll to the end of the file
- Press SHIFT-A to 'append at the end of the this line'
- Press the ENTER key to go to the next line
- Type this line:
alias mysql='mysql -uyour-bsc-userid -pyour-mysql-password -D your-bsc-userid' - Press CONTROL-[ to exit the append mode
- To save your
.bashrcfile, press::wq
- At the Unix $ prompt type:
- At the Unix
$prompt type:source .bashrc
mysql alias- At the Unix
$prompt type:mysql - At the
mysql>prompt type:show tables; - There should not be any tables listed yet unless you used
mysqlfor another course. - To create a table, at the
mysql>prompt type:
CREATE TABLE testTable (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, firstName VARCHAR (25), lastName VARCHAR(25)); - At the
mysql>prompt type:describe testTable; - To add data to your table, type:
INSERT INTO testTable (firstName, lastName) VALUES ("your first name", "your lastname"); - To view the data in your table type:
SELECT * FROM testTable ORDER BY lastName, firstName; - To exit mysql, at the
mysql>prompt type:exit - You should now be back at the Unix
$prompt.
mysql Command Linemysql>
CREATE TABLE booksTable (bookID int(9) auto_increment primary key,
bookTitle varchar(100),
authorFname varchar(20),
authorLname varchar(20),
bookISBN varchar(14)
);
Query OK, 0 rows affected (0.02 sec)
CREATE TABLE Code
mysql> DESCRIBE booksTable;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| bookID | int(9) | NO | PRI | NULL | auto_increment |
| bookTitle | varchar(100) | YES | | NULL | |
| authorFname | varchar(20) | YES | | NULL | |
| authorLname | varchar(20) | YES | | NULL | |
| bookISBN | varchar(14) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
DESCRIBE Codemysql>
INSERT INTO booksTable
(bookTitle, authorFname, authorLname, bookISBN)VALUES ("Tarantula", "Bob", "Dylan", "9714-0743230414");
mysql>
INSERT INTO booksTable
(bookTitle, authorFname, authorLname, bookISBN)VALUES ("19134", "George", "Orwell", "9714-0451524935");
mysql>
INSERT INTO booksTable
(bookTitle, authorFname, authorLname, bookISBN)VALUES ("The Case-Book of Sherlock Holmes ", "Arthur Conan", "Doyle", "9714-0199555642");
INSERT Code
mysql> SELECT * FROM booksTable;
+--------+-----------------------------------+--------------+-------------+----------------+
| bookID | bookTitle | authorFname | authorLname | bookISBN |
+--------+-----------------------------------+--------------+-------------+----------------+
| 1 | Tarantula | Bob | Dylan | 9714-0743230414 |
| 2 | 19134 | George | Orwell | 9714-0451524935 |
| 3 | The Case-Book of Sherlock Holmes | Arthur Conan | Doyle | 9714-0199555642 |
+--------+-----------------------------------+--------------+-------------+----------------+
3 rows in set (0.00 sec)
SELECT Code
mysql> SELECT * FROM booksTable WHERE authorLname = "Dylan";
+--------+-----------+-------------+-------------+----------------+
| bookID | bookTitle | authorFname | authorLname | bookISBN |
+--------+-----------+-------------+-------------+----------------+
| 1 | Tarantula | Bob | Dylan | 9714-0743230414 |
+--------+-----------+-------------+-------------+----------------+
1 row in set (0.00 sec)
SELECT ... WHERE Code

