用JSP创建Mysql数据库

在网站根目录新建一个mysql.jsp的文件,文件代码如下

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
    // 定义数据库连接参数
    String jdbcDriver = "com.mysql.jdbc.Driver";
    
    //链接端口及用户名密码
    String jdbcUrl = "jdbc:mysql://localhost:3306/?user=root&password=root&useSSL=false";
 
    // 注册JDBC驱动
    Class.forName(jdbcDriver);
 
    // 打开连接
    Connection conn = DriverManager.getConnection(jdbcUrl);
 
    // 创建数据库的SQL语句,jspcrm2是要新建的数据库
    String sql = "CREATE DATABASE IF NOT EXISTS jspcrm2";
 
    // 创建Statement对象
    Statement statement = conn.createStatement();
 
    // 执行SQL语句
    boolean isCreated = statement.execute(sql);
 
    // 关闭Statement和Connection
    statement.close();
    conn.close();
    //out.println(isCreated);
    if (isCreated) {
        out.println("数据库创建成功!");
    } else {
        out.println("数据库已存在,无需创建。");
    }
     
%>

根据你的mysql数据库去修改一下用户名、密码;

创建成功,运行结果如下

image.png

文章导航