Connecting to Mysql using JAVA jdbc

Source: Internet
Author: User

First, you need a jar package to connect to Mysql.

String driver = "com. mysql. jdbc. driver "; // The URL points to the name of the database to be accessed String url =" jdbc: mysql: // 192.168.1.111: 3306/test "; // point to mysql String name = "platform" in the LAN server; String pass = "platform"; try {Class. forName (driver); // load the driver Connection conn = DriverManager. getConnection (url, name, pass); // connect to the database Statement statement = conn. createStatement (); // create a statement String SQL = "select * from User"; // query the SQL statement. ResultSet rs = statement.exe cuteQuery (SQL); // obtain the returned result set based on the query statement, which stores the required data while (rs. next () {String userName = rs. getString ("username"); // when the result set is read cyclically, You can traverse and obtain the data System. out. println (userName) ;}} catch (ClassNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();}
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.