[MySQL usage tips] JDBC connection

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Add the jar of MySQL-connector to build path. Package Org. BUPT. JDBC;/*** @ author gnuhpc * Email: warmbupt@gmail.com * blog: http://blog.csdn.net/gnuhpc * @ date 2010-1-6 */import Java. SQL. sqlexception; public class jdbchelloworld {/*** @ Param ARGs * @ throws sqlexception * @ throws classnotfoundexception */public static void main (string [] ARGs) throws sqlexception, classnotfoundexception {// 1. register the driver class. forname ("com. mySQL. JDBC. driver ");// MySQL driver // 2. Obtain the database connection java. SQL. Connection conn = java. SQL. drivermanager. getconnection ("JDBC: mysql: // localhost/publications? Useunicode = true & characterencoding = GBK "," root "," passw0rd "); // 3. get expression Java. SQL. statement stmt = Conn. createstatement (); // 4. execute SQL Java. SQL. resultset rs = stmt.exe cutequery ("select * From books"); // 5. show the data in the result set while (RS. next () {system. out. println (RS. getint (1); system. out. println (RS. getstring ("pages"); system. out. println (RS. getstring ("title");} // execute the SQL statement for inserting data // stmt.exe cuteupdate ("insert into user values (2, '123456', 'linux kernel ') "); // 6. release resource RS. close (); stmt. close (); Conn. close ();}}

 

Author: gnuhpc

Source: http://www.cnblogs.com/gnuhpc/

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.