JDBC学习

 

笔记如上

学了个代码

import java.sql.*;

public class jdbc01 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //注册驱动
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            //获取连接
            conn = DriverManager.getConnection("jdbc:mysql//localhost:3306/student", "root", "123456");
            //执行SQL
            String insertSql = "insert into dept(deptno,dname,loc) values(01,'第一','m',20)";
            stmt.executeUpdate(insertSql);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

  因为配置idea没整明白,所以代码还没运行起来

 

posted @ 2021-11-13 21:04  Acholl  阅读(26)  评论(0)    收藏  举报