Quickly build a Java project based on vs code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.