IDEA Commercial Java Web Connection MySQL-JDBC Error Analysis (Errors Caused by Adding classes and lib)

Keywords: Java Attribute MySQL Database

IDEs such as IDEA and eclipse are quite different, so we should treat them differently. If classes and lib are added privately, accidents will happen. For example, my error is as follows:

Error:Internal error: (java.io.FileNotFoundException) E:\IntellJAVA\ONE\TEST\out\artifacts\TEST_war_exploded\WEB-INF\lib\mysql-connector-java-5.1.45-bin.jar (Deny access.)

Information:Internal caches are corrupted or have outdated format, forcing project rebuild: java.io.FileNotFoundException: E:\IntellJAVA\ONE\TEST\out\artifacts\TEST_war_exploded\WEB-INF\lib\mysql-connector-java-5.1.45-bin.jar (Deny access.)
java.io.FileNotFoundException: E:\IntellJAVA\ONE\TEST\out\artifacts\TEST_war_exploded\WEB-INF\lib\mysql-connector-java-5.1.45-bin.jar (Deny access.)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at com.intellij.openapi.util.io.FileUtil.openOutputStream(FileUtil.java:531)
    at com.intellij.openapi.util.io.FileUtil.performCopy(FileUtil.java:491)
    at . . . . . . . . . . . . . . . . . . . . 

So we should calmly analyze the reasons and find out the solutions. First, we should restore the accident scene and investigate the process in turn.

Version: IDEA Business Version 2017.3.4

MySQL-JDBC: mysql-connector-java-5.1.45-bin.jar

Prerequisite: A Java web project has been created without any changes, as shown in the figure: (Tomcat is not configured yet)

At this point, most blogs say you need to add and set classes or lib directories, and then Barabara
However, it is of no use, because IDEA itself does not need to configure the two goods.

Add some files as shown below:

At this time, configure tomcat, click and run, and find an out folder, IDEA has helped us solve the classes folder, and then look at the lib folder.

Add tomcat dependency packages and mysql-connector-java-5.1.45-bin.jar, and add Artifacts (as we will see later)





Running again and observing the changes in the out folder, Haha found an additional lib folder lying in the mysql-connector-java-5.1.45-bin.jar and tomcat dependency packages we just added.

Then test MySQL-JDBC, pass!

If you are confused, you may need to know the various directories of javaweb. You can refer to the following blog
Intellij Creates Projects from scratch: Organizing the Directories and Configuration Functions of web Projects

If you don't know much about Artifacts, you can refer to the following blog
Detailed deployment of IDEA artifacts

Test cases (login validation module):

Packed to CSDN download center, earning points by the way:
http://download.csdn.net/download/mikeoperfect/10251201

  • MySQL database
/*
Navicat MySQL Data Transfer

Source Server         : aa
Source Server Version : 50720
Source Host           : localhost:3306
Source Database       : user

Target Server Type    : MYSQL
Target Server Version : 50720
File Encoding         : 65001

Date: 2018-02-13 13:06:12
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for hh
-- ----------------------------
DROP TABLE IF EXISTS `hh`;
CREATE TABLE `hh` (
  `username` varchar(20) DEFAULT NULL,
  `password` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk;

-- ----------------------------
-- Records of hh
-- ----------------------------
INSERT INTO `hh` VALUES ('aa', '111');
  • DBBean.java
package com;
import java.sql.*;

public class DBBean
{
    //Database username
    String userName="root";
    //Database password
    String userPassword="123";
    //The URL of the database, including the encoding format used to connect to the database
    String url="jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=gb2312&useSSL=false";
    //Define a connection object
    Connection dbcon;
    //Error message string
    Statement stmt;
    ResultSet rs;
    /**
     *@Initialization operations, including assigning initial values to variables and connecting to databases
     */
    public DBBean()
    {
        //Initialization parameter values
        stmt = null;
        rs = null;
        //Connect to the database
        try
        {
            //Class packages used for declarations
            Class.forName("org.gjt.mm.mysql.Driver");
            //Get the connection object of the database
            dbcon= DriverManager.getConnection(url,userName,userPassword);
            System.out.print("df");
        }
        catch(SQLException ex)
        {
            //Print out abnormal information
            System.out.println(ex.toString());
        }
        catch(ClassNotFoundException ex)
        {
            //Print out abnormal information
            System.out.println(ex.toString());
        }
    }
    /**
     *@Execute sql execution statement on database, mainly insert and update operation, return a boolean variable
     */
    public boolean exeSql(String strSql)
    {
        try
        {
            stmt=dbcon.createStatement();
            stmt.executeUpdate(strSql);
            return true;
        }
        catch(Exception ex)
        {
            //Print out abnormal information
            System.out.println(ex.toString());
            return false;
        }
    }
    /**
     *@Execute sql query statement on database and return an object of ResultSet type
     */
    public ResultSet exeSqlQuery(String strSql)
    {
        try
        {
            stmt=dbcon.createStatement();
            rs =stmt.executeQuery(strSql);
        }
        catch(Exception ex)
        {
            //Print out abnormal information
            System.out.println(ex.toString());
            rs = null;
        }
        return rs;
    }
    public static void main(String []args){
        new DBBean();
    }
}

  • login.jsp
<%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Login interface</title>
</head>
<body>
<center>
    <h1 style="color:red">Sign in</h1>
    <form id="indexform" name="indexForm" action="logincheck.jsp" method="post">
        <table border="0">
            <tr>
                <td>Account number:</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="password">
                </td>
            </tr>
        </table>
        <br>
        <input type="submit" value="Sign in" style="color:#BC8F8F">
    </form>
    <form action="zhuce.jsp">
        <input type="submit" value="register" style="color:#BC8F8F">
    </form>
</center>
</body>
</html>
  • logincheck.jsp
<%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<jsp:useBean id="sd" class="com.DBBean" scope="application" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>

<%
    request.setCharacterEncoding("UTF-8");
    String username=(String)request.getParameter("username");
    String password=(String)request.getParameter("password");//Remove the value of login.jsp

    //Here are the database operations
    String sql="select * from hh where username="+"'"+username+"'";//Define a query statement
    ResultSet rs=sd.exeSqlQuery(sql);//Run the above statement
    if(rs.next())
    {
        if(password.equals(rs.getString("password"))){
            response.sendRedirect("loginsuccess.jsp");
        }
        else{
            out.print("<script language='javaScript'> window.alert('Password error');</script>");
            response.setHeader("refresh", "0;url=login.jsp");
        }
    }
    else
    {
        out.print("<script language='javaScript'> window.alert('Account error');</script>");
        response.setHeader("refresh", "0;url=login.jsp");
    }

%>
</body>
</html>
  • loginsuccess.jsp
<%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
<h1>Landing successfully</h1>
</body>
</html>

Summary

The most taboo for new things is that they don't know why or why. The key is that the data is too complicated. Although there is some corner of the data, how to get the most effective and useful copy in the shortest time is really a difficult problem. This time I combed all kinds of directories and their usage in javaweb in IDEA.

Posted by BMurtagh on Sun, 16 Dec 2018 19:33:03 -0800