Java uses JDBC driver to connect MySQL database _java

Source: Internet
Author: User

Java drives connection to MySQL database using JDBC:

1. Download driver, import jar Package

2. Load Driver

3. Set up the connection

After the connection is successful, some operations are done on the data in the database

1. Download driver, import jar Package





When you see a corresponding jar package in the JDBC directory, the first step is complete.

2. Load Driver

3. Set up the connection

 import java.sql.Connection; 
 
Import Java.sql.DriverManager; public class Db_helper {public static void main (String[]args) {try {class.forname ("Com.mysql . jdbc. Driver "); 
    Load MySQL JDBC driver System.out.println ("Load driven success!"); 
      catch (Exception e) {System.out.print ("Load driver failed!"); 
    E.printstacktrace ();  
       try {//public static Connection getconnection (String url,string user,string password)/* * Url:a database URL of the form jdbc:subprotocol:subname * user:the database user on whose behalf th E connection is being made * password:the user ' s password/connection connect = DriverManager 
      . getconnection ("Jdbc:mysql://localhost:3306/students", "Root", ""); 
    SYSTEM.OUT.PRINTLN ("Database connection successful!"); 
      catch (Exception e) {System.out.print ("Database connection failed!"); 
    E.printstacktrace (); } 
  } 
} 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.