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/