www.traintelco.com MySQL and Java Developer’s Guide Mark Matthews Jim Cole Joseph D Gradecki www.traintelco.com Publisher: Robert Ipsen Editor: Robert M Elliott Managing Editor: Vincent Kunkemueller Book Producer: Ryan Publishing Group, Inc Copyeditor: Elizabeth Welch Proofreader: Nancy Sixsmith Compositor: Gina Rexrode 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 particular 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 professional 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 permission 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) SQL (Computer program language) Java (Computer program language) I Title A76.3.S67M38 2003 005.75’65—dc21 2002155887 Printed in the United States of America 10 www.traintelco.com C O N T E N TS Acknowledgments xi About the Authors xiii Introduction Chapter Chapter Chapter xv An Overview of MySQL Why Use an RDBMS? Multiuser Access Storage Transparency Transactions Searching, Modifying, and Analyzing Data Ad Hoc Queries Why Choose MySQL? MySQL and JDBC What’s Next 2 5 JDBC and Connector/J What Is JDBC? What about ODBC? Modeling Database Applications with JDBC JDBC Versions JDBC Driver Types SQL Standards Examining the JDBC Interface The java.sql Package The javax.sql Package Understanding Connector/J JDBC Support within 3.0.1 Obtaining JDBC Drivers What’s Next 10 11 13 13 14 15 15 18 21 22 24 24 Working with MySQL SQL 25 What Is a Database? Database Models Data Types Designing a Database Introducing MySQL SQL Overview of MySQL Creating Databases Creating Tables Inserts Selects SELECT Statement Extensions 25 27 29 29 32 33 34 35 39 40 42 iii www.traintelco.com iv Contents Chapter Chapter Updates Deletes Using SHOW More on Tables Transactions Functions/Operators Joins NULL What’s Next 47 50 51 53 55 56 56 59 59 Installing MySQL, Java, and Connector/J 61 Installing MySQL Linux Installation Windows Installation All Other Installations Installing Java Testing the Java Installation Installing Connector/J Testing the Connector/J Installation What’s Next 61 62 63 63 64 64 65 66 66 Using JDBC with Java Applications and Applets 67 Hello World Loading the Connector/J Driver Using DriverManager to Connect to a Database Executing Queries Through Statement Objects Using the ResultSet Object Determining the Cursor Position Moving the Cursor Getter Methods Primitive Getters Closing the Objects Making It Real Our Main Function The init() Method The buildGUI() Method Executing a Query with No Results Deleting Database Rows Updating Database Rows CREATE TABLE DROP TABLE Disconnecting from the Database Advanced ResultSet Manipulation One Step Forward One Step Back Fast-Forward to the End 67 69 69 75 78 79 79 80 82 85 85 88 89 89 91 97 99 101 101 103 104 113 114 114 www.traintelco.com Contents Rewind to the Beginning Goto Record Freehand Query Batches Limiting Results Database Warnings and Exceptions What’s Next Chapter Achieving Advanced Connector/J Functionality with Servlets Servlets DataSource Connections Execution Environment Databases PreparedStatements Connecting to the Database Determining the Submit Type Displaying Data Updating Data Using Placeholders in a Loop Using Placeholders in PreparedStatement Using setObject/setBytes Getting BLOBs Joins Updatable ResultSets The Update Button Code The Insert Button Code Update Methods Manipulating Date/Time Types Methods for Retrieving a Value as a Date Type Methods for Retrieving a Value as a Time Type Methods for Retrieving a Value as a Timestamp Type Handling BLOB and CLOB Using Streams to Pull Data Handling ENUM Using Connector/J with JavaScript What’s Next Chapter v 114 114 115 115 116 117 118 119 119 122 123 123 124 129 129 130 132 133 134 136 139 141 142 149 150 152 154 155 155 155 156 158 159 161 163 MySQL Type Mapping 165 Character Column Types CHAR VARCHAR TINYTEXT TEXT MEDIUMTEXT LONGTEXT TINYBLOB 166 166 167 167 167 167 168 168 www.traintelco.com vi Contents BLOB MEDIUMBLOB LONGBLOB SET ENUM Using Character Types Date and Time Column Types DATE TIME DATETIME YEAR TIMESTAMP Using Date and Time Types Numeric Column Types TINYINT SMALLINT MEDIUMINT INT BIGINT FLOAT DOUBLE DECIMAL Using Numeric Types What’s Next Chapter Transactions and Table Locking with Connector/J Understanding the Problem MySQL's Transaction Table Types The InnoDB Table Type The BDB Table Type Converting to Transactional from Nontransactional Performing Transactions in MySQL Using the autocommit Variable Update Transactions The SELECT/INSERT Transaction Multiple Table Transactions Foreign Key Integrity on Deletes Ending a Transaction Transaction Isolation Dirty Reads Phantom Reads Nonrepeatable Reads Table Locking What’s Next www.traintelco.com 168 168 169 169 169 169 171 172 172 172 173 173 173 175 176 176 176 177 177 177 177 178 178 180 181 181 182 182 184 184 185 185 187 190 191 192 192 192 193 194 194 195 196 Contents Chapter Using Metadata 197 Using Database Metadata Getting the Object General Source Information Feature Support Data Source Limits SQL Object Available Transaction Support The ResultSet Metadata Getting Column Information Other ResultSet Metadata What’s Next Chapter 10 Connection Pooling with Connector/J What Is a Connection Pool? Pooling with DataSource Pooling with the DriverManager DDConnectionBroker What’s Next Chapter 11 EJBs with MySQL Multi-tier Architecture Using Beans EJB Types The EJB Environment Application Server Configuration The Role of the Servlet Entity Beans Session Beans Using the Beans Adding a Query Bean-Managed Persistence ejbCreate() ejbLoad() ejbStore() ejbRemove() ejbFindByPrimaryKey() Setter/Getter Methods What’s Next Chapter 12 vii Building a General Interface for MySQL Tasks SQL Exceptions MySQL Connections The Task Delegate www.traintelco.com 197 200 202 203 204 204 204 205 205 208 210 211 212 213 218 219 221 223 223 225 225 226 229 230 230 234 236 238 240 241 242 243 243 244 245 245 247 248 252 253 255 viii Contents The Task Manager Task Results The Database Information Task User Input for Tasks The SQL Query Task The Show Columns Task The Insert Row Task What’s Next Chapter 13 Database Administration Using the mysql Administration Application Managing Users and Permissions Changing Root Adding Users Limiting Resources Configuring the Query Cache Forcing a Cache Understanding Log Files Error Logs General Logs Binary Logs Slow Query Logs Maintaining Your Tables Repairing Tables Backing Up and Restoring Your Database Restoring Data InnoDB Table Types DBD Table Types What’s Next Chapter 14 Performance and Tuning Connector/J 3.0 Performance Database Tuning Server Options Using RAID Optimizing Tables The MySQL Query Optimizer Table Indexes JDBC Tuning Minimizing Data Requests Keeping Consistent Connections Handling Statements Batching Using Transactions and Locking Defining the Architecture Getting Data Conclusion www.traintelco.com 255 264 268 270 272 275 280 286 287 287 289 289 290 292 293 294 294 295 295 296 296 296 297 298 301 302 302 303 305 305 308 308 309 309 310 312 313 313 314 315 316 316 317 317 318 Contents Appendix A MySQL Development and Test Environments Test Architecture #1 Test Architecture #2 Servlet Architecture The EJB Architecture Appendix B Databases and Tables The accounts Database and Tables The identification Database and Tables Test Databases Database Products The Database Test Appendix C The JDBC API and Connector/J The java.sql Package Array BatchUpdateException Blob CallableStatement Clob Connection DataTruncation DatabaseMetaData Date Driver DriverManager DriverPropertyInfo ParameterMetaData PreparedStatement Ref ResultSet ResultSetMetaData Savepoint SQLData SQLException SQLInput SQLOutput SQLPermission SQLWarning Statement Struct Time Timestamp Types www.traintelco.com ix 319 319 320 321 323 325 325 326 327 327 327 329 330 331 332 332 333 335 335 337 337 343 343 343 344 344 345 346 347 350 351 351 352 352 353 353 354 354 355 356 356 357 396 MySQL Functions and Operators MD5(string) The MD5 function will calculate and return a checksum value for the supplied string The MD5 returns a checksum for string s Example: mysql> SELECT MD5('This is a string'); + + | MD5('This is a string') | + + | 41fb5b5ae4d57c5ee528adb00e5e8e74 | + + row in set (0.00 sec) PASSWORD(string) The PASSWORD function will convert the supplied string into a proprietarybased encrypted string Example: mysql> SELECT PASSWORD('johnd'); + -+ | PASSWORD('johnd') | + -+ | 0c4e736925ab7792 | + -+ row in set (0.00 sec) USER() SYSTEM_USER() SESSION_USER() All of these functions will return the current user logged in the MySQL database server Example: mysql> SELECT USER(); + + | USER() | + + | ODBC@localhost | + + row in set (0.00 sec) VERSION() The VERSION function will return a string representing the current MySQL server version Example: mysql> SELECT version(); + + | version() | + + | 3.23.52-nt | + + row in set (0.00 sec) www.traintelco.com APPENDIX E Connector/J Late-Breaking Additions pdating the Connector/J driver is difficult enough; simultaneously writing a book and timing it to release with the driver is that much more difficult! After the final copyedit of the pages of this book, a gamma version of Connector/J 3.0 was released This appendix provides information on the most important additions Any future changes not listed in this appendix can be found on the change page at http://www.mysql.com/downloads/api-jdbcdev.html The vast majority are minor bug fixes, but some enhancements were important enough to mention here U Failover Support One of the most important considerations when developing a databaseoriented system is availability MySQL handles availability by supporting replication A master database server writes updates to the database both to a table and a log file Slave servers access the log file and duplicate the updates Updates to the system are allowed on the master server and read on both the master and slave servers There can be any number of slaves to the primary master If you are writing an application that accesses the master database server, you normally have to write code to handle a failure on the master and switch to a slave machine for all reads Updates are postponed until the master server is put back online 397 www.traintelco.com 398 Connector/ J Late-Breaking Additions The Connector/J 3.0.4 Gamma supports automatic fail-over support at the JDBC level By setting various options in the database URL, the driver will support automatic read-only queries across any number of slave hosts The format for the new URL is jdbc:mysql://[hostname][,failoverhost1, failoverhost2 ][:port]/[dbname]… For example: jdbc:mysql://192.168.120, 192.168.1.24/test One requirement for the use of JDBC fail-over is that the autocommit variable must be set to true for the current connection because fail-over support isn’t appropriate across a transaction Two other URL connection variables affect the fail-over The first is queriesBeforeRetryMaster, which has a default value of 50 When a fail-over occurs, this variable will be used to determine how many queries to perform on a fail-over server before trying the master server The second parameter is autoReconnect If this variable is false, the driver will fail-over to a slave database only during a connection attempt on the master server If the master server isn’t available, the driver will fail to a slave If this variable is true, the system will check the connection upon every query and failover to a slave if the master is no longer available Windows Named Pipes If you are executing MySQL and an application on Windows NT/2000/XP, you can use Named Pipes to connect to the database Support for Named Pipes is through a new socket factory plugin added to Connector/J To use a named pipe between the JDBC driver and a local MySQL database, add the following parameter to the URL connection string: SocketFactory=com.mysql.jdbc.NamedPipeSocketFactory A specific pipe path can be used by specifying the namedPipePath and a pipe string, or the default pipe of \\.\pipe\MySQL will be used Note that if the NamedPipeSocketFactory is used in a URL, the hostname, failover hosts, and port number will naturally be ignored An application using named pipes instead of TCP/IP will execute faster due to the reduction in overhead Batch Processing Error Continuation Connector/J 3.0 supports batch processing in which multiple queries are sent to the database in a batch to reduce overhead involved in creating statement and www.traintelco.com SS L 399 connections to the database You can set up a URL parameter called continueBatchOnError, which tells the system whether an error in one query should cause the batch to fail or not The default value of the variable is true To change the value, use a string like this one: continueBatchOnError=true Strict Updates When using updatable ResultSet objects, changes to the object are automatically sent to the database without writing an insert query The parameter strictUpdates can be used to tell the server whether or not an update should be allowed when all of the primary keys for the table have not been included in the result set In other words, if we have a table with two primary keys, prim_id and prim_id_two, a ResultSet object would need to have both of the columns in it in order to allow the rows of the ResultSet object to be updated The default for this parameter is true To change the value, use a string like this one: strictUpdates=true Profile SQL If you are interested in profiling queries to the database including the queries generated from Container Managed Persistent Enterprise Java Beans, use the profilesSql parameter This parameter, which defaults to false, will dump all queries as well as execution times to STDERR To change the value, use a string like this one: profilesSql=true SSL The MySQL database server supports client connections using Secure Socket Layer (SSL) Connector/J 3.0.4 Gamma supports creating a SSL connection as well In order for the driver to build such a connection, several criteria must be met: ■■ MySQL version 4.0.4 or later ■■ JDK 1.4 (which includes Java Secure Sockets Extension) or JDK 1.2/1.3 with the extensions added www.traintelco.com 400 Connector/ J Late-Breaking Additions ■■ A MySQL certificate included with the current 4.0.4 or greater server ■■ A client certificate It should be noted that using SSL will affect the performance of the system due to the added overhead of encrypting and decrypting all communication between the JDBC and the database Since this feature is still in the gamma phase, we refer you to the instructions provided in the Readme file of the latest Connector/J 3.0 download www.traintelco.com Index Symbols + (addition) operator, 369 / (division) operator, 369 = (equality) operator, 372 (equality) operator, 373 > (greater than) operator, 373 >= (greater than or equal to) operator, 373 != (inequality) operator, 372 (inequality) operator, 372 < (less than) operator, 373