Skip to content

Instantly share code, notes, and snippets.

@theArjun
Created February 14, 2019 13:39
Show Gist options
  • Select an option

  • Save theArjun/116e97edcdde36a1981bdb671e15f080 to your computer and use it in GitHub Desktop.

Select an option

Save theArjun/116e97edcdde36a1981bdb671e15f080 to your computer and use it in GitHub Desktop.
String Format in Insert Query using JDBC Connection
package com.gces.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class AmritaJDBC {
public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
public static final String JDB_URL = "jdbc:mysql://localhost:3306/gces";
public static void main(String[] args) {
String insertQuery;
String name = "Amrita", address = "Kahun";
float salary = 200000;
insertQuery = String.format("INSERT INTO `employees` (`username`, `address`, `salary`) VALUES ('%s','%s','%f')" , name, address, salary);
try
{
Class.forName(JDBC_DRIVER);
Connection connectionObject = DriverManager.getConnection(JDB_URL,"root","");
Statement statementObject = connectionObject.createStatement();
statementObject.executeUpdate(insertQuery);
connectionObject.close();
}
catch(SQLException exc)
{
exc.printStackTrace();
}
catch(ClassNotFoundException exc)
{
exc.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment