Java Connection MySQL Database

Source: Internet
Author: User

1, set up the table user and insert data (at this time the database is users, the user name is root, the password is 123456)

Create Table User  varchar( varchar); Insert  into User Values ('tufujie','123456');

2. Writing Java files

//demonstrates how to connect to MySQL using the COM.MYSQL.JDBC connection Bridge Packagecom.tfj.db;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement; Public classJavactmysql { Public Static voidMain (string[] args) {Try {            //1. Load (load) driverClass.forName ("Com.mysql.jdbc.Driver"); //2. Get the link (registration)            /** Connection ct = drivermanager.getconnection (* "jdbc:mysql://localhost:3306/users?             Useunicode=true&characterencoding=utf-8 ", *"root","123456"); */            //or you can do it the following way.Connection ct =DriverManager. getconnection ("jdbc:mysql://localhost:3306/Users? user=Root&password=123456&useunicode=true&characterenconding=utf-8 "); //3. Identical to SQL Server, SQL statements            if(ct! =NULL&&!ct.isclosed ()) System.out.println ("Database Test Connection succeeded"); Statement SM=ct.createstatement (); ResultSet RS= Sm.executequery ("SELECT * FROMUser");  while(Rs.next ()) {//User nameSYSTEM.OUT.PRINTLN ("Administrator name:" + rs.getstring (1) + "Password:" + rs.getstring (2)); }            //close an open resourceRs.close ();            Sm.close ();        Ct.close (); } Catch(Exception e) {e.printstacktrace (); }    }}

Java Connection 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.