MySQL's first program

Source: Internet
Author: User

Each time write Java link data How to write, this time to do a summary, convenient reference.

1. Download MySQL driver jar package on MySQL

  

2. Download the driver package

  

3. Program Directory

  

4. Procedures

1  Packagemysqltest;2 Importjava.sql.Connection;3 ImportJava.sql.DriverManager;4 Importjava.sql.PreparedStatement;5 ImportJava.sql.ResultSet;6  Public classTest1 {7      Public Static voidMain (string[] args) {8Connection connection=NULL;9PreparedStatement preparedstatement=NULL;TenResultSet resultset=NULL; One         Try { AString driverclass= "Com.mysql.jdbc.Driver"; -String url= "Jdbc:mysql://localhost:3306/test"; -String user= "Root"; theString password= "123456"; - Class.forName (driverclass); -connection=drivermanager.getconnection (URL, user, password); -System.out.println ("connect=" +connection); +String sql= "SELECT * from TB1"; -Preparedstatement=connection.preparestatement (SQL); +resultset=preparedstatement.executequery (); A              while(Resultset.next ()) { at                 intId=resultset.getint (1); -String name=resultset.getstring (2); -System.out.println (id+ "....." +name); -             } -}Catch(Exception e) { -             //Todo:handle Exception in         } -  to     } +  -}

5. Interpreting Knowledge points

Jdbc:mysql://localhost:3306/test
3306 is the port number
Test is the database name
There is no table name, and the table name is placed in the SQL statement.
LocalHost is the IP address, as follows

  

6. Effects

  

  

MySQL's first program

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.