当前位置:Gxlcms > 数据库问题 > 使用jdbc操作mysql

使用jdbc操作mysql

时间:2021-07-01 10:21:17 帮助过:20人阅读

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;
    }
}

 

运行:

技术分享图片

 

使用jdbc操作mysql

标签:select   port   rman   user   encoding   private   分享   http   class   

人气教程排行