zoukankan      html  css  js  c++  java
  • JDBC的使用

    1.JDBCd 使用步骤

    1.指定JDBC驱动类名

    String driverName = "com.mysql.jdbc.Driver
    

    2.指定MySQL的JDBCURL

     String connectionString = "jdbc:mysql://主机IP:3306/数据库名?"+"user=用户名&password=密码&useUnicode=true&charcterEncoding=utf-8"
    

    3加载JDBC驱动类

    Class.forName(driverName);
    

    4.得到一个Connection连接

    Connection connection = DriverManager.getConnection(connectionString);
    

    5.得到一个用于执行SQL语句的Statement

    Statement statement = connection.createStatement();
    

    6.指定要执行的SQL语句

    String SQL = "SQL语句";
    

    7.执行SQL语句

    • 1).执行插入、删除、更新语句:
      int count = statment.executeUpdate(SQL);
      返回受影响的行数
    • 2)执行查询语句
      .ResultSet resultSet = statment.executeQuery(SQL);
      返回应该ResultSet类型的对象,就是一个数据表。

    获取方法:

    确定哪一行:next()方法
    确定那一列:getString("列名") 或getString(列序号)根据返回类型选用getInt()、getDate()
    

    当SQL语句的内容事先不知道的情况:

    • PreparedStatement = connection.paepareStatement(SQL语句,不确定位置写?)
    • 填内容:preparedStatement.setString(1,tille);

    8.关闭Statement

    preparedStatement.close()
    

    9.关闭Connection

    connection.close();
    
  • 相关阅读:
    分享jstl实现分页,类似百度分页
    分享git的常用命令
    ubuntu certbot 生成免费泛域名证书
    es创建普通索引以及各种查询
    动态代理
    开闭原则
    单一原则
    单例模式
    设计模式之观察者模式
    SpringBoot集成spring-data-jpa注入Bean失败
  • 原文地址:https://www.cnblogs.com/puxuebing/p/9111611.html
Copyright © 2011-2022 走看看