[Turn] to quickly build Java-based project VS Code

Sometimes readily wanted to write a little test Java code to the console-based program, it will be used in some other framework, and based on Maven build.

1, Java Extension Pack must be installed.

2, VS Code opens a specified directory, create the appropriate directories: src \ main \ java, used to store the Java code we write.

3, create a pom.xml, which simply copy project information to (adjust Java version, compiled in accordance with the actual situation of the 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>
    <version>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>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

4, this time Maven for Java plug-in will automatically synchronize files and create a project based on Eclipse style, for example: .classpath, .project. Worked on Java projects knows that the default program code in src / main / java, test in src / test / java, the second part is to create a sense this directory.

5, src \ create Program.java under main \ java, and writing to create a console program of the same.

public class Program {
    public static void main(String[] args) {
    
    }
}

6, the remaining question now is how to find and add jar package we need. Suppose now need to use the MySQL database operation jar, go http://mvnrepository.com search mysql. Similar results can be seen return: mysql »mysql-connector-java is the corresponding <groupId>» <artifactId>. You can see a detailed list of versions later point inside. According to this model open pom.xml add our dependence:

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.12</version>
    </dependency>
</dependencies>

7, VS Code will automatically synchronize and download Maven dependency according to the modified file. Next you can write 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()) {
    int id = resultSet.getInt("id");
    String name = resultSet.getString("name");

    System.out.println(String.format("id=%d, name=%s", id, name));
}

 


---------------------
Author: junchu25
Source: CNBLOGS
Original: https: //www.cnblogs.com/junchu25/p/9600183.html
Disclaimer: This article author original article, reproduced, please attach Bowen link!
Content analysis By: CSDN, CNBLOG blog articles reprinted a key plug

Guess you like

Origin www.cnblogs.com/admans/p/11566244.html