java包学习之JDBC

public class DemoJDBC {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        // 通过DriverManager获取数据库连接
         String url = "jdbc:mysql://ip地址:3306/shiro?useUnicode=true&characterEncoding=utf-8";
         String user = "账号";
         String password = "密码";
         Connection connection = (Connection) DriverManager.getConnection(url, user, password);

         PreparedStatement statement = (PreparedStatement) connection.prepareStatement(
                         "insert role ( roleDesc) value ( ?)");
         statement.setString(1, "测试");
         statement.executeUpdate();
         ResultSet resultSet = statement.executeQuery("select * from user");
         // 操作ResultSet结果集
         while (resultSet.next()) {
               // 第一种获取字段方式
               System.out.println(resultSet.getString(1) + " " +
                             resultSet.getString(2) );
           }

         // 关闭数据库连接
         resultSet.close();
         statement.close();
         connection.close();
    }
}

 jdbc是比较原始的,很多mybatis底层都是基于这个做的,步骤简单 第一 获取连接connection 第二  定义 statement 第三执行 statement

原文地址:https://www.cnblogs.com/imfjj/p/10967925.html