Create the following two. java files:
Package CN. dzj. demo. JDBC; import Java. SQL. *;/*** create Date: 2013-6-24 Author: dzj E-mail: 625065470@qq.com */public class basebetter {public static void main (string [] ARGs) throws classnotfoundexception {// todo auto-generated method stubtry {jdbctemplate ();} catch (exception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public static void jdbctemplate () throws exception {connection conn = NULL; statement STM = NULL; resultset rs = NULL; try {// build connectionconn = jdbcutils. getconnection (); // create statementstm = Conn. createstatement (); // execute sqlrs = cmd.exe cutequery ("select * from persons;"); // while getwhile (RS. next () {system. out. println (RS. getObject (1) + "," + Rs. getObject (2) ;}} finally {jdbcutils. free (Conn, STM, RS );}}}
Package CN. dzj. demo. JDBC; import Java. SQL. *;/*** create Date: 2013-6-24 Author: dzj E-mail: 625065470@qq.com */public final class jdbcutils {public static string url = "JDBC: mysql: // localhost: 3306/PIMS "; public static string username =" root "; public static string Password =" 123456 "; public static string drivername =" com. mySQL. JDBC. driver "; static {try {class. forname (drivername);} catch (classnotfou Ndexception e) {Throw new exceptionininitializererror (e) ;}} public static connection getconnection () throws sqlexception {return drivermanager. getconnection (URL, username, password);} public static void free (connection Conn, statement St, resultset RS) {try {If (RS! = NULL) {Rs. Close () ;}catch (sqlexception e) {e. printstacktrace () ;}finally {try {If (st! = NULL) {st. Close () ;}catch (sqlexception e) {e. printstacktrace () ;}finally {try {If (Conn! = NULL) {conn. Close () ;}} catch (sqlexception e) {e. printstacktrace ();}}}}}