黑狐家游戏

java数据库编程实例,java数据库编程教程,Java数据库编程实战,深入浅出实例解析

欧气 1 0
本教程深入浅出地解析Java数据库编程实例,涵盖Java数据库编程实战技巧,通过丰富实例帮助读者掌握Java数据库编程核心。

本文目录导读:

  1. Java数据库编程基础
  2. Java数据库编程实例

随着互联网技术的飞速发展,数据库技术在各个行业中的应用越来越广泛,Java作为一种流行的编程语言,在数据库编程领域也有着广泛的应用,本文将结合Java数据库编程实例,深入浅出地解析Java数据库编程的相关知识,帮助读者掌握Java数据库编程的技巧。

Java数据库编程基础

1、数据库简介

java数据库编程实例,java数据库编程教程,Java数据库编程实战,深入浅出实例解析

图片来源于网络,如有侵权联系删除

数据库是存储、管理和检索数据的系统,常见的数据库类型有关系型数据库(如MySQL、Oracle)和非关系型数据库(如MongoDB、Redis)。

2、JDBC简介

JDBC(Java Database Connectivity)是Java语言中用于访问数据库的API,通过JDBC,Java程序可以与各种数据库进行交互。

3、JDBC连接数据库

java数据库编程实例,java数据库编程教程,Java数据库编程实战,深入浅出实例解析

图片来源于网络,如有侵权联系删除

以下是使用JDBC连接MySQL数据库的示例代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCDemo {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mydatabase";
        String username = "root";
        String password = "123456";
        Connection conn = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("数据库连接成功!");
        } catch (ClassNotFoundException e) {
            System.out.println("未找到数据库驱动");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Java数据库编程实例

1、创建数据库表

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateTableDemo {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mydatabase";
        String username = "root";
        String password = "123456";
        Connection conn = null;
        Statement stmt = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            conn = DriverManager.getConnection(url, username, password);
            // 创建Statement对象
            stmt = conn.createStatement();
            // 创建表
            String sql = "CREATE TABLE IF NOT EXISTS user (id INT, name VARCHAR(255), age INT)";
            stmt.executeUpdate(sql);
            System.out.println("表创建成功!");
        } catch (ClassNotFoundException e) {
            System.out.println("未找到数据库驱动");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("数据库操作失败");
            e.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2、插入数据

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertDataDemo {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mydatabase";
        String username = "root";
        String password = "123456";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            conn = DriverManager.getConnection(url, username, password);
            // 创建PreparedStatement对象
            String sql = "INSERT INTO user (id, name, age) VALUES (?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 1);
            pstmt.setString(2, "张三");
            pstmt.setInt(3, 20);
            // 执行插入操作
            pstmt.executeUpdate();
            System.out.println("数据插入成功!");
        } catch (ClassNotFoundException e) {
            System.out.println("未找到数据库驱动");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("数据库操作失败");
            e.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3、查询数据

java数据库编程实例,java数据库编程教程,Java数据库编程实战,深入浅出实例解析

图片来源于网络,如有侵权联系删除

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class QueryDataDemo {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mydatabase";
        String username = "root";
        String password = "123456";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            conn = DriverManager.getConnection(url, username, password);
            // 创建PreparedStatement对象
            String sql = "SELECT * FROM user WHERE age > ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 18);
            // 执行查询操作
            rs = pstmt.executeQuery();
            // 处理查询结果
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
            }
        } catch (ClassNotFoundException e) {
            System.out.println("未找到数据库驱动");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("数据库操作失败");
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

4、更新数据

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class UpdateDataDemo {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mydatabase";
        String username = "root";
        String password = "123456";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            conn = DriverManager.getConnection(url, username, password);
            // 创建PreparedStatement对象
            String sql = "UPDATE user SET age = ? WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 21);
            pstmt.setInt(2, 1);
            // 执行更新操作
            int affectedRows = pstmt.executeUpdate();
            if (affectedRows > 0) {
                System.out.println("数据更新成功!");
            } else {
                System.out.println("没有找到匹配的数据!");
            }
        } catch (ClassNotFoundException e) {
            System.out.println("未找到数据库驱动");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("数据库操作失败");
            e.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

5、删除数据

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DeleteDataDemo {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mydatabase";
        String username = "root";
        String password = "123456";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            conn = DriverManager.getConnection(url, username, password);
            // 创建PreparedStatement对象
            String sql = "DELETE FROM user WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 1);
            // 执行删除操作
            int affectedRows = pstmt.executeUpdate();
            if (affectedRows > 0) {
                System.out.println("数据删除成功!");
            } else {
                System.out.println("没有找到匹配的数据!");
            }
        } catch (ClassNotFoundException e) {
            System.out.println("未找到数据库驱动");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("数据库操作失败");
            e.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

本文通过Java数据库编程实例,深入浅出地讲解了Java数据库编程的相关知识,读者可以通过本文的学习,掌握Java数据库编程的基本技巧,为以后的项目开发打下坚实的基础,在实际开发过程中,还需不断积累经验,提高编程能力。

黑狐家游戏
  • 评论列表

留言评论