Problem Solution Discussion Checking for Errors

Beware of Class.forName The exam ple program Connect .j ava regist ers t he JDBC driver like t his: Class.forName com.mysql.jdbc.Driver.newInstance ; Youre supposed t o be able t o regist er drivers wit hout invoking newInstance , like so: Class.forName com.mysql.jdbc.Driver; However, t hat call doesnt work for som e Java im plem ent at ions, so be sure not t o om it newInstance , or you m ay find yourself enact ing t he Java m ot t o, writ e once, debug everywhere. Som e JDBC drivers MySQL Connect or J am ong t hem allow you t o specify t he usernam e and password as param et ers at t he end of t he URL. I n t his case, you om it t he second and t hird argum ent s of t he getConnection call. Using t hat URL st yle, t he code t hat est ablishes t he connect ion in t he exam ple program could have been writ t en like t his: connect using username and password included in URL Connection conn = null; String url = jdbc:mysql:localhostcookbook?user=cbuserpassword=cbpass; try { Class.forName com.mysql.jdbc.Driver.newInstance ; conn = DriverManager.getConnection url; System.out.println Connected; } The charact er t hat separat es t he user and password param et ers should be , not ; . 2 .2 .7 .1 Addit ion a l con n e ct ion pa r a m e t e r s For non- localhost connect ions, specify an explicit port num ber by adding : port_num t o t he host nam e in t he connect ion URL: String url = jdbc:mysql:mysql.snake.net:3307cookbook; For connect ions t o localhost , t here is no opt ion for specifying t he Unix dom ain socket pat hnam e, at least not for MySQL Connect or J. Ot her MySQL JDBC drivers m ay allow for t his; check t heir docum ent at ion.

2.3 Checking for Errors

2.3.1 Problem

Som et hing went wrong wit h your program and you dont know what .

2.3.2 Solution

Everybody has problem s get t ing program s t o work correct ly. But if you dont ant icipat e difficult ies by checking for errors, you m ake t he j ob a lot harder. Add som e error-checking code so your program s can help you figure out what went wrong.

2.3.3 Discussion

You now know how t o connect t o t he MySQL server. I t s also a good idea t o know how t o check for errors and how t o ret rieve MySQL- relat ed error inform at ion from t he API , so t hat s what well cover next . When errors occur, MySQL provides a num eric error code and a corresponding descript ive t ext error m essage. The recipes in t his sect ion show how t o access t his inform at ion. Youre probably anxious t o see how t o do m ore int erest ing t hings such as issue queries and get back t he result s , but error checking is fundam ent ally im port ant . Program s som et im es fail, especially during developm ent , and if you dont know how t o det erm ine why failures occur, youll be flying blind. The exam ple program s in t his sect ion show how t o check for errors, but will in fact execut e w it hout any problem s if your MySQL account is set up properly. Thus, you m ay have t o m odify t he exam ples slight ly t o force errors t o occur so t hat t he error- handling st at em ent s are t riggered. For exam ple, you can change a connect ion- est ablishm ent call t o supply a bad password. This will give you a feel for how t he code act s when errors do occur. A general debugging aid t hat is not specific t o any API is t o check t he MySQL query log t o see what queries t he server act ually is receiving. This requires t hat you have query logging t urned on and t hat you have access t o t he log on t he MySQL server host . The log oft en will show you t hat a query is m alform ed in a part icular way and give you a clue about why your program is not const ruct ing t he proper query st ring. I f youre running a script under a w eb server and it fails, check t he servers error log.

2.3.4 Perl