D
D
Danil2021-01-20 09:00:37
Java
Danil, 2021-01-20 09:00:37

Data from db to xml to java?

There is a database with one table and one field I
connect to the database using JDBC
I also add records to the database from the java code. It is
necessary to extract all the record data to the xml file using java
Code:

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;

public class DBDemo {

    public static void main(String[] args) {
         try{
             String url = "jdbc:mysql://localhost/test?useUnicode=true&serverTimezone=UTC&useSSL=false";
             String username = "root";
             String password = "root";
             Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance();
              
             try (Connection conn = DriverManager.getConnection(url, username, password)){
            	System.out.println("Подключение успешно установлено!");
                Statement statement = conn.createStatement();
                int rows = statement.executeUpdate("INSERT INTO TEST(FIELD) values('12345'), ('3214')");
                System.out.printf("Добавлено: %d записей", rows);
             }
         }
         catch(Exception ex){
             System.out.println("Connection failed...");
              
             System.out.println(ex);
         }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
acwartz, 2021-01-20
@acwartz

Well, here it is described in detail: https://www.ibm.com/developerworks/xml/tutorials/x...
In short:
you get a sample.
Create an xml document.
You manually write nodes and data nodes in it.
Write down xml file somewhere there or stream.
Profit.
If not a tutorial, then databases themselves can generate XML:
https://ils.unc.edu/courses/2013_fall/inls623_001/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question