John.Wiley.and.Sons.MySQL.and.Java.Developers.Guide.eBook-KB.pdf

(4245 KB) Pobierz
MySQL and Java Developer's Guide
22892155.003.png
MySQL and Java
Developer’s Guide
Mark Matthews
Jim Cole
Joseph D. Gradecki
22892155.004.png
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.
This book is printed on acid-free paper.
Copyright © 2003 by Wiley Publishing, Inc. All rights reserved.
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada.
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: permcoordinator@wiley.com.
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.
Library of Congress Cataloging-in-Publication Data:
Matthews, Mark.
MySQL and Java developer’s guide / Mark Matthews.
p. cm.
ISBN 0-471-26923-9 (PAPER/WEBSITE)
1. SQL (Computer program language) 2. Java (Computer program
language) I. Title.
A76.3.S67M38 2003
005.75’65—dc21
2002155887
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
22892155.005.png
CONTENTS
Acknowledgments
xi
About the Authors
xiii
Introduction
xv
Chapter 1 An Overview of MySQL
1
Why Use an RDBMS?
2
Multiuser Access
2
Storage Transparency
2
Transactions
3
Searching, Modifying, and Analyzing Data
4
Ad Hoc Queries
5
Why Choose MySQL?
5
MySQL and JDBC
7
What’s Next
8
Chapter 2 JDBC and Connector/J
9
What Is JDBC?
9
What about ODBC?
10
Modeling Database Applications with JDBC
11
JDBC Versions
13
JDBC Driver Types
13
SQL Standards
14
Examining the JDBC Interface
15
The java.sql Package
15
The javax.sql Package
18
Understanding Connector/J
21
JDBC Support within 3.0.1
22
Obtaining JDBC Drivers
24
What’s Next
24
Chapter 3 Working with MySQL SQL
25
What Is a Database?
25
Database Models
27
Data Types
29
Designing a Database
29
Introducing MySQL SQL
32
Overview of MySQL
33
Creating Databases
34
Creating Tables
35
Inserts
39
Selects
40
SELECT Statement Extensions
42
iii
22892155.006.png
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
Chapter 4 Installing MySQL, Java, and Connector/J
61
Installing MySQL
61
Linux Installation
62
Windows Installation
63
All Other Installations
63
Installing Java
64
Testing the Java Installation
64
Installing Connector/J
65
Testing the Connector/J Installation
66
What’s Next
66
Chapter 5 Using JDBC with Java Applications and Applets
67
Hello World
67
Loading the Connector/J Driver
69
Using DriverManager to Connect to a Database
69
Executing Queries Through Statement Objects
75
Using the ResultSet Object
78
Determining the Cursor Position
79
Moving the Cursor
79
Getter Methods
80
Primitive Getters
82
Closing the Objects
85
Making It Real
85
Our Main Function
88
The init() Method
89
The buildGUI() Method
89
Executing a Query with No Results
91
Deleting Database Rows
97
Updating Database Rows
99
CREATE TABLE
101
DROP TABLE
101
Disconnecting from the Database
103
Advanced ResultSet Manipulation
104
One Step Forward
113
One Step Back
114
Fast-Forward to the End
114
22892155.001.png 22892155.002.png
Zgłoś jeśli naruszono regulamin