Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.coderslab;
- import com.mysql.jdbc.Connection;
- import java.math.BigDecimal;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- public class Main {
- public static void main(String[] args) {
- addNewProduct("Nowy produkt 2", "Opis nr 2", 4.4);
- }
- public static void addNewProduct(String productName, String productDesc, double price) {
- try {
- Connection connection = (Connection) DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/products_ex?useSSL=false",
- "root",
- "tajne123"
- );
- String queryToAdd = "insert into products(name, description, price) values(?,?,?)";
- PreparedStatement preparedStatement = connection.prepareStatement(queryToAdd);
- preparedStatement.setString(1, productName);
- preparedStatement.setString(2, productDesc);
- BigDecimal bigDecimal = new BigDecimal(price);
- preparedStatement.setBigDecimal(3, bigDecimal);
- preparedStatement.executeUpdate();
- } catch (SQLException ex) {
- System.out.println(ex.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment