实验内容及步骤:
(1)代码: package liyuxuan.study.java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class CreateDatabase { public static void main(String[] args) { // 数据库连接参数 String url = "jdbc:mysql://localhost:3306/"; String username = "root"; String password = "root"; // 要创建的数据库名称 String databaseName = "room1"; // 创建数据库的SQL语句 String createDatabaseSQL = "CREATE DATABASE " + databaseName; try { // 连接到MySQL数据库 Connection connection = DriverManager.getConnection(url, username, password); // 创建数据库 Statement statement = connection.createStatement(); statement.executeUpdate(createDatabaseSQL); System.out.println("成功创建数据库:" +databaseName); } catch (SQLException e) { e.printStackTrace(); } } } package liyuxuan.study.java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class DropDatabase { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/"; String username = "root"; String password = "root"; String databaseName = "room1"; String dropDatabaseSQL = "DROP DATABASE " + databaseName; try { Connection connection = DriverManager.getConnection(url, username, password); Statement statement = connection.createStatement(); statement.executeUpdate(dropDatabaseSQL); System.out.println("删除成功"); statement.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }(2)运行结果:
(1)代码: package liyuxuan.study.java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class CreateTable { public static void main(String[] args) { // 数据库连接参数 String url = "jdbc:mysql://localhost:3306/room1"; String username = "root"; String password = "root"; String createTableSQL = "CREATE TABLE room (CREATE TABLE room (" + "id INT PRIMARY KEY AUTO_INCREMENT," + "typr VARCHAR(50) NOT NULL," + "size VARCHAR(50))"; try { Connection connection = DriverManager.getConnection(url, username, password); Statement statement = connection.createStatement(); statement.executeUpdate(createTableSQL); System.out.println("表创建成功"); } catch (SQLException e) { e.printStackTrace(); } } } package liyuxuan.study.java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class DropTable { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/room1"; String username = "root"; String password = "root"; try (Connection conn = DriverManager.getConnection(url, username, password); Statement stmt = conn.createStatement()) { String sql = "DROP TABLE room"; stmt.executeUpdate(sql); System.out.println("删除表成功"); } catch (SQLException e) { e.printStackTrace(); } } } (2)运行结果:
(1)代码: package liyuxuan.study.java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class InsertTable { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/room1"; String username = "root"; String password = "root"; try (Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO room (id,typr,size) VALUES (?, ?, ?)")) { pstmt.setInt(1, 1); // 设置第一个参数为整数类型的值 pstmt.setString(2, "标间"); pstmt.setString(3,"小型"); int rowsAffected = pstmt.executeUpdate(); System.out.println(rowsAffected + " 插入成功"); } catch (SQLException e) { e.printStackTrace(); } } } | |
实验内容及步骤: System.out.println(rowsAffected + " 创建成功"); pstmt.close(); } } (2)运行结果: | |
你的问题:
|