Problem Solution Writing Applications That Adapt to the MySQL Server Version

9.13 Getting Server Metadata

9.13.1 Problem

You want t o t he MySQL server t o t ell you about it self.

9.13.2 Solution

Several SELECT and SHOW st at em ent s ret urn inform at ion about t he server.

9.13.3 Discussion

MySQL offers several SQL st at em ent s t hat provide you wit h inform at ion about t he server it self and about your current client connect ion. A few t hat you m ay find useful are list ed here. To obt ain t he inform at ion provided by any of t hem , issue t he query, t hen process t he result set t o ret rieve t he query out put . Bot h SHOW st at em ent s allow a LIKE pattern clause for lim it ing t he result s only t o t hose rows m at ching t he pat t ern. St a t e m e n t I n for m a t ion pr odu ce d by st a t e m e n t SELECT VERSION Server version st ring SELECT DATABASE Current dat abase nam e em pt y if none SELECT USER Current usernam e SHOW STATUS Server st at us indicat ors SHOW VARIABLES Server configurat ion variables These queries are all MySQL- specific. I f youre working in Java, JDBC provides several dat abase- independent m et hods for obt aining server m et adat a, som e of which provide t he sam e inform at ion as som e of t he preceding st at em ent s. Use your connect ion obj ect t o obt ain t he dat abase m et adat a, t hen invoke t he appropriat e m et hods t o get t he inform at ion in which youre int erest ed. You should consult a JDBC reference for a com plet e list , but here are a few represent at ive exam ples: DatabaseMetaData md = conn.getMetaData ; can also get this with SELECT VERSION System.out.println Product version: + md.getDatabaseProductVersion ; this is similar to SELECT USER but doesnt include the hostname System.out.println Username: + md.getUserName ;

9.14 Writing Applications That Adapt to the MySQL Server Version

9.14.1 Problem

You want t o use a given feat ure t hat is only available as of a part icular version of MySQL.

9.14.2 Solution

Ask t he server for it s version num ber. I f t he server is t oo old, m aybe you can fall back t o a workaround for t he m issing feat ure, if one exist s.

9.14.3 Discussion