《Java程序设计》实验报告(四)之数据库操作

实验内容及步骤:

  1. 数据库的建立、删除等。

(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)运行结果:

《Java程序设计》实验报告(四)之数据库操作_第1张图片

《Java程序设计》实验报告(四)之数据库操作_第2张图片

  1. 表的建立、删除修改等操作。

(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)运行结果:

《Java程序设计》实验报告(四)之数据库操作_第3张图片

  1. 用户的建立及设置密码。

(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)运行结果:

《Java程序设计》实验报告(四)之数据库操作_第4张图片

你的问题:

  1. 数据库的建立、删除等操作时,遇到数据库连接错误、SQL语句错误问题。
  2. 表的建立、删除修改等操作时,遇到表结构定义错误、表字段类型错误问题。

你可能感兴趣的:(实验报告,java,开发语言)