Tag Archives: Java learning

MySQL | ERROR : Every derived table must have its own alias [Solved]

Reason for resolution:

In the process of multi-level query, you need to give the table alias.

Code example

Wrong query method:

select * from (select s.sno from student s where s.ssex='woman');

Correct query method:

select * from (select s.sno from student s where s.ssex='woman') as mid_sno;

In fact, the difference lies in whether there is an as mid behind it_sno. This is to ensure that each derived table needs to have its own alias.

[Solved] BindingException: Type interface XXX is not known to the MapperRegistry

Problem description

When testing the selection operation of mybatis, the idea reports an error,

org.apache.ibatis.binding.BindingException: Type interface xxx is not known to the MapperRegistry.

Solution:

Reason: each mapper.xml needs to be registered in the mybatis core configuration file
add the following code to mybatis-config.xml.

<mappers>
    <mapper resource="com/dingha/dao/UserMapper.xml"/>
</mappers>

Done!

CommunicationsException: Communications link failure [How to Solve]

Today, when I was learning mybatis, I always reported an error when executing the query operation! The error is as follows:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

It probably means that there is a problem with the database connection. After checking the database name, user name and password, there are no errors. Some blogs say that they need to change the MySQL configuration file. I really think it’s too troublesome to change

Solution:

A blog commented that if MySQL is version 8.0 or above, usessl in the configuration should be set to false
and then it will be

useSSL=true

Byte 1 of UTF-8 sequence of error creating document instance. And bytes is invalid

Records of mybatis related bugs

For mybatis learning, I encountered many inexplicable bugs from the beginning! After Baidu and barrage, they didn’t solve it. Later, they changed inexplicably, but they were right. First look at the bug:

"D:\SoftWare\Idea\IntelliJ IDEA 2020.3.2\jbr\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\SoftWare\Idea\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=54216:D:\SoftWare\Idea\IntelliJ IDEA 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\SoftWare\Idea\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar;D:\SoftWare\Idea\IntelliJ IDEA 2020.3.2\plugins\junit\lib\junit5-rt.jar;D:\SoftWare\Idea\IntelliJ IDEA 2020.3.2\plugins\junit\lib\junit-rt.jar;D:\Desktop\MyBatis02\mybatis-02\target\test-classes;D:\Desktop\MyBatis02\mybatis-02\target\classes;D:\Environment\apache-maven-3.8.1\maven-repo\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar;D:\Environment\apache-maven-3.8.1\maven-repo\org\mybatis\mybatis\3.5.2\mybatis-3.5.2.jar;D:\Environment\apache-maven-3.8.1\maven-repo\junit\junit\4.13.2\junit-4.13.2.jar;D:\Environment\apache-maven-3.8.1\maven-repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.lxz.dao.UserDaoTest,getUserLike

java.lang.ExceptionInInitializerError
	at com.lxz.dao.UserDaoTest.getUserLike(UserDaoTest.java:14)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 9; 1 字节的 UTF-8 序列的字节 1 无效。
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
	at com.lxz.utils.MybatisUtils.<clinit>(MybatisUtils.java:21)
	... 26 more
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 9; 1 字节的 UTF-8 序列的字节 1 无效。
	at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:260)
	at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:126)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.<init>(XMLConfigBuilder.java:81)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:77)
	... 28 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 9; 1 字节的 UTF-8 序列的字节 1 无效。
	at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:204)
	at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:178)
	at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
	at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:306)
	at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3085)
	at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605)
	at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:534)
	at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:888)
	at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
	at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
	at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
	at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
	at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:258)
	... 31 more
Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 1 字节的 UTF-8 序列的字节 1 无效。
	at java.xml/com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:702)
	at java.xml/com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:568)
	at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1904)
	at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanData(XMLEntityScanner.java:1377)
	at java.xml/com.sun.org.apache.xerces.internal.impl.XMLScanner.scanComment(XMLScanner.java:800)
	at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanComment(XMLDocumentFragmentScannerImpl.java:1069)
	at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2898)
	... 39 more


Process finished with exit code -1

First, for reading bugs. In idea, you should read backwards, from bottom to top. Just look at the line of cause by. For the above error reports, * * * * just remove the Chinese comments in the interface mapping file and configuration file of mybatis, or change the encoding method to utf8.

About priority of. Properties file (supplementary)

We knew before that the reference configuration file can be realized through the properties attribute: the reference configuration file code given in the official mybatis document is:

	<!--Introduction of external configuration files-->
    <properties resource="db.properties">
        <property name="username" value="root"/>
        <property name="pwd" value="123123"/>
    </properties>

We can also write the code to import the configuration file as:

<properties resource="db.properties"/>

The premise of this writing is that our resource file contains the name and PWD attributes! When we use the official method of importing the configuration file, but write the name and PWD attributes in the external configuration file, idea will automatically execute the code based on the external file as the standard.

Java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver [How to Solve]

Java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver error reporting solution

Error report description problem description cause analysis solution operation results

Error reporting description

java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver reports an error


Problem description

when learning the contents of Java database, because the old version of teaching materials are used, and the Java version has been updated and some functions have been deleted, an error message prompted by java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver appears when reading the database: 

try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        }

Error message: java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver


Cause analysis

since JDK1.8, the JDBC ODBC bridge has been deleted, so the ODBC driver cannot be used </ font>


Solution

 

    1. Step 1: install the old version of JDK. I install the download link of version 1.7 here: just click the next step for the jdk1.7 installation step. Step 2: change the Java environment

 

    1. friends using eclipse can refer to this blog:

 

    1. java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcodbcdriver solution 2.1 I use the integrated development environment of idea, The method is as follows:

 

    1. in file – project structure

    1. in project settings, select the JDK version downloaded for you by project SDK (here is 1.7)

 

    1. and click apply to run


Operation results

run smoothly ^ ^

[Solved] Java Error: Must declare a named package because this compilation unit is associated to the named module

Using the latest version of eclipse, you may encounter such a problem (error report)

“Must declare a named package because this compilation unit is associated to the named module ‘xxx'”
the reason for this is that in the latest version of eclipse, the package needs to be defined while defining a class.

How to define a package:

In the eclipse menu bar, select File – & gt; New-> Class command, in the Import dialog box, find the package, and then name it( The position of the red line cannot be empty)

Generating equals/hashCode implementation but without a call to superclass

Warning message

Generating equals/hashCode implementation but without a call to
superclass, even though this class does not extend java.lang.Object.
If this is intentional, add ‘(callSuper=false)’ to your type.

Baidu translation results:

Generate the equals/hashcode implementation, but do not call the superclass, even if the class does not extend Java. Lang. object. If this is intentional, add “(callsuper = false)” to your type.

terms of settlement

Method 1

Add the @ equalsandhashcode (callsuper = true) annotation to the class

After adding, you can include the properties of its parent class when generating the equals/hashcode method

Method 2

Add content to the configuration class, but I haven’t used it, so I only recommend a link. The address is: https://www.cnblogs.com/zt007/p/13086238.html

Springboot project: error parsing HTTP request header note: further occurrences of HTTP request parsing

There is no error reported after the spirngboot project is started, but when testing the interface with postman, it is found that there will be intermittent (one good and one bad) errors

Error parsing HTTP request header
 Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException : Invalid character found in method name. HTTP method names must be tokens

Simple appearance is also very normal, it is a success, a mistake, make me at a loss.

Looking up the online materials, 98% of them said that the configuration change of the maximum header value of Tomcat would be good, but springboot is built-in Tomcat,

The method of modification is as follows: sever.tomcat.max -http-header-size=8192

But this configuration has been abandoned by the boot project of higher version,

It can be changed to: sever.max -http-header-size=8192

Full of joy, thought that good, and then found No.

The other 2% said it might be related to the HTTPS protocol, so it could be changed to http.

But I started it locally. It’s always http.

After struggling for a long time, I found that the culprit actually added content length to the header when requesting and gave it a fixed value, so I would report an error. Stupid cry~~

As for the function of modifying the max HTTP header size, I set this to 1 and request again to find that the error reported is not the same as my previous one, but:

Error processing request
org.apache.coyote .http11.HeadersTooLargeException: An attempt was made to write more data to the response headers than there was room available in the buffer.

About Java File.separator

in Windows path separator and Linux path separator is different, when the absolute path is used directly, cross-platform will be exposed “No such file or diretory” exception.

File
File file1 = new File (“C:\ TMP \test.txt”);
File file1 = new File (“C:\ TMP \test.txt”)

File file2 = new File (“/ TMP /test.txt”);
File file2 = new File (“/ TMP /test.txt”);

if cross-platform is considered, it is best to say:
File myFile = new File(“C:” + file.separator + “TMP” + file.separator, “test.txt”);
File myFile = new File(“C:” + file.separator + “TMP” + file.separator, “test.txt”);

The

File class has several static fields that are similar to separator, which are system related and should be used as far as possible in programming.

separatorChar

public static final char separatorChar

is the default name separator associated with the system. This field is initialized to the first character that contains the system property file.separator value. On UNIX systems, the value of this field is ‘/’; On Microsoft Windows, it is ‘\’.

separator

public static final String separator

is the system-specific default name separator, which is represented as a string for convenience. This string contains only one character, separatorChar.

pathSeparatorChar

public static final char pathSeparatorChar

is the system-dependent path separator. This field is initialized as the first character that contains the system property path.separator value. This character is used to separate filenames in a given file sequence in the form of a path list. On UNIX systems, this field is ‘:’; On Microsoft Windows, it is ‘; ‘.

pathSeparator

public static final String pathSeparator

is the system-dependent path separator, which is represented as a string for convenience. This string contains only one character, pathSeparatorChar.

☞ warm prompt: to return to my blog index

The differences between the equals method in the string class and the equals method in the object class

1. First, let’s look at the equals method

in Object class

public boolean equals(Object obj) {
        return (this == obj);
    }

we can see if the Object class is judging the same Object, let’s take a look at the following example

Employee emp1 = new Employee("张三", 5000, 2005, 5 ,5);
Employee emp2 = new Employee("张三", 5000, 2005, 5 ,5);
System.out.println(emp2.equals(emp1));

stop here and think about what’s going to come out?

, I’m sure a lot of beginners like me think it will output true. But the answer is false. Why?We’re looking at the only line of code in the body of the method

        return (this == obj);

, what does this==obj stand for?In fact, what we’re doing here is determining whether this is the same address as obj, or whether it’s the same object in the heap.

it is easy to see that emp1 and emp2 do not store the same address in the stack, so it will print false.

2. Next, let’s look at the equals method in the String class

public boolean equals(Object anObject) {
		//判断是否同一个对象
        if (this == anObject) {
            return true;
        }
        //判断anObject 是否是String的实例对象
        	//如果是的话判断两个String对象的长度是否相等
        			//如果相等再判断每一个字符是否相等
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

The equals method in the

String class does two things. The first method is the same as the equals method in Object. If not the same object, then determine whether the length is equal, then determine whether each character is equal.