Java How to connect MYSQ source code L explained

Source: Internet
Author: User
Tags stmt

import java.sql.*;  import java.text.simpledateformat;  import java.util.*;   import java.util.Date;  import java.text.*;  import  java.util.calendar;   public class conn {    public  Static void main (String[] args)  {        conn   lm = new conn ();         string t=  lm.getstringdate ();           try {                 class.forname (" Com.mysql.jdbc.Driver ")      //load MYSQL JDBC drivers                  }           catch  (exception e)  {                   e.printstacktrace ();               }          try {                 Connection connect =  Drivermanager.getconnection ("Jdbc:mysql://localhost:3307/student", "root", "Zhangyabiao");                 //connection URL is    jdbc: mysql//server address/database name  &NBSP, and the following 2 parameters are login username and password, respectively                  system.out.println ("[" +t+ "]" + "successfully connect to Database!   ");                 statement stmt =  connect.createstatement ();                  resultset rs  = stmt.executequery ("Select * from zh");                 //user  the name of your table                  while  (Rs.next ())  {                      System.out.println (Rs.getstring ("n") + ". " +rs.getstring ("N1") + "  " +rs.getstring ("N2");                        }                 system.out.println ("[" +t+ "]" + "successfully obtained data");               }          catch  (exception e)  {                 system.out.print ("[" +t+ "]" + "failed to obtain data");                  E.printstacktrace ();               }             }       Public static string getstringdate ()  {              date currenttime = new date ();              SimpleDateFormat formatter = new  SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");              string datestring =  Formatter.format (currenttime);              return datestring;      }    }
Results test
/* [2017-05-08 22:05:48] successfully connected to the database! ...... [2017-05-08 22:05:48] Successful data acquisition results in the above, indicating that you are connected to the database successfully. */


Java How to connect MYSQ source code L explained

Related Article

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.