Is production: true

Java

Java, developed by Sun Microsystems in 1991, is a versatile and widely used programming language known for its platform independence and robust features.

The language draws developer’s eye sight due to

  • Platform Independence: Java programs can run on any device with a Java Virtual Machine (JVM), making it platform-independent. Supported by every major operating system.
  • Object-Oriented: Java is a powerful object-oriented language, allowing developers to model real-world entities using classes and objects.
  • Rich Standard Library: Java provides a comprehensive standard library with ready-to-use components, reducing development time.
  • Strong Type System: Java’s strong type system helps prevent errors at compile time, improving code robustness.
  • Automatic Memory Management: Java features automatic memory management through garbage collection, simplifying memory tasks.
  • Support for Multi-Threading: Java supports multi-threading, enabling developers to create concurrent programs.
  • Security: Java emphasizes security features like sandboxing, making it a preferred choice for secure applications.
  • Community Support: Java has a large developer community, offering resources, libraries, and frameworks.

Java continues to evolve with regular feature updates and remains a popular choice for developers across various domains.

Web Site

Tools

  • Maven
  • Gradle
  • Eclipse
  • JetBrain

Basic

A pure jdbc connection sometimes can dangerous, because if missing the try with resource pattern, the data source will not be automatically closed. A lot of non closed connection can lead to out of resource (connection leak).


Pure JDBC operations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import javax.sql.DataSource;
import java.sql.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


DataSource ds = ...;
try(Connection conn = ds.getConnection()) {    // remember to enclosed by try with resource pattern
    conn.setAutoCommit(false);              // starts with disable auto commit

    // Selecting from table
    PreparedStatement stmt = conn.prepareStatement("select fieldA,fieldB,fieldC from {table}");
    ResultSet rs = stmt.executeQuery();
    while (rs.next()){          // loop over records
        System.out.println("Field A: " + rs.getString("fieldA"));
        System.out.println("Field A: " + rs.getString("fieldB"));
        System.out.println("Field C: " + rs.getString("fieldC"));
    }


    String insertionSql = "INSERT INTO {table} (fieldA, fieldB, fieldC) VALUES (?,?,?)";
    stmt = conn.prepareStatement(insertionSql);
    stmt.setString(1, "a");
    stmt.setString(2, "b");
    stmt.setString(3, "c");
    int resultCount = stmt.executeUpdate(); // return number of record inserted

    ArrayList<Integer> list = new ArrayList<Integer>();
    List<Map<String, String>> dataToBeInserted = Arrays.asList(
            Stream.of(
                    new AbstractMap.SimpleEntry<>("fieldA", "a"),
                    new AbstractMap.SimpleEntry<>("fieldB", "b"),
                    new AbstractMap.SimpleEntry<>("fieldC", "c")
            ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),
            Stream.of(
                    new AbstractMap.SimpleEntry<>("fieldA", "d"),
                    new AbstractMap.SimpleEntry<>("fieldB", "e"),
                    new AbstractMap.SimpleEntry<>("fieldC", "f")
            ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
    );
    stmt.executeBatch();

    stmt = conn.prepareStatement(insertionSql);
    int i=0;
    for(Map<String, String> item : dataToBeInserted){
        stmt.setString(1, item.get("fieldA"));
        stmt.setString(2, item.get("fieldB"));
        stmt.setString(3, item.get("fieldC"));
        i++;
        if(i%100 == 0){
            list.add(stmt.executeBatch().length);
        } else {
            stmt.addBatch();
        }
    }
    if(i%100 != 0) {
        list.add(stmt.executeBatch().length);
    }
    System.out.println("Batch insertion result: " + list.stream().mapToInt(x->x).sum());


    /**
     *The procedure have three parameters
     *  - in value 
     *  - out value
     *  - in and out value
     * Result:
     *  - 2 param will the error message if any
     *  - 3 param will be the actual return value
     */
    String callProcedure = "{ call procedure_name(?,?,?)}";
    CallableStatement cs = conn.prepareCall(callProcedure);
    cs.setString(1, "inputValue for 1");        // The value passing in the procedure
    cs.registerOutParameter(2, Types.VARCHAR);  // The value registered for output
    cs.setString(3, "inputValue for 3");        // The value passing in the procedure
    cs.registerOutParameter(3, Types.VARCHAR);  // The value registered for output
    cs.execute();

    String error = cs.getString(2);             // extract the error if any 
    String resultValue = cs.getString(3);       // extract the value if any

    if(error != null){
        System.out.println("Error from procedure: " + error);
    } else {
        System.out.println("Result from procedure: " + resultValue);
    }
} catch (Exception ex){
    throw ex;
}
Proxy Setting for Maven
May 22, 2023 · Updated: May 22, 2023 · by Xeth · ~2 min read · English

How to setup maven proxy server configuration in different version. It is common that companies block the direct access to internet for their stuff, in case we need to do some kind …

#java #maven #issue