zoukankan      html  css  js  c++  java
  • JavaEE学习之jdbc基础认识

    JDBC的定义:

      Java Data Base Connectivity,java数据库连接;是一种用于执行SQL语句的Java API;

    JDBC的操作步骤:

      1.注册驱动

    Class.forName();

      2.获取连接

    Connection xxx=DriverManager.getConnection();

      3.编写sql

    String sql=“ ”;

      4.创建预编译的语句执行者

    prepareStatement yyy=xxx.prepareStatement(sql,username,password);

      5.设置参数

    yyy.getString ()或者yyy.getInt();

      6.执行查询

    查询操作

    yyy.executeQuery();

    增删改操作

    yyy.executeUpdate();

      7.释放资源

    ???.close();

     案例:

      1 package cn.it.jdbc;
      2 
      3 import java.sql.Connection;
      4 import java.sql.DriverManager;
      5 import java.sql.PreparedStatement;
      6 import java.sql.ResultSet;
      7 import java.sql.SQLException;
      8 import java.sql.Statement;
      9 
     10 import org.junit.Test;
     11 
     12 import com.jdbc.utlis.jdbcUtils;
     13 import com.jdbc.utlis.jdbcUtilsDemo;
     14 
     15 public class helloDemo {
     16     @Test
     17     public void Myjdbc() throws ClassNotFoundException, SQLException {
     18         // 1.注册驱动
     19         Class.forName("com.mysql.jdbc.Driver");
     20         // 2.获取连接
     21         Connection connection = DriverManager.getConnection(
     22                 "jdbc:mysql://localhost:3306/day01", "root", "root");
     23         // 3.编写sql
     24         String sql = "select * from user";
     25         // 4.创建语句执行者
     26         PreparedStatement statement = connection.prepareStatement(sql);
     27         // 5.设置参数
     28 
     29         // 6.执行sql语句
     30         ResultSet resultSet = statement.executeQuery();
     31         // 7.遍历结果
     32         while (resultSet.next()) {
     33             System.out.println(resultSet.getString("id") + "----"
     34                     + resultSet.getString("username"));
     35 
     36         }
     37         resultSet.close();
     38         statement.close();
     39         connection.close();
     40     }
     41 
     42     // 测试jdbcUtils测试方法
     43     @Test
     44     public void Utils() {
     45         Connection connection = null;
     46         PreparedStatement statement = null;
     47 
     48         // 获取连接
     49         try {
     50             connection = jdbcUtils.getConnection();
     51             // 编写SQL语句
     52             String sql = "INSERT INTO user VALUES(?,?)";
     53             // 语句预编译
     54             statement = connection.prepareStatement(sql);
     55             // 设置参数
     56             statement.setInt(1, 5);
     57             statement.setString(2, "小明");
     58             // 执行SQL
     59             int i = statement.executeUpdate();
     60             // 判断语句是否执行成功
     61             if (i == 1) {
     62                 System.out.println("OK!!!");
     63             } else {
     64                 System.out.println("NO!!!");
     65             }
     66 
     67         } catch (ClassNotFoundException e) {
     68             // TODO Auto-generated catch block
     69             e.printStackTrace();
     70         } catch (SQLException e) {
     71             // TODO Auto-generated catch block
     72             e.printStackTrace();
     73         } finally {
     74             jdbcUtils.closeStatent(statement);
     75             jdbcUtils.closeConn(connection);
     76 
     77         }
     78 
     79     }
     80 
     81     // 更新操作
     82     @Test
     83     public void update() {
     84         Connection connection = null;
     85         PreparedStatement statement = null;
     86         try {
     87             // 注册驱动
     88             Class.forName("com.mysql.jdbc.Driver");
     89             // 获取连接
     90             connection = DriverManager.getConnection(
     91                     "jdbc:mysql://localhost:3306/day01", "root", "root");
     92             // sq1语句
     93             String sql = " UPDATE user SET username=? where id=? ";
     94             // 创建语句执行者
     95             statement = connection.prepareStatement(sql);
     96             // 设置参数
     97             statement.setString(1, "尔康");
     98             statement.setInt(2, 6);
     99             // 执行sql语句
    100             statement.executeUpdate();
    101         } catch (ClassNotFoundException e) {
    102             // TODO Auto-generated catch block
    103             e.printStackTrace();
    104         } catch (SQLException e) {
    105             // TODO Auto-generated catch block
    106             e.printStackTrace();
    107         }
    108         try {
    109             statement.close();
    110             connection.close();
    111         } catch (SQLException e) {
    112             // TODO Auto-generated catch block
    113             e.printStackTrace();
    114         }
    115 
    116     }
    117 
    118     // 删除操作
    119     @Test
    120     public void delete() {
    121         Connection connection = null;
    122         PreparedStatement prepareStatement = null;
    123         try {
    124             Class.forName("com.mysql.jdbc.Driver");
    125             connection = DriverManager.getConnection(
    126                     "jdbc:mysql://localhost:3306/day01", "root", "root");
    127             String sql = "delete from user where id=?";
    128             prepareStatement = connection.prepareStatement(sql);
    129             prepareStatement.setInt(1, 5);
    130             int i = prepareStatement.executeUpdate();
    131             if (i == 1) {
    132                 System.out.println("OK");
    133             }
    134         } catch (ClassNotFoundException e) {
    135             // TODO Auto-generated catch block
    136             e.printStackTrace();
    137         } catch (SQLException e) {
    138             // TODO Auto-generated catch block
    139             e.printStackTrace();
    140         }
    141 
    142         try {
    143             prepareStatement.close();
    144             connection.close();
    145         } catch (SQLException e) {
    146             // TODO Auto-generated catch block
    147             e.printStackTrace();
    148         }
    149 
    150     }
    151 }
    jdbc

     

  • 相关阅读:
    将ip地址转成二进制数据,除基本输入输出不调用库函数实现
    将ip地址转成二进制数据
    一种合并链表方法实现 严蔚敏 数据结构
    阿里巴巴集团2014年校园招聘系统工程师北京笔试题
    显示模态模态对话框和非模态对话框
    MFC中利用CString和Format成员函数将数字格式化输出
    ObjectARX2012错误1 fatal error C1083: 无法打开包括文件:“arxHeaders.h”: No such file or directory; fatal error C1083: 无法打开包括文件:“map”: No such file or directory
    Metasploit 读书笔记-神器Meterpreter
    解决:kali linux 在vmware 虚拟机中使用bridge模式上网的问题
    metasploit 读书笔记-EXPLOITATION
  • 原文地址:https://www.cnblogs.com/sehcy/p/6512403.html
Copyright © 2011-2022 走看看