1. Basic parameters
// JDBC driver name and database URL Static Final String jdbc_driver = "Com.mysql.jdbc.Driver"; // localhost: local Static Final String Db_url = "Jdbc:mysql://localhost:3306/mysql"; // The user name and password of the database need to be set according to their own Static Final String USER = "root"; Static Final String PASS = "123456";
2. Inject drive connection database, Zzz_form table for name retrieval, close database
Try{ //registering the JDBC driverClass.forName ("Com.mysql.jdbc.Driver"); //Open LinkSYSTEM.OUT.PRINTLN ("Connect database ..."); Conn=drivermanager.getconnection (Db_url,user,pass); //Execute QuerySYSTEM.OUT.PRINTLN ("instantiating statement to ..."); stmt=conn.createstatement (); String SQL; SQL= "SELECT name from Zzz_form"; ResultSet RS=stmt.executequery (SQL); //Expand the result set database while(Rs.next ()) {//Retrieving by FieldsString name = rs.getstring ("name"); Mynamelist.add (name); SYSTEM.OUT.PRINTLN (name); } //Close after completionRs.close (); Stmt.close (); Conn.close (); }Catch(SQLException se) {//Handling JDBC ErrorsSystem.out.print ("Handling JDBC errors"); Se.printstacktrace (); }Catch(Exception e) {//Handling Class.forName ErrorsSystem.out.print ("Handling Class.forName Error"); E.printstacktrace (); }finally{ //Close Resource Try{ if(stmt!=NULL) Stmt.close (); }Catch(SQLException se2) {}//don't do anything. Try{ if(conn!=NULL) Conn.close (); }Catch(SQLException se) {se.printstacktrace (); } }
Description
1. Import library Mysql-connector-java-5.1.42-bin.jar
Java link MySQL