JAVAEE connection Mysql based development environment and JDBC configuration IDEA

JAVA environment configuration

  • 1. Download jdk
    enter the official website jdk13 download the official website
    to download the appropriate installation procedure depending on your system, here my system is windows, so I chose the following SetupHere Insert Picture Description

  • 2. After downloading the installation package, follow the prompts to complete the installation.
    Encountered a problem: Since the official website to download slower rate over the wall to go to Hong Kong is still very slow. So I found a link Huawei cloud, here attached link address, with the latest 13.0.2 version is slightly different, but does not affect use. Huawei cloud link
    Here Insert Picture Description

  • 3. After installation, start the configuration environment variable.
    (1) Select computer, right-click and select Properties
    Here Insert Picture Description
    (2) Click Advanced system settings
    Here Insert Picture Description
    (3) Select Environment Variables
    installation FIG step, a new system variable called the JAVA_HOME variable value of the install jdk address.
    Here Insert Picture Description
    Select the Path variable and click Edit to add two environment variables to the path.
    It is just a new JAVA_HOME, the other is in the bin directory jdk installation path.
    Here Insert Picture Description
    Finally click OK to complete the setting environment variables.

  • 4. Verify the environment is configured successfully.
    (1) Open the window for running win + R, and enter cmd.
    Here Insert Picture Description
    (2) Enter the java -version command line
    Here Insert Picture Description
    displays information similar to the above, i.e., the configuration environment successfully JAVA

Tomcat download, installation and configuration

  • 1. Open the Tomcat's official website to download Tomcat official website
    Select version to download, here I choose Tomcat10 the latest
    Here Insert Picture Description
    version to select the desired download.
    Here Insert Picture Description
  • 2. Install the
    downloaded zip file, unzip to.
  • 3.配置环境变量。
    (1)与jdk环境变量配置类似,首先新建名为CATALINA_HOME,值为Tomcat解压地址的环境变量。
    Here Insert Picture Description
    (2)将环境变量添加到Path中。
    在Path中新建名为%CATALINA_HOME%\bin的变量。
    Here Insert Picture Description
  • 4.检验Tomcat是否配置成功。
    (1)使用Win+R打开cmd。
    (2) 使用startup其中Tomcat服务
    Here Insert Picture Description
    (3) 打开浏览器输入http://localhost:8080/,出现以下页面,则说明Tomcat配置成功。
    Here Insert Picture Description

Mysql安装与配置

  • 1.打开Mysql官网,选择Mysql Installer。Installer地址
    Here Insert Picture Description
    选择No,thanks直接开始下载。此处因为直接从浏览器中下载速度较慢,所以可以选择复制链接到迅雷中进行下载。
    Here Insert Picture Description

  • 2.下载完成后,点击进行安装。

  • 根据你的要求,选择合适的版本,进行安装。
    安装过程很简单,选择next即可。
    (注意:其中有一个页面需要输入root密码,一定要牢记自己的root密码)
    Here Insert Picture Description

  • 3.验证Mysql是否配置成功。
    (1)使用Win+R打开cmd。
    (2) 输入mysql -V可以看到mysql的版本信息。
    Here Insert Picture Description
    (3) 输入mysql -u root -p,然后输入安装时设置的root密码,可以进入数据库。
    如下图所示,表示数据库安装成功。
    Here Insert Picture Description

创建IDEA JAVAEE项目

  • 1.打开IDEA,选择Create New Project。

  • 2.选择Web Application。
    Here Insert Picture Description

  • 3.在我的IDEA中,Application Server中没有Tomcat选项,要在项目建立完成后,按以下步骤进行配置。
    (1)选择右上角的Add Configurations.
    Here Insert Picture Description
    (2)根据以下步骤添加Tomcat
    Here Insert Picture Description

注意!!
在这我遇到一个问题,此处附上供大家参考。
我第一次下载并添加的是Tomcat的最新版本,结果会提示以下错误,所以就试了试用旧一点的版本,改用了上面的9,就可以正常配置。
Here Insert Picture Description
(3)测试是否可以正常运行
修改web文件夹下WEB-INF中的index.jsp.
点击运行项目。
Here Insert Picture Description
(4)点击运行
弹出类似以下界面,说明可以正常运行,即第一个JAVAEE项目创建完成。
Here Insert Picture Description

使用JDBC连接Mysql并读取数据

  • 1.此处因为我下载Mysql数据库时,下载的比较全,已经下载了JDBC,所以不需要去官网寻找。
    在Mysql的安装位置找到JDBC对应的jar包。此处注意,如果你下载Mysql时,只下载了Mysql server,此时需要去官网下载JDBC,不过要注意版本的一致性!!
    Here Insert Picture Description
  • 2.将jar包复制到WEB-INF下新建的lib中。
  • 3.选择File下的Project Structure。
    按照以下步骤将jar包添加到项目中。
    Here Insert Picture Description
  • 4.在src文件中新家一个JAVA类,名字自取,此处我取名为test_jdbc。
    代码如下所示
import java.sql.*;

public class test_jdbc {
    public static String getdata(){
        String url="jdbc:mysql://127.0.0.1:3306/testjdbc?serverTimezone=GMT&useUnicode=true&characterEncoding=gbk ";//此处的testjdbc换成你的数据库名即可

        String driver = "com.mysql.cj.jdbc.Driver";

        String sqlString = "select * from test1 ";

        try{
            //加载驱动
            Class.forName(driver);
            //创建连接
            Connection connection =  DriverManager.getConnection(url,"root","199813");
            //获取statement
            Statement statement = connection.createStatement();
            //获得执行结果
            ResultSet resultSet = statement.executeQuery(sqlString);
            //打印结果
            while(resultSet.next()) {
                System.out.println(resultSet.getString(1));
            }
        }catch (ClassNotFoundException e){
                e.printStackTrace();
        } catch (SQLException e){
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {

        getdata();//
    }

}

这时候在右上角的运行按钮处,我没找到jdbc,但是可以选择菜单栏中run,运行程序,程序可以执行,而且执行后右上角会出现test_jdbc。

  • 5. Review the results.
    -
    Database result below
    Here Insert Picture Description
    this point, connect to the database successfully JDBC.

Tips: There are two ways to establish a database:

  1. The command line:
    Create Database XXX; // establish a database
    use XXX; // into the database
    create table XX (testinfor varchar (30 )); // Create a table
    Insert into XX value ( '') ; // data inserted
  2. Use visualization tools. Here I am using Navicat.
Published an original article · won praise 0 · Views 16

Guess you like

Origin blog.csdn.net/qq_40209622/article/details/104594234