2018-01-04爬虫项目实践学习记录

1.简介

 JDBC全称为java database connectivity,是sun公司指定的java数据库连接技术的简称。 他是sun公司和数据库开发商共同开发出来的独立于DBMS的应用程序接口,它为java程序员

进行数据库编程提供了统一的API。

2.流程

``` * 向表中插入一个用户

```java

String sql = "insert into student (id,book_name,author,score,region,score) valus (?,?,?,?,?,?)";

  try {

      pstmt = conn.prepareStatement(sql);

      pstmt.setString(1, "小李");

      pstmt.setString(2, "27");

      pstmt.setString(3, "大专毕业");

      pstmt.setDate(4, new java.sql.Date(System.currentTimeMillis()));

      int i = pstmt.executeUpdate();

      System.out.println(i);

  } catch (SQLException e) {

      e.printStackTrace();

  }

```

修改用户

```java

int i = 0; String sql = "update student set name=? where name=?"; try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, "张三"); pstmt.setString(2, "李四"); //执行修改语句,显示修改 i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); }

``` * 查询一个用户,并显示所有信息

```java String sql = "select * from student where name = ? "; try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, "张三"); rs = pstmt.executeQuery(); int col = rs.getMetaData().getColumnCount(); while (rs.next()) { for (int i = 1; i <= col; i++) {

              System.out.print(rs.getMetaData().getColumnName(i)+":"+rs.getString(i) + "\t");

          }

      }

  } catch (SQLException e) {

      e.printStackTrace();

  }

``` * 删除用户

```java

int i = 0; String sql = "delete from student where name=?"; try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, "小王"); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); } catch (SQLException e) { e.printStackTrace(); } ```

你可能感兴趣的:(2018-01-04爬虫项目实践学习记录)