您的位置:首页 > 数据库

JDBC的使用及操作过程

2017-05-12 16:42 169 查看

JDBC的使用及操作过程

概念:

JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据。JDBC代表Java数据库连接。

JDBC库中所包含的API通常与数据库使用于:

连接到数据库

创建SQL或MySQL语句

在数据库中执行SQL或MySQL查询

查看和修改数据库中的数据记录

使用JDBC的前提条件

1.配置环境变量

请确认您已完成以下设置:

JAVA(JDK)安装

数据库系统的安装(如:MySQL的安装)

除上述者外环境配置外,还需要建立一个数据库,为本程项目作为测试使用。假设创建一个数据库:
test
,在这个数据库上创建一张表:
employees


2.创建JDBC程序应用

2.1 导入包

在程序中包含数据库编程所需的JDBC类。大多数情况下,使用
import java.sql.*
就足够了,如下所示:

//STEP 1. Import required packages
import java.sql.*;

2.2 注册JDBC的驱动程序

需要初始化驱动程序,这样就可以打开与数据库的通信。以下是代码片段实现这一目标:

//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");

2.3 创建一个连接

通过DriverManager类的getConnection方法建立连接时,需要注意getConnection会抛出SQLException异常,需要在try/catch块 中捕获

static final String USER = "root";
static final String PASS = "pwd123456";
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);

2.4 执行增删查找的功能

2.4.1 Insert

private static int insert(Student student) {
   Connection conn = getConn();
   int i = 0;
   String sql = "insert into students (Name,Sex,Age) values(?,?,?)";
   PreparedStatement pstmt;
   try {
       pstmt = (PreparedStatement) conn.prepareStatement(sql);
       pstmt.setString(1, student.getName());
       pstmt.setString(2, student.getSex());
       pstmt.setString(3, student.getAge());
       i = pstmt.executeUpdate();
       pstmt.close();
       conn.close();
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return i;
}

2.4.2 UpData

private static int update(Student student) {
   Connection conn = getConn();
   int i = 0;
   String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'";
   PreparedStatement pstmt;
   try {
       pstmt = (PreparedStatement) conn.prepareStatement(sql);
       i = pstmt.executeUpdate();
       System.out.println("resutl: " + i);
       pstmt.close();
       conn.close();
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return i;
}

2.4.3 select

private static Integer getAll() {
   Connection conn = getConn();
   String sql = "select * from students";
   PreparedStatement pstmt;
   try {
       pstmt = (PreparedStatement)conn.prepareStatement(sql);
       ResultSet rs = pstmt.executeQuery();
       int col = rs.getMetaData().getColumnCount();
       System.out.println("============================");
       while (rs.next()) {
           for (int i = 1; i <= col; i++) {
               System.out.print(rs.getString(i) + "\t");
               if ((i == 2) && (rs.getString(i).length() < 8)) {
                   System.out.print("\t");
               }
            }
           System.out.println("");
       }
           System.out.println("============================");
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return null;
}

2.4.4 delete

private static int delete(String name) {
   Connection conn = getConn();
   int i = 0;
   String sql = "delete from students where Name='" + name + "'";
   PreparedStatement pstmt;
   try {
       pstmt = (PreparedStatement) conn.prepareStatement(sql);
       i = pstmt.executeUpdate();
       System.out.println("resutl: " + i);
       pstmt.close();
       conn.close();
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return i;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息