MySQL and Java Developer s Guide 1st Edition Mark Matthews download
MySQL and Java Developer s Guide 1st Edition Mark Matthews download
https://ebookname.com/product/mysql-and-java-developer-s-
guide-1st-edition-mark-matthews/
https://ebookname.com/product/java-2-web-developer-certification-
study-guide-2nd-edition-natalie-levi/
https://ebookname.com/product/splunk-developer-s-guide-second-
edition-smith/
https://ebookname.com/product/programming-java-2-micro-edition-
for-symbian-os-a-developer-s-guide-to-midp-2-0-martin-de-jode/
https://ebookname.com/product/immune-dysfunction-and-
immunotherapy-in-heart-disease-1st-edition-ronald-ross-watson/
Marx s Revenge The Resurgence of Capitalism and the
Death of Statist Socialism 1st Edition Meghnad Desai
https://ebookname.com/product/marx-s-revenge-the-resurgence-of-
capitalism-and-the-death-of-statist-socialism-1st-edition-
meghnad-desai/
https://ebookname.com/product/clean-coal-1st-edition-klaes-g-
douwe/
https://ebookname.com/product/the-healing-code-6-minutes-to-heal-
the-source-of-your-health-success-or-relationship-issue-1st-
edition-loyd/
https://ebookname.com/product/a-little-book-of-language-1st-
edition-david-crystal/
https://ebookname.com/product/urogenital-imaging-direct-
diagnosis-in-radiology-dx-direct-direct-diagnosis-in-
radiology-1st-edition-hamm/
First Mothers Bonnie Angelo
https://ebookname.com/product/first-mothers-bonnie-angelo/
MySQL and Java
Developer’s Guide
Mark Matthews
Jim Cole
Joseph D. Gradecki
Publisher: Robert Ipsen Copyeditor: Elizabeth Welch
Editor: Robert M. Elliott Proofreader: Nancy Sixsmith
Managing Editor: Vincent Kunkemueller Compositor: Gina Rexrode
Book Producer: Ryan Publishing Group, Inc.
Designations used by companies to distinguish their products are often claimed as trademarks. In all instances
where Wiley Publishing, Inc., is aware of a claim, the product names appear in initial capital or ALL CAPITAL
LETTERS. Readers, however, should contact the appropriate companies for more complete information regarding
trademarks and registration.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under
Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the
Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center,
Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4470. Requests to the Publisher for
permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd.,
Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, E-mail: [email protected].
Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing
this book, they make no representations or warranties with respect to the accuracy or completeness of the
contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particu-
lar purpose. No warranty may be created or extended by sales representatives or written sales materials. The
advice and strategies contained herein may not be suitable for your situation. You should consult with a profes-
sional where appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other
commercial damages, including but not limited to special, incidental, consequential, or other damages.
For general information on our other products and services please contact our Customer Care Department
within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley Publishing logo and related trade dress are trademarks or registered trademarks
of Wiley Publishing, Inc., in the United States and other countries, and may not be used without written permis-
sion. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated
with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be
available in electronic books.
2002155887
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
C O N T E N TS
Acknowledgments xi
Introduction xv
iii
iv Contents
Updates 47
Deletes 50
Using SHOW 51
More on Tables 53
Transactions 55
Functions/Operators 56
Joins 56
NULL 59
What’s Next 59
BLOB 168
MEDIUMBLOB 168
LONGBLOB 169
SET 169
ENUM 169
Using Character Types 169
Date and Time Column Types 171
DATE 172
TIME 172
DATETIME 172
YEAR 173
TIMESTAMP 173
Using Date and Time Types 173
Numeric Column Types 175
TINYINT 176
SMALLINT 176
MEDIUMINT 176
INT 177
BIGINT 177
FLOAT 177
DOUBLE 177
DECIMAL 178
Using Numeric Types 178
What’s Next 180
Index 401
A C K N O W L E D G M E N TS
Dedication
To my wife Diane, for all her support in my "geeky" endeavors, and to our
new daughter Lauren.
I would also like to dedicate this work to Monty, David, and the rest of the
fine group of developers at MySQL AB. Without their contribution to the
software community and dedication to free software and open source ideals,
this book would not have been possible.
--Mark Matthews
I would like to dedicate this book to my parents. Their ever-present love and
encouragement have made so many things possible.
—Jim Cole
This book is dedicated to the trinity: God, Jesus Christ, and the Holy Spirit.
—Joseph D. Gradecki
Acknowledgments
I need to acknowledge the patience and support of my beautiful and loving wife
and our boys. Thank you for the opportunity to be your husband and father.
Tim, thank you for the opportunities. Jim, welcome to this new adventure and I
look forward to many more in the future. Thank you to Liz Welch for the excel-
lent review.
xi
A B O U T T H E CAOUNTTHEO
NRTS
xiii
Introduction
H
ave you ever been assigned a project and realized that you had no idea
how you were going to accomplish it? Many developers have experi-
enced this feeling when asked to interface their code with a database.
With a few exceptions, most developers were busy learning Lisp, linked lists,
and big-O notation during their formal education instead of learning the funda-
mentals of relationship database management systems. When the time comes
to interface their code with a database, they turn to a book like the one you are
holding.
Your challenge might be to write a Web-based system using servlets and Enter-
prise JavaBeans (EJBs) to transfer shipping records from the home office in
Bend, Oregon, to a satellite shipper in New Jersey. Or perhaps your father just
opened his new medical office and you volunteered to create a scheduling sys-
tem over the weekend.
Whatever the situation, interfacing an application to a database is one of the
most fundamental tasks a developer is required to perform. This book is
designed for developers who either have a pressing task ahead of them or who
are curious about how to read database information into their application.
By combining MySQL, the number-one open source database available, with
Java, the most portable language ever developed, you can create an undis-
putable champion. So, sit back in your desk chair with a hot chocolate and get
ready to supercharge your coding.
xvi I NTRO D U CTI O N
NOTE
All the code and examples in this book can be found on the the support Web site at
www.wiley.com/compbooks/matthews.
Two different versions of MySQL are used throughout this book: 4.0.4 and
3.23.52. JDBC connectivity is handled using MySQL’s Connector/J driver, and
we cover both versions 2.0.14 and development 3.0.1.
Book Organization
The first four chapters of this book provide an overview of databases, JDBC,
and installation of the tools you will be using. The remainder of the book is an
in-depth guide to building database applications with MySQL, Connector/J,
JDBC, and Java.
All of the coding examples in this book are built using MySQL as the primary
database, Java as our coding language, and Connector/J, MySQL’s JDBC dri-
ver. Although the installation of these components isn’t overly difficult, this
chapter provides comprehensive instructions for obtaining all of the neces-
sary components and performing a step-by-step installation. We also provide
simple examples for testing the installation.
queries instead of one. This chapter looks at inserting multiple pieces of infor-
mation into multiple tables, what problems can arise, and how transactions can
be used to solve these problems.
all together using a Certificate Authority application. Using JSP, servlets, and
EJB, the application shows how to create new accounts, request certificates,
and enable the verification of certificates. All of the information, including
the binary certificate, is stored in a MySQL database with multiple tables.
I
n this chapter, we explain why you might choose to use a database system
with your software. We also provide an overview of the MySQL database
server and the Connector/J JDBC driver.
For many years, large corporations have enjoyed the ability to deploy relational
database management systems (RDBMSs) across their enterprise. Companies
have used these systems to collect vast amounts of data that serve as the “fuel”
for numerous applications that create useful business information.
Until recently, RDBMS technology has been out of reach for small businesses
and individuals. Widely used RDBMS systems such as Oracle and DB2 require
complex, expensive hardware. License fees for these systems are in the tens to
hundreds of thousands of dollars for each installation. Businesses must also
hire and retain staff with specialized skill sets to maintain and develop these
systems. Smaller enterprises have relied on systems like Microsoft Access and
FoxPro to maintain their corporate data.
Early on, during the explosive growth of the Internet, open source database
systems like mSQL, Postgres (now PostgreSQL), and MySQL became available
for use. Over a relatively short amount of time, the developers of these systems
have provided a large subset of the functionality provided by the expensive
commercial database systems. These open source database systems also run
on less-expensive commodity hardware, and have proven in many cases to be
easier to develop for and maintain than their commercial counterparts.
1
2 An Overview of MySQL
Finally, smaller businesses and individuals have access to the same powerful
level of software tools that large corporations have had access to for over a
decade.
Multiuser Access
Many programs use flat files to store data. Flat files are simple to create and
change. The files can be used by many tools, especially if they are in comma- or
tab-delimited formats. A large selection of built-in and third-party libraries is
available for dealing with flat files in Java. The java.util.Properties class
included with the Java Development Kit is one example.
Flat file systems can quickly become untenable when multiple users require
simultaneous access to the data. To prevent corrupting the data in your file, you
must lock the file during changes, and perhaps even during reads. While a file is
locked, it cannot be accessed by other users. When the file becomes larger and
the number of users increases, this leads to a large bottleneck because the file
remains locked most of the time—your users are forced to wait until they can
have exclusive access to the data.
RDBMSs avoid this situation by employing a number of locking strategies at
varying granularities. Rather than using a single lock, the database system can
lock an individual table, an individual page (a unit of storage in the database,
usually covering more than one row), or an individual row. This increases
throughput when multiple users are attempting to access your data, which is a
common requirement in Web-based or enterprise-wide applications.
Storage Transparency
If you use flat files in your software, you are also responsible for managing their
storage on disk. You have to figure out where and how to store the data, and
Random documents with unrelated
content Scribd suggests to you:
“Hush, Jack! Keep your temper,” whispered Alvar, in English.
“Señor, I am the grandson of Señor Don Guzman de la
Rosa, of Seville, well known as a friend to the government,
and this is my half-brother from England.”
No, informing their friends was the last thing wished for. In
the morning they would see.
After walking for some way along a different road from the
one they had come by, they stopped at a little wayside
tavern, where they were given to understand that they
were to pass the night.
“No one who could help us,” answered Alvar. “I do not think
there is anything to be afraid of for ourselves; we can easily
prove that we are English when we get to some town; it is
of Cherry that I think—he will be so frightened.”
“Oh, no; I hope they will send to Ronda for our passports in
the morning. But, Jack, do not fly in a passion. We must be
very civil, and say we are quite willing to be detained in the
service of the government.”
“That,” said Alvar dryly, “is what many have tried to do, but
they have not succeeded.”
They hoped that the early morning might produce Don Luis,
but they saw nothing of any one but the soldier who
brought them their food, which was still of the poorest.
Alvar’s patience began to give way at last; he walked up
and down the room.
“Don’t you think they’ll let us out soon?” said Jack, who had
subsided into a sort of glum despair.
The thing was ended with ludicrous ease. Don Luis bowed to
Alvar, and turning to the officer declared that Don Alvar
Lester was perfectly well known to him, and that the other
gentleman was certainly his half-brother and an
Englishman. The officer bowed also, smiled, hoped that they
had not been incommoded; it was a slight mistake.
Alvar turned a deaf ear, and bowed and smiled with equal
politeness.
“He had been sure that in due time the slight mistake would
be rectified. Were they now free to go?”
“Yes;” and Don Luis interposed, begging them to come and
get some breakfast with him while their horses could be got
ready. Their guide?—oh, he was still detained on suspicion.
“You must lie down, my dear boy; you have had no rest. I
will do it.”
“My father, too,” Cheriton said, with a painful effort at self-
control. “I think—there’s no chance. I must try to do it; but
—oh—Jack—Jack!”
But Cherry could not speak a word; he only put out his
hand and caught Jack’s, as if to feel sure of his presence
also.
“Mi querido,” said Alvar in his gentle, natural tones, “all the
terror is over—now you can rest. I think you had better go,
Jack. I will take care of him,” he added.
Chaucer.
“It was very dreadful here,” said Gipsy, in a low voice, with
fresh tears springing.
“Yes—but I don’t know. What will they all say? Isn’t it wrong
when we are so young?”
“I’ll wait!” said Gipsy softly; and then voices sounded near,
and she started away from him, while Jack—but Jack could
never recollect exactly what he did during the next ten
minutes, till the thought of how he was to tell his story
sobered him. Practical life had not hitherto occupied much
of Jack’s mind; he had had no distinct intentions beyond
taking honours, and if possible a fellowship, till he had been
seized upon by this sudden passion, which in most lads
would probably have been a passing fancy, but in so earnest
and serious a nature took at once a real and practical
shape. But when Jack thought of facing Mr Stanforth, and
still worse his own father, with his wishes and his hopes, a
fearful embarrassment seized on him. No, he must first
make his cause good with the only person who was likely to
be listened to—he must find Cherry. However, the first
person he met was Mr Stanforth, who innocently asked him
if he knew where his daughter was. Jack blushed and
stared, answering incoherently,—
“Did I ever not know my own mind? I’m not a fool!” said
Jack angrily. “And, if you could have seen just the way she
looked, Cherry, you wouldn’t have any doubts.”
“As to that,” said Mr Stanforth, “I like Jack very well, and for
my part I think young people are all the better for having to
fight their way; but whatever may take place in the future I
can allow no intercourse till your father’s consent is
obtained. That will give a chance of testing their feelings on
both sides. Gipsy is a mere child, she may not understand
herself.”
The next morning all were up with the sun, Gipsy busily
dispensing the chocolate and pressing it on Cheriton as he
sat at the table. Suddenly she turned, and, with a very
pretty gesture, half confident, half shy, she held up a cup to
Jack, who stood behind.
“Won’t you have some?” she said, with a hint of her own
mischief in her eyes and voice. Jack seized the cup, and—
upset it over the deft, quick hands that tendered it to him.
“Oh, I have burnt you!” he exclaimed, in so tragic a voice
that all present burst out laughing.
Gipsy had never heard her father speak in this tone before
—she was awed and silenced.
“I cannot help it now, papa,” said Gipsy, who felt that those
days and nights of terror had developed her feelings more
than weeks of common life. She gave her father’s hand a
little squeeze, and looked up in his face with the tears on
her black eyelashes. She meant to say, “I love you all the
better because of this new love which has made everything
deeper and warmer for me,” but all she managed to say was
—“There! There are all the things tumbling out of your
knapsack! I’m not going to have that happen again even if—
if—whatever should take place in the future.”
“Ah, they do not suit you, that is not what you like,” Alvar
said cheerfully; but he never said, “It is not good, this sort
of life does not make a nation great or virtuous.”
“Mi querido, I have been looking for you. Will you come
home? I want you,” he said.
“Ah, I must tell you!” said Alvar reluctantly. “It is bad news,
indeed. Sit down again—here—I have received this.” He
took a telegraph paper out of his pocket and put it into
Cherry’s hand.
“I must go,” said Cheriton. “Don’t waste any words about it.
I know he wants me. I’ll be careful enough, only make
haste.”
It was still early in the day, and they were able to start
within a few hours, only taking a few of their things with
them, amid a confusion of tears, sympathy, and regret; Don
Guzman evidently parting from Alvar with reluctance, and
bestowing a tremendous embrace on Cheriton in return for
his thanks for the kindness that had been shown to him.
Manoel, on the other hand, was evidently relieved at their
early departure.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com