Java JDBC MySQL Connection putData(int tID, String toolID)

Here you can find the source of putData(int tID, String toolID)

Description

put Data

License

Open Source License

Declaration

public static void putData(int tID, String toolID) 

Method Source Code


//package com.java2s;
/*//from   w ww.  ja va  2s . c  om
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import java.sql.SQLException;
import java.sql.Timestamp;

public class Main {
    private static final String addDataQ = "INSERT INTO  `toolsdb`.`customer` (`tenantID`, `toolID`, `time`) VALUES (?, ?, ?)";
    private static String dbUrl = "jdbc:mysql://localhost:3306/toolsdb";
    private static String user = "root";
    private static String password = "root";

    public static void putData(int tID, String toolID) {
        Connection con = null;
        PreparedStatement preparedStatement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(dbUrl, user, password);
            preparedStatement = con.prepareStatement(addDataQ);
            preparedStatement.setInt(1, tID);
            preparedStatement.setString(2, toolID);
            preparedStatement.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. getMysqlIndices(String conString)
  2. getNewDatabaseConnection(String dbUser, String dbPassword)
  3. getRssminerDB()
  4. getSearchReaderConnection()
  5. openConnection()
  6. query(String sql)
  7. queryAll(String sql, List params)
  8. readMapListBySQL(String driverClass, String host, String port, String database, String user, String password, String sql)
  9. removeById(int Id)

  10. HOME | Copyright © www.java2s.com 2016