How Java connects to MySQL database

Source: Internet
Author: User

A little bit of Java database is not to configure the file, directly introduce a jar package on the line, which is different from PHP, but the specific database operation is relative to PHP has some complex, the main steps to do are as follows:
1: Introduction of JAR packages and SQL packages.
2: Load the database driver,
3: Set, connect, query, execute
4: Follow the steps to execute.
5: Retrieving results
The code is as follows:

 PackageCom_jdbc_1;Importjava.sql.*;Importjavax.sql.*; Public  class demon_1 {    /** * @param args * *     Public Static void Main(string[] args)throwsException {//define a connectionConnection ct=NULL;//Define query criteriaPreparedStatement ps=NULL;//define result setResultSet rs=NULL;//Select database driverClass.forName ("Com.mysql.jdbc.Driver");//Set connection informationCt=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test","Root","Toor");//Set queryPs=ct.preparestatement ("SELECT * from TestUser");//execute and get result setRs=ps.executequery ();intI=0;//Traversal output         while(Rs.next ()) {System.out.println (Rs.getint (1)+" "+rs.getstring ("username")); }    }}

How Java connects to a MySQL database

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.