Java Connection MySQL Database

Source: Internet
Author: User
Tags mysql download stmt

1, first download the MySQL installation package, download the link mysql download, and then install.

2. Enter the user name and password at the time of installation to connect to the database.

3. Install the MYSQ JDBCL driver in Eclipse, the installation steps are as follows:







To test if the MQL is successfully connected, code:

Package Day_09;import java.sql.*;p ublic class test{public static void Main (string[] args) {String url= "Jdbc:mysql://loca       Lhost:3306/mydb ";       String user= "root";      String password= "281889";            Connection Conn=null;           Statement Stmt=null;     ResultSet Rs=null;        try {class.forname ("com.mysql.jdbc.Driver");          Conn=drivermanager.getconnection (Url,user,password);          Stmt=conn.createstatement ();        String SQL;        Sql= "INSERT into student values (2014218, ' Li Yue ', ' female ')";         Stmt.execute (SQL);          Sql= "SELECT * from student";            rs = stmt.executequery (SQL);           System.out.println ("School Number" + "\t\t\t" + "name" + "\t\t\t" + "gender");            String name = NULL;              while (Rs.next ()) {name = Rs.getstring ("sname");          System.out.println (rs.getstring ("sno") + "\t\t\t" + name+ "\t\t\t" +rs.getstring ("Sex")); }} catch (ClassNotFoundException E1) {System.out.println ("Database driver does not exist!")           "+e1.tostring ());           } catch (SQLException E2) {System.out.println ("the database has an exception" +e2.tostring ()); } finally {try {if (rs!=null) Rs.                   Close ();                   if (stmt!=null) stmt.close ();               if (conn!=null) conn.close ();               } catch (SQLException e) {System.out.println (e.tostring ()); }           }        }      }

Printing results:




Java Connection MySQL Database

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.