导入包
import java.sql.*;
定义链接参数
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";
加载MySql的驱动类
class.forName("com.mysql.cj.jdbc.Driver");
链接mysql
Connection conn = DriverManager.getConnection(URL,NAME,PASSWORD);
创建Statement对象
Statement stmt = conn.createStatement();
executeUpdate方法
int row = stmt.executeUpdate("update dandan_user_bank set name=\\"测试2\\" where id=1");
executeQuery方法
ResultSet rs = stmt.executeQuery("select * from dandan_user_bank");
代码:
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.*;
@RestController
public class IndexApplication {
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";
@RequestMapping(value = "/",method = RequestMethod.GET)
public String index() throws Exception
{
Class.forName("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=\\"测试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;
}
}
运行: