MySQL database connection

Source: Internet
Author: User

The project uses hibernate to connect to the database. More flexible database connections may be required when performing some special tasks. Record it for future use

Public static void main (string [] ARGs) {// driver name string driver = "com. mySQL. JDBC. driver "; // URL pointing to the name of the database to be accessed scutcsstring url =" JDBC: mysql: // ip: Port/database? Useunicode = true & characterencoding = utf8 "; // string user =" XXX "in MySQL configuration; // string Password =" XXX "in connection with MySQL configuration in Java "; try {// load the driver class. forname (driver); // continuous database connection conn = drivermanager. getconnection (URL, user, password); If (! Conn. isclosed () system. Out. println ("succeeded connecting to the database! "); // Statement is used to execute the SQL statement = Conn. createstatement (); // The SQL statement to be executed string SQL = "select * from XXX Where"; resultset rs = statement.exe cutequery (SQL); hashmap <integer, string> Hm = new hashmap <integer, string> (); int Index = 0; while (RS. next () {string filename = Rs. getstring ("FILENAME"); // system. out. println (filename); HM. put (index, filename); index ++;} Rs. close (); Conn. close (); system. out. PRI Ntln ("-----------------"); system. out. println ("database has" + HM. size () + "data");} catch (classnotfoundexception e) {system. out. println ("Sorry, can't find the driver! "); E. printstacktrace ();} catch (sqlexception e) {e. printstacktrace ();} catch (exception e) {e. printstacktrace ();}}

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.