Download the JDBC driver from MySQL's HomePage, find the JAR file containing org.gjt.mm.mysql.driver.class after unpacking, and contain it to the project file. Package com.catmiw.testmysql;
Import java.sql. *;
Public class mysqljdbc {public static void main (string args []) {try {class.forname ("org.gjt.mm.mysql.driver); // loads mysql JDBC driver system.out.println (" Success Loading Mysql Driver! ");} Catch (exception e) {system.out.print (" Error Loading MySQL Driver! "); E.PrintStackTrace ();
Try {Connection Connect = DriverManager.getConnection ("JDBC: MySQL: // LocalHost / Catmiw", "root", ""); // Connect URL to JDBC: MySQL // Server Address / Database Name // 2 The parameters are the login username and password system.out.println ("Success Connect MySQL Server!");
Statement Stmt = Connect.createStatement (); ResultSet RS = Stmt.executeQuery ("SELECT * FROT"); while (rs.next ()) {system.out.println (Rs.getstring ("name");
}}}} Catch (Exception E) {system.out.print ("get data error!"); E.PrintStackTrace ();}}} The results of the program are as follows: Success Loading MySQL Driver! Success Connect MySQL Server! A DOG