Created
February 14, 2019 13:39
-
-
Save theArjun/116e97edcdde36a1981bdb671e15f080 to your computer and use it in GitHub Desktop.
String Format in Insert Query using JDBC Connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
