Import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. preparedstatement; import Java. SQL. resultset;/*** JDBC connection to MySQL database ** @ author zhanqi **/public class MySQL {Private Static string driver = "com. mySQL. JDBC. driver "; Private Static string url =" JDBC: mysql: // localhost: 3306/Database Name "; public static void main (string [] ARGs) {connection Ct = NULL; preparedstatement PS = NULL; resu Ltset rs = NULL; try {// load the driver class. forname (driver); // obtain the connection Ct = drivermanager. getconnection (URL, "root", "root"); // obtain the pre-compiled SQL statement object PS = CT. preparestatement ("select count (*) from table name"); // obtain the result set rs = ps.exe cutequery (); // cyclically retrieve the record while (RS. next () {system. out. println (RS. getint (1) ;}} catch (exception e) {e. printstacktrace ();} finally {// close the resource try {If (RS! = NULL) {Rs. Close () ;}if (PS! = NULL) {ps. Close () ;}if (CT! = NULL) {CT. Close () ;}} catch (exception e) {e. printstacktrace ();}}}}
Appendix:
- MySQL Community Server 5.5: Download
- MySQL JDBC driver 5.1: Download