Java和Bean Searcher与MySQL的使用

Java是一种编程语言,用于开发跨平台的应用程序。Bean Searcher是一种Java库,用于快速搜索JavaBean。MySQL是一种关系型数据库管理系统,用于存储和管理数据。

在Java中使用Bean Searcher与MySQL可以实现如下功能:

  1. 连接到MySQL数据库并创建表格。
  2. 插入数据到表格中。
  3. 使用Bean Searcher搜索数据。
  4. 修改和删除数据。

以下是使用Java和Bean Searcher与MySQL的示例代码:

  1. 连接到MySQL数据库并创建表格
import java.sql.*;

public class ConnectMySQL {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "password");

            stmt = conn.createStatement();
            String sql = "CREATE TABLE people " +
                         "(id INT NOT NULL AUTO_INCREMENT, " +
                         " name VARCHAR(255), " +
                         " age INT, " +
                         " PRIMARY KEY ( id ))";

            stmt.executeUpdate(sql);
            System.out.println("Table created successfully...");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
  1. 插入数据到表格中
import java.sql.*;

public class InsertData {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "password");

            stmt = conn.createStatement();
            String sql = "INSERT INTO people (name, age) " +
                         "VALUES ('John', 25)";

            stmt.executeUpdate(sql);
            System.out.println("Data inserted successfully...");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
  1. 使用Bean Searcher搜索数据
import java.sql.*;
import java.util.List;
import org.beansearch.BeanSearcher;
import org.beansearch.reflection.ReflectionProvider;

public class SearchData {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "password");

            stmt = conn.createStatement();
            String sql = "SELECT * FROM people";

            ResultSet rs = stmt.executeQuery(sql);

            ReflectionProvider reflectionProvider = new ReflectionProvider();
            List<People> peopleList = new BeanSearcher<>(reflectionProvider).searchBeans(rs, People.class);

            for (People person : peopleList) {
                System.out.println(person.getName() + " " + person.getAge());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
  1. 修改和删除数据
import java.sql.*;

public class ModifyData {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "password");

            stmt = conn.createStatement();
            String sql = "UPDATE people " +
                         "SET age = 30 " +
                         "WHERE name = 'John'";

            stmt.executeUpdate(sql);
            System.out.println("Data updated successfully...");

            sql = "DELETE FROM people " +
                  "WHERE name = 'John'";

            stmt.executeUpdate(sql);
            System.out.println("Data deleted successfully...");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

这是Java和Bean Searcher与MySQL的一个简单示例,可以根据需要进行修改和扩展

java + Bean Seracher + mysql使用

原文地址: https://www.cveoy.top/t/topic/fgUe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录