Java MYSQL Database Setup

Source: Internet
Author: User
Tags connection pooling

1. First to load the Java MySQL driver, add the downloaded Mysql-connect-bin-java.jar package to the project
2. Then through class.forname ("Com.mysql.jdbc.Driver"); Load MySQL data-driven
3. Through the getconnection URL mode to connect the attention, sometimes if the report
Connection Pooling Connection Database report communications link failure error, change your localhost to 127.0.0.1 try

1 Packagechp07;2 ImportJava.sql.*;3 4 Importcom.mysql.jdbc.PreparedStatement;5 Public classJdbc_test {6 StaticPreparedStatement PS =NULL;7 StaticResultSet rs;8 Public Static voidMain (string[] args) {9 //TODO auto-generated Method StubTenConnection con =NULL;//Create a Connection object to connect to the database One Try { AClass.forName ("Com.mysql.jdbc.Driver");//load MySQL data-driven - -Con =Drivermanager.getconnection ( the"Jdbc:mysql://127.0.0.1:3306/mysql", "root", "123456");//Create a data connection - //System.out.println ("database connection succeeded"); - -String sql = "Select User from ' User ' U";//SQL statement to delete data +PS =(PreparedStatement) con.preparestatement (SQL); -rs =ps.executequery (); + while(Rs.next ()) { ASystem.out.println (rs.getstring ("User")); at - } -Con.close ();//To close a database connection -}Catch(Exception e) { -SYSTEM.OUT.PRINTLN ("Database connection failed" +e.getmessage ()); - } in - } to +}
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.