First create a library petdb, and then create a table pettable to add data to it.
read out using JDBC.
Import java.sql.*;/** * Created by Chuiyuan on 2/6/16. */public class MySQL {/** * JDBC driver name and database URL */static final String db_name= "Petdb"; Static final String table_name= "pettable"; Static final String jdbc_driver = "Com.mysql.jdbc.Driver"; Static final String Db_url = "jdbc:mysql://localhost/" +db_name; /** * Database Credentials */static final String USER = "Petuser"; Static final String PASSWD = "Petpwd"; public static void Main (String [] args) {Connection conn = null; Statement stmt = null; try {/** * Register JDBC Driver */class.forname (jdbc_driver); /** * Open a connection */System.out.println ("Connect to databse ...."); conn = Drivermanager.getconnection (Db_url, USER, PASSWD); /** * Execute a query */SYSTEM.OUT.PRINTLN ("creating statement"); stmt = Conn.createstatement (); String SQL; sql = "SELECT * from" + table_name; ResultSet rs = stmt.executequery (SQL); /** * Extract data from result set */while (Rs.next ()) {//retrive by LA St Column name String name = rs.getstring ("name"); String species = rs.getstring ("species"); Date birth = rs.getdate ("Birth"); Date death = rs.getdate ("death"); Display System.out.print ("Name:" + name); System.out.print (", Species:" + species); System.out.print (", Birth:" +birth); System.out.println (", Death:" + death); }/** * Clean up Environment */rs.close (); Stmt.close (); Conn.close (); }catch (Exception e) {e.printstacktrace (); }finally {/** * Used to close resources */try {if (stmt!=null) {stmt. Close (); }}catch (SQLException se) {se.printstacktrace (); } try {if (conn!=null) {conn.close (); }}catch (SQLException se1) {se1.printstacktrace (); } } }}
Use of MySQL JDBC