Use of MySQL JDBC

Source: Internet
Author: User
Tags getdate stmt

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

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.