Merge Data Tables in Java
In Java, you can use a database connection library (such as JDBC) to connect to a database and execute SQL statements to merge two data tables. Here is an example code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class MergeTables {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/database_name";
String user = "username";
String password = "password";
try {
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
String sql = "INSERT INTO table1 SELECT * FROM table2";
stmt.executeUpdate(sql);
conn.close();
System.out.println("Tables merged successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the example above, a database connection is first created, followed by the creation of a Statement object to execute SQL statements. The SQL statement uses INSERT INTO to insert data from table2 into table1, achieving the merging of the two tables.
It is important to note that the database connection information (url, username, password) in the above code needs to be modified according to the actual situation. Additionally, in practical applications, it may be necessary to write more complex SQL statements to merge data tables based on specific requirements.