Using JDBC to manipulate MySQL

Source: Internet
Author: User
Tags stmt

Importing packages

Import java.sql.*;

Define Link parameters

private static final String url= "Jdbc:mysql://116.52.192.341:3306/newbtc?characterencoding=utf8&usessl=false";

private static final String name= "BTC";

private static final String password= "btc//123";

Load the MySQL driver class

Class.forName ("Com.mysql.cj.jdbc.Driver");

Link MySQL

Connection conn = drivermanager.getconnection (Url,name,password);

Create a statement object

Statement stmt = Conn.createstatement ();

Executeupdate method

int row = stmt.executeupdate ("Update Dandan_user_bank set name=\" Test 2\ "where id=1");

ExecuteQuery method

ResultSet rs = stmt.executequery ("SELECT * from Dandan_user_bank");

Code:

Package Com.spring;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.restcontroller;import java.sql.*; @RestControllerpublic class indexapplication {private static final String url= "jdbc:mysql://116.52.192.341:3306/newbtc?characterencoding=utf8&    Amp;usessl=false ";    private static final String name= "BTC";    private static final String password= "btc//123"; @RequestMapping (value = "/", method = requestmethod.get) Public String Index () throws Exception {Class.fornam        E ("Com.mysql.cj.jdbc.Driver");        Connection conn = drivermanager.getconnection (URL, NAME, PASSWORD);        Statement stmt = Conn.createstatement ();        int row = stmt.executeupdate ("Update Dandan_user_bank set name=\" Test 3\ "where id=1");        ResultSet rs = stmt.executequery ("SELECT * from Dandan_user_bank");        String values = ""; while (Rs.next ()) {values += Rs.getint ("userid") + "--" +rs.getstring ("name") + "<br/>";        } rs.close ();        Stmt.close ();        Conn.close ();    return values; }}

Run:

Using JDBC to manipulate MySQL

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.