Java connection to MySQL database and its simple operations

Source: Internet
Author: User

After nearly one day, I finally connected to the MySQL database. Here is a brief summary:

1. Configure the MySQL database first.

2. Download the MySQL driver mysql-connector-java-5-jar and add it to the current project

Package second; import Java. SQL. *; public class inensshow {public static string driver = "com. mySQL. JDBC. driver "; // MySQL database JDBC driver public static string url =" JDBC: mysql: // localhost: 3306/db1 "; // database urlpublic static connection conn; // The database connection class public static statement stmt; // The statement interface object is used to accommodate SQL statements. It can execute static SQL statements and return the results public static resultset Rs; // processing result set public static string SQL; // SQL statement public static void conn_ SQL () {try {class. forname (driver); // enable the database connection driver} catch (classnotfoundexception e) {e. printstacktrace (); system. out. println ("driver not found:" + E);} Try {conn = drivermanager. getconnection (URL, "root", "123456"); // connect to the database} catch (sqlexception SQL _e) {SQL _e.tostring (); system. out. println ("cann' t connection MySQL. ") ;}} public static void clo_ SQL () {try {Conn. close (); //} catch (sqlexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {conn_ SQL (); // Add // modify // Delete try {stmt = Conn. createstatement (); SQL = "select * from user"; rs1_stmt.exe cutequery (SQL); While (RS. next () {string name = Rs. getstring ("name"); string passw = Rs. getstring ("password"); system. out. println ("usename:" + name + "userpassword:" + passw) ;}} catch (exception e) {e. printstacktrace () ;}clo_ SQL ();}}

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.