“
java,import java.sql.Connection;,import java.sql.DriverManager;,import java.sql.ResultSet;,import java.sql.Statement;,,public class DatabaseExample {, public static void main(String[] args) {, try {, // 加载数据库驱动, Class.forName("com.mysql.jdbc.Driver");,, // 连接数据库, Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_db", "username", "password");,, // 创建Statement对象, Statement statement = connection.createStatement();,, // 执行SQL查询, ResultSet resultSet = statement.executeQuery("SELECT * FROM users");,, // 处理查询结果, while (resultSet.next()) {, int id = resultSet.getInt("id");, String name = resultSet.getString("name");, System.out.println("ID: " + id + ", Name: " + name);, },, // 关闭资源, resultSet.close();, statement.close();, connection.close();, } catch (Exception e) {, e.printStackTrace();, }, },},
“以下是一个简单的Java数据库代码示例,使用了JDBC(Java Database Connectivity)来连接和操作MySQL数据库,在这个示例中,我们将创建一个名为"students"的表,并插入一些数据。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class DatabaseExample { public static void main(String[] args) { // 数据库连接信息 String url = "jdbc:mysql://localhost:3306/test_db"; String user = "root"; String password = "your_password"; // 加载驱动 try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); return; } // 连接数据库 try (Connection connection = DriverManager.getConnection(url, user, password)) { // 创建表 String createTableSQL = "CREATE TABLE IF NOT EXISTS students (" + "id INT PRIMARY KEY AUTO_INCREMENT," + "name VARCHAR(50) NOT NULL," + "age INT NOT NULL," + "grade VARCHAR(10) NOT NULL" + ")"; try (PreparedStatement preparedStatement = connection.prepareStatement(createTableSQL)) { preparedStatement.executeUpdate(); System.out.println("Table 'students' created successfully!"); } // 插入数据 String insertDataSQL = "INSERT INTO students (name, age, grade) VALUES (?, ?, ?)"; try (PreparedStatement preparedStatement = connection.prepareStatement(insertDataSQL)) { preparedStatement.setString(1, "张三"); preparedStatement.setInt(2, 18); preparedStatement.setString(3, "高三"); int rowsAffected = preparedStatement.executeUpdate(); System.out.println("Inserted " + rowsAffected + " row(s) into 'students' table."); } // 查询数据 String queryDataSQL = "SELECT * FROM students"; try (PreparedStatement preparedStatement = connection.prepareStatement(queryDataSQL)) { java.sql.ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); String grade = resultSet.getString("grade"); System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age + ", Grade: " + grade); } } } catch (SQLException e) { e.printStackTrace(); } } }
在这个示例中,我们首先加载了MySQL的JDBC驱动,然后使用DriverManager.getConnection()
方法连接到数据库,接下来,我们创建了一个名为"students"的表,并插入了一些数据,我们查询了表中的所有数据并将其打印到控制台。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/643100.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复