Sometimes you want to write a bit of Java test Code, console programs, and some other frameworks, and build on Maven.
1, Java Extension Pack must be installed.
2, VS code open a specified directory, create the appropriate directory: Src\main\java, used to store the Java code we write.
3, create Pom.xml, inside copy simple project information can (according to the actual situation to adjust Java version, compile target platform, etc.), refer to the following:
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <ve Rsion>0.0.1-snapshot</version> <packaging>jar</packaging> <name>demo</name> < Description>demo project</description> <properties> <project.build.sourceEncoding>UTF-8< /project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ Project.reporting.outputencoding> <java.version>1.8</java.version> <maven.compiler.source& Gt;1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </propert Ies></project>
4. This time, the Maven for Java plugin automatically synchronizes and creates Eclipse-style project files, such as. classpath,. Project. Java projects have been developed to know the default program code is Src/main/java, the test is in Src/test/java, the second part of the meaning of creating a directory is this.
5, create the Program.java under Src\main\java, and the same way you create a console program.
Public class Program { publicstaticvoid main (string[] args) { }}
6. The remaining question now is how to find and add the jar packages we need. Suppose you need to use MySQL jar for database operation now, go to http://mvnrepository.com search MySQL first. You can see a similar return result: Mysql»mysql-connector-java is the corresponding <groupId>»<artifactId>. You can see the detailed version list when you click inside. Follow this pattern to open pom.xml to add our dependencies:
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId> mysql-connector-java</artifactid> <version>8.0.12</version> </dependency> </dependencies>
7. VS code automatically synchronizes and downloads dependencies based on changes to MAVEN files. Then you can write the code:
Class.forName ("Com.mysql.cj.jdbc.Driver"); String URL= "Jdbc:mysql://localhost:3306/uffice"; String User= "Root"; String Password= "123456"; Connection Connection=drivermanager.getconnection (URL, user, password); Statement Statement=connection.createstatement (); ResultSet ResultSet= Statement.executequery ("SELECT * from Bua_user"); while(Resultset.next ()) {intid = resultset.getint ("id"); String name= resultset.getstring ("name"); System.out.println (String.Format ("Id=%d, name=%s", ID, name));}
Quickly build a Java project based on vs code