Java JDBC MySQL Connection addItem(int itemId, String itemName, int page, String tradeable)

Here you can find the source of addItem(int itemId, String itemName, int page, String tradeable)

Description

add Item

License

Open Source License

Declaration

public static void addItem(int itemId, String itemName, int page, String tradeable) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;

import java.sql.SQLException;

public class Main {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/twitch";
    static final String USER = "java";
    static final String PASS = "java";

    public static void addItem(int itemId, String itemName, int page, String tradeable) {

        Connection dbConnection = null;

        PreparedStatement preparedStatement = null;

        String sql = "INSERT INTO items" + " (id, itemName, itemId, page, tradable)" + " VALUES (NULL,?,?,?,?)";

        try {/*from w  ww  .  j a  va  2  s .co  m*/

            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(sql);

            preparedStatement.setString(1, itemName);
            preparedStatement.setInt(2, itemId);
            preparedStatement.setInt(3, page);
            preparedStatement.setString(4, tradeable);

            //logPreparedStatement(preparedStatement);
            preparedStatement.executeUpdate();

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } finally {
            try {
                dbConnection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    private static Connection getDBConnection() {

        Connection dbConnection = null;

        try {

            Class.forName(JDBC_DRIVER);

        } catch (ClassNotFoundException e) {

            System.out.println(e.getMessage());

        }

        try {

            dbConnection = DriverManager.getConnection(DB_URL, USER, PASS);
            return dbConnection;

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        }

        return dbConnection;

    }
}

Related

  1. checkConnection(String dbFile)
  2. computeAverageGeneIdsPerName()
  3. createConnection()
  4. createConnection(String hostname, String port, String database, String username, String password)