JDBC connection to MySQL database

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.