JDBC连接数据库

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Mian {
    public static void main(String[] args) throws Exception{
        //注册驱动
        //Class.forName("com.mysql.jdbc.Driver");

        // 获取连接
        //如果默认的是本机的ip端口是3306的话可以简写获取连接的方式: 
        String url = "jdbc:mysql:///xuexi";
        //String url = "jdbc:mysql://127.0.0.1:3306/xuexi";
        String user = "root";
        String paswd = "123321";
        Connection connection = DriverManager.getConnection(url, user, paswd);

        //定义sql语句
        String sql = "UPDATE ck set ck = ck + 50000000 WHERE id = 2";

        //获取执行sql的对象
        Statement statement = connection.createStatement();
        //使用获取的对象执行mysql并返回执行结果
        int i = statement.executeUpdate(sql);
        
         System.out.println(i);
         //释放
        connection.close();
        statement.close();

    }
}

您可能还喜欢...