Join Two String Value using MySQL CONCAT Function in Java

This tutorial is all about “Join Two String Value using MySQL CONCAT Function in Java”. This will teach you on how you can join two string values from MySQL database and display it into a Table in Java. We use “CONCAT” function in MySQL Query to join to value.

MySQL CONCAT function in Java is used to concatenate two strings to form a single string. In actual database, the first name and last name is set in specific column in MySQL. Using CONCAT, the first name and last name joined using CONCAT to form a single line of string when display in Table.

Join Two String Value using MySQL CONCAT Function in Java Steps

Create or add a new Form inside your source package and input your desired name.

Design your form just look like the image below.

Insert the following codes below to access the required libraries for this program.

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumnModel;
import net.proteanit.sql.DbUtils;

 

Insert the following codes below for variable declaration needed in accessing MySQL Database.

Connection conn=null;
ResultSet rs = null;
PreparedStatement pst=null;[/java]

Insert the codes below inside your public method to connect to your MySQL Connection class. Check your MySQL Connection statement to identify your connection method. In my case, I used "MyDbConnection".

[java]conn = dbConnection.MyDbConnection();[/java]

Create a new method inside your class. In my case, I use “loadData” method name.

[java]public final void loadData(){

}

 

Insert the following codes below inside your new created method. As you notice that CONCAT is in the SELECT query conbining the FirstName and LastName attribute from userinfo entity.

try{
String sql="SELECT concat(FirstName,' ',LastName) as NAME FROM userinfo";
pst=conn.prepareStatement(sql);
rs = pst.executeQuery(sql);
jTable1.setModel(DbUtils.resultSetToTableModel(rs));

}catch(SQLException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}

 

Run your program and the output should look like the image below.

About The Join Two String Value using MySQL CONCAT Function In Java

Project Name: Join Two String Value using MySQL CONCAT Function
Language/s Used: JAVA
Database: MySQL
Type: Desktop Application
Developer: IT SOURCECODE
Updates: 0
Join Two String Value using MySQL CONCAT Function In Java– Project Information

There are many ways to join two or more string values to form a single string in Java. This tutorial use CONCAT function in MySQL query to perform that feature. You can also use “+” sign to join two string variable in Java if you are working a program without database. If you have comments and suggestions regarding this tutorial, feel free to contact us.

Related Articles You May Like:

Leave a Comment