Java Learning Path: 2, Mysql link and query

Source: Internet
Author: User
Tags dsn

1, in fact, I found that with the skill of PHP in the inside, learning Java up or not very laborious, but Java has no process-oriented

 Packagesecond;Importjava.sql.*;//Import Public classMySQL { Public Static voidMain (string[] args)throwssqlexception{String Driver= "Com.mysql.jdbc.Driver";//declaring the class to be importedString DSN = "Jdbc:mysql://127.0.0.1:3306/mysql";//DSNString user = "root";//Account nameString pass = "";//Password        Try{class.forname (driver);//I think it should be equivalent to the new Class () in PHP;Connection conn = drivermanager.getconnection (DSN, user, pass);//instantiate a DB connection            if(!conn.isclosed ()) {//if the link is not closed "can actually be understood as if the link succeeds"System.out.println ("link succeeded!")); } Statement Statement= Conn.createstatement ();//Create a statement objectString sql = "Select user,host from User";//SQL statementsResultSet rs = statement.executequery (SQL);//executes the sq statement and gets a resource result set             while(Rs.next ()) {//loop, the pointer moves downString name = rs.getstring ("user");//nameString local= rs.getstring ("host");//Address            }        }Catch(ClassNotFoundException e) {//ExceptionSystem.out.println (E.getmessage ()); }    }}

PS: It is worth noting that if you want to use MySQL, you have to first import the MySQL library,

Eclipse Import Method: Project Project-Properties->java Build path-add external jar

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.