java操作jdbc然后操作数据库的基本步骤

1、首先链接mysql数据需要对应的jar包例如:mysql-connector-java-5.1.37.jar;官网上下载下来

2、新建一个目录libs如图所示点击Add As Library按钮
Snipaste_2019-10-24_17-09-09.png

3、代码区域

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

public class eee {
    public static void main(String[] args) {
//        提取公共变量
        Connection conn = null;
        Statement stmt = null;
//        1、注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //        2、创建数据库的链接对象
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root");
//        3、写sql语句
            String sql = "update user set u_name = '小小米' where u_id = 6 ";
//        4、穿件操作sql对象
            stmt = conn.createStatement();
//        5、执行sql
            int count = stmt.executeUpdate(sql);
//        6、操作结果
            System.out.println(count);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

你可能感兴趣的:(java操作jdbc然后操作数据库的基本步骤)