How do I insert a string to a StringBuffer?

package org.kodejava.example.lang;

public class StringBufferInsert {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("kodeava");
        System.out.println("Text before        = " + sb.toString());

        //  |k|o|d|e|a|v|a|....
        //  0|1|2|3|4|5|6|7|...
        //
        // From the above sequence you can see that the index of the
        // string is started from 0, so when we insert a string in
        // the fourth offset it means it will be inserted after the
        // "e" letter. There are other overload version of this
        // method that can be used to insert other type of data such
        // as char, int, long, float, double, Object, etc.
        sb.insert(4, "j");
        System.out.println("After first insert = " + sb.toString());

        // Here we insert a string to the StringBuffer at index 8
        sb.insert(8, " examples");
        System.out.println("Final result       = " + sb.toString());
    }
}

The program will print the following output:

Text before        = kodeava
After first insert = kodejava
Final result       = kodejava examples

How do I insert a string in the StringBuilder?

package org.kodejava.example.lang;

public class StringBuilderInsert {
    public static void main(String[] args) {
        StringBuilder alphabets = new StringBuilder("abcdfghopqrstuvwxyz");
        System.out.println("alphabets = " + alphabets);

        //  |a|b|c|d|f|g|h|i|....
        //  0|1|2|3|4|5|6|7|8|...
        //
        // From the above sequence you can see that the index of the string is
        // started from 0, so when we insert a string in the fourth offset it
        // means it will be inserted after the "d" letter. There are other overload
        // version of this method that can be used to insert other type of data
        // such as char, int, long, float, double, Object, etc.
        alphabets.insert(4, "e");
        System.out.println("alphabets = " + alphabets);

        // Here we insert an array of characters to the StringBuilder.
        alphabets.insert(8, new char[] {'i', 'j', 'k', 'l', 'm', 'n'});
        System.out.println("alphabets = " + alphabets);
    }
}

The result of the code snippet above:

alphabets = abcdfghopqrstuvwxyz
alphabets = abcdefghopqrstuvwxyz
alphabets = abcdefghijklmnopqrstuvwxyz

How do I insert a record into database table?

In this example you’ll learn how to create a program to insert data into a database table. To insert a data we need to get connected to a database. After a connection is obtained you can create a java.sql.Statement object from it, and using this object we can execute some query string.

package org.kodejava.example.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class SelectStatementSample {
    public static void main(String[] args) throws Exception {
        Connection connection = null;
        try {
            // Register MySQL JDBC driver class.
            Class.forName("com.mysql.jdbc.Driver");

            // Get a connection to database.
            String url = "jdbc:mysql://localhost/kodejava";
            String user = "kodejava";
            String password = "kodejava123";
            connection = DriverManager.getConnection(url, user, password);

            // Create a statement object.
            Statement stmt = connection.createStatement();

            String sql = "INSERT INTO m_users (username, first_name, " +
                    "last_name, address) VALUES ('fbar', 'Foo', " +
                    "'Bar', 'Sunset Road 123')";

            // Call an execute method in the statement object 
            // and pass the query.
            stmt.execute(sql);
        } catch (ClassNotFoundException e) {
            System.err.println("Could not load database driver!");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
}