jdbc快速入门(java访问数据库)

airfling 2020-01-12

package com.itcast.jdbc;

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

public class JdbcDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2.获取数据库连接对象
        Connection conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root");
        // 3.定义sql语句
        String sql = "update student3 set math = 67 where id = 3";
        // 4.获取执行sql的对象
        Statement stmt = conn.createStatement();
        // 5.执行sql
        int count = stmt.executeUpdate(sql);
        // 6.处理结果
        System.out.println(count);
        //7.释放资源
        stmt.close();
        conn.close();
    }
}

相关推荐