Importing csv file in mysql database - mysql

I have a csv file with 8 columns. When I am importing the csv file into mysql database, it is ignoring the 2nd column and shifting the rest of the data from the other columns to the left, please help.
I think the problem is with the csv file as some csv files are uploaded successfully. How can I fix it?
This is my query to store csv file into database:
LOAD DATA INFILE '$file_name' INTO TABLE import
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(#srno,#customer_name,#date,#mobno,#city,#state,#type,#telecaller)
SET customer_name=#customer_name,date=#date,mobno=#mobno,city=#city,
state=#state,type=#type,telecaller=#telecaller,datetime='$datetime'

This code worked for me. Test it out with a local database.
package Example_Import;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.io.*;
import java.lang.String;
import java.util.Scanner;
public class Example {
//import mysql-connector-java-5.1.32.jar
//http://www.tutorialspoint.com/jdbc/jdbc-create-tables.htm
private static String JDBC_URL = "jdbc:mysql://localhost:3306/sakila";
private static String USER = "root";
private static String PASSWORD = "2004";
public static void main(String[] args) throws IOException {
Connection myConnection = null;
Statement statement = null;
PreparedStatement preparedStatement = null;
try{
//CONNECTING TO THE DATABASE
myConnection = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
System.out.println("Connected to database"); //CONNECTED
//CREATING TABLE IN THE DATABASE
System.out.println("Creating table in given database...");
statement = myConnection.createStatement();
String sql = "CREATE TABLE REGISTRATION " +
"(id INTEGER not NULL, " +
" last VARCHAR(25), " +
" first VARCHAR(25), " +
" balance FLOAT, " +
" Credit FLOAT," +
" Date DATE," +
" Rating CHAR," +
" PRIMARY KEY ( id ))";
statement.executeUpdate(sql);
System.out.println("Created table in given database...");
statement.close(); //closing statement
//CREATING A PREPARED STATEMENT
String insertTableSQL = "INSERT INTO REGISTRATION"
+ "(id, last, first, balance, Credit, Date, Rating) VALUES"
+ "(?,?,?,?, ?, ?, ?)";
preparedStatement = myConnection.prepareStatement(insertTableSQL);
//RETRIEVING INFORMATION FROM CSV FILE
//opening a file input stream
BufferedReader reader = new BufferedReader(new FileReader("SAMPLE.csv"));
String line = null; //line read from csv
Scanner scanner = null; //scanned line
SimpleDateFormat date = new SimpleDateFormat("mm/DD/yyyy");
reader.readLine(); //omits the first line
//READING FILE LINE BY LINE AND UPLOADING INFORMATION TO DATABASE
while((line = reader.readLine()) != null){
scanner = new Scanner(line);
scanner.useDelimiter(",");
while(scanner.hasNext()){
preparedStatement.setInt(1,Integer.parseInt(scanner.next()));
preparedStatement.setString(2, scanner.next());
preparedStatement.setString(3, scanner.next());
preparedStatement.setFloat(4, Float.parseFloat(scanner.next()));
preparedStatement.setFloat(5, Float.parseFloat(scanner.next()));
try {
java.util.Date d;
d = date.parse(scanner.next());
preparedStatement.setDate(6, new java.sql.Date(d.getTime()));
} catch (ParseException e) {
e.printStackTrace();
}
preparedStatement.setString(7, scanner.next());
}
preparedStatement.executeUpdate();
}
preparedStatement.close();
System.out.println("Data imported");
reader.close(); //closing CSV reader
} catch (SQLException e){
e.printStackTrace();
}finally{//CLOSING CONNECTION
try{
if(statement!=null)
myConnection.close();
}catch(SQLException se){
}// do nothing
try{
if(myConnection!=null)
myConnection.close();
}catch(SQLException se){
se.printStackTrace();
}
System.out.println("Connection closed");
}
}
}

Related

failed to upload multiple files simultaneously into mysql database using jsp and java

I am trying to upload multiple files simultaneously but my code is uploading only one file into MySQL database even if I select multiple files while uploading. There is one folder named as "resources" in my code. So when I click on submit button with multiple files selected for uploading then all files are storing into "resources" folder but among all files only one record is shown in database. Please help me where I am getting wrong. I searched for this answer from 2 weeks.
Here is my code.. UploadServletClass.java.
package com.servlets;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import com.servlet.db.DB;
import java.util.List;
import java.util.stream.Collectors;
#WebServlet(name = "UploadServletClass", urlPatterns = {
"/UploadServletClass"
})
#MultipartConfig(fileSizeThreshold = 1024 * 1024 * 10, //10MB
maxFileSize = 1024 * 1024 * 1000, //1GB
maxRequestSize = 1024 * 1024 * 1000) //1GB
public class UploadServletClass extends HttpServlet {
PrintWriter out = null;
Connection con = null;
PreparedStatement ps = null;
HttpSession session = null;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain;charset=UTF-8");
try {
out = response.getWriter();
session = request.getSession(false);
String folderName = "resources";
//String uploadPath = request.getServletContext().getRealPath("") + File.separator + folderName;
String uploadPath = request.getServletContext().getRealPath("") + File.separator + folderName;
File dir = new File(uploadPath);
if (!dir.exists()) {
dir.mkdirs();
}
// Part filepart = request.getPart("file");
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
String fileName = "";
String path = folderName + File.separator + fileName;
Timestamp added_date = new Timestamp(System.currentTimeMillis());
String div = request.getParameter("division"); //textbox value of division field
String repunit = request.getParameter("reportingunit"); //textbox value of reportingunit field
String docnum = request.getParameter("documentnumber"); //textbox value of documentnumber field
String docName = request.getParameter("documentname"); //textbox value of documentname field
String docUploader = request.getParameter("documentuploader"); //textbox value of documentuploader field
String docOwner = request.getParameter("documentowner"); //textbox value of documentowner field
String docType = request.getParameter("Document_Type"); //textbox value of Document_Type field
String docCategory = request.getParameter("Document_Category"); //textbox value of Document_Category field
System.out.println("filename:" + fileName);
System.out.println("path:" + uploadPath);
System.out.println("Name:" + firstName);
try {
con = DB.getConnection();
System.out.println("connection done");
List < Part > fileParts = request.getParts().stream().filter(part - > "file".equals(part.getName())).collect(Collectors.toList());
for (Part filePart: fileParts) {
// fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
//fileName = filePart.getSubmittedFileName();
filePart.write(uploadPath + File.separator + fileName);
InputStream ins = filePart.getInputStream();
String sql = "insert into newfiles(firstname,lastname,filename,path,division,reportingunit,documentnumber,documentname,documentuploader,documentowner,documenttype,documentcategory,added_date) values(?,?,?,?,?,?,?,?,?,?,?,?,?)"; //inserting all values into database
ps = con.prepareStatement(sql);
Files.copy(ins, Paths.get(uploadPath + File.separator + fileName), StandardCopyOption.REPLACE_EXISTING);
}
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setString(3, fileName);
ps.setString(4, path);
ps.setString(5, div); //index specifies the respective parameter in the query
ps.setString(6, repunit);
ps.setString(7, docnum);
ps.setString(8, docName);
ps.setString(9, docUploader);
ps.setString(10, docOwner);
ps.setString(11, docType);
ps.setString(12, docCategory);
ps.setTimestamp(13, added_date);
int status = ps.executeUpdate();
if (status > 0) {
session.setAttribute("filename", fileName);
String msg = " File Uploaded Successfully...";
request.setAttribute("msg", msg);
RequestDispatcher rd = request.getRequestDispatcher("/process.jsp");
rd.forward(request, response);
System.out.println("File Uploaded Successfully");
System.out.println("Uploaded Path:" + uploadPath);
}
} catch (SQLException e) {
out.println("Exception:" + e);
System.out.println("Exception1:" + e);
} finally {
try {
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
out.println(e);
}
}
} catch (IOException | ServletException e) {
out.println("Exception:" + e);
System.out.println("Exception2:" + e);
}
}
}
You are still calling executeUpdate() just once. Here what I would do (just the try block, the rest is more or less OK):
try {
con = DB.getConnection();
System.out.println("connection done");
List < Part > fileParts = request.getParts().stream().filter(part - > "file".equals(part.getName())).collect(Collectors.toList());
String sql = "insert into newfiles(firstname,lastname,filename,path,division,reportingunit,documentnumber,documentname,documentuploader,documentowner,documenttype,documentcategory,added_date) values(?,?,?,?,?,?,?,?,?,?,?,?,?)"; //inserting all values into database
ps = con.prepareStatement(sql);
int counter = 0;
for (Part filePart: fileParts) {
// fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
//fileName = filePart.getSubmittedFileName();
filePart.write(uploadPath + File.separator + fileName);
InputStream ins = filePart.getInputStream();
Files.copy(ins, Paths.get(uploadPath + File.separator + fileName), StandardCopyOption.REPLACE_EXISTING);
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setString(3, fileName);
ps.setString(4, path);
// all the other ps.setXY calls
int status = ps.executeUpdate();
if (status > 0) {
counter++;
System.out.println("Successfully uploaded file #" + counter + ", uploaded path:" + uploadPath);
} else {
// handle error
}
}
if (counter > 0) {
String msg = "Successfully uploaded " + counter + " files.";
request.setAttribute("msg", msg);
System.out.println(msg);
session.setAttribute("filename", fileName); // this is just the last file name, does it make sense?
}
RequestDispatcher rd = request.getRequestDispatcher("/process.jsp");
rd.forward(request, response);
} catch (SQLException e) {
...
I.e. you have to INSERT a DB record for each uploaded file. Btw. I would recommend to re-design your DB and have one table for documents, and a second table for files, using 1:N relation between documents and files.

How do I work with aws rds myql on eclipse(Java)?

I have downloaded aws sdk and connected my account and the database. But now I do not know what I need to do next. How do insert, delete or create table through java on eclipse.
I know to do these in a local database. I tried changing the url in getConnection() function to the my endpoint on eclipse but I keep getting error stating
"Access denied for user 'aws'#'xxx.xxx.xxx.xxx' (using password: YES)" (real IP modified for security reasons).
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class MySQLAccess {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
private static final String url = "jdbc:mysql://aws.cyduxshnlizb.ap-south-1.rds.amazonaws.com:3306";
final private String user = "myusername";
final private String passwd = "mypassword";
public void readDataBase() throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection(url,user,passwd);
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// Result set get the result of the SQL query
resultSet = statement
.executeQuery("select * from feedback.comments");
writeResultSet(resultSet);
// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into feedback.comments values (default, ?, ?, ?, ? , ?, ?)");
// "myuser, webpage, datum, summary, COMMENTS from feedback.comments");
// Parameters start with 1
preparedStatement.setString(1, "Test");
preparedStatement.setString(2, "TestEmail");
preparedStatement.setString(3, "TestWebpage");
preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
preparedStatement.setString(5, "TestSummary");
preparedStatement.setString(6, "TestComment");
preparedStatement.executeUpdate();
preparedStatement = connect
.prepareStatement("SELECT myuser, webpage, datum, summary, COMMENTS from feedback.comments");
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);
// Remove again the insert comment
preparedStatement = connect
.prepareStatement("delete from feedback.comments where myuser= ? ; ");
preparedStatement.setString(1, "Test");
preparedStatement.executeUpdate();
resultSet = statement
.executeQuery("select * from feedback.comments");
writeMetaData(resultSet);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void writeMetaData(ResultSet resultSet) throws SQLException {
// Now get some metadata from the database
// Result set get the result of the SQL query
System.out.println("The columns in the table are: ");
System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
for (int i = 1; i<= resultSet.getMetaData().getColumnCount(); i++){
System.out.println("Column " +i + " "+ resultSet.getMetaData().getColumnName(i));
}
}
private void writeResultSet(ResultSet resultSet) throws SQLException {
// ResultSet is initially before the first data set
while (resultSet.next()) {
// It is possible to get the columns via name
// also possible to get the columns via the column number
// which starts at 1
// e.g. resultSet.getSTring(2);
String user = resultSet.getString("myuser");
String website = resultSet.getString("webpage");
String summary = resultSet.getString("summary");
Date date = resultSet.getDate("datum");
String comment = resultSet.getString("comments");
System.out.println("User: " + user);
System.out.println("Website: " + website);
System.out.println("Summary: " + summary);
System.out.println("Date: " + date);
System.out.println("Comment: " + comment);
}
}
// You need to close the resultSet
private void close() {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
}
}

Want to update MySQL DB with the values from excel sheet in minimal time

I have written below code to read cells from excel and then update it to MySQL table. There are more than 2000 records and this code is only updating the last record but not all the records. If I put 'pstm.executeBatch();' inside for loop, then it updates all the records but one by one, which takes about 2 minutes. I want to reduce this time, so added "&rewriteBatchedStatements=true" in URL and put 'pstm.executeBatch();' outside for loop. In console it shows reading of all the records but the database has only the last record updated.
package com.company.testdata;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class UpdateDataCopy {
public static void main(String[] args) throws Exception {
String user = "root";
String pass = "test";
String jdbcUrl = "jdbc:mysql://192.1.2.1/db_bro_mumbai?useSSL=false"+
"&rewriteBatchedStatements=true";
String driver = "com.mysql.jdbc.Driver";
try {
PreparedStatement pstm = null;
Class.forName(driver);
Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
FileInputStream input = new FileInputStream("E:\\Work\\TestData.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(input);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row;
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
row = (XSSFRow) sheet.getRow(i);
int id = (int)row.getCell(0).getNumericCellValue();
System.out.println(id);
String firstname = row.getCell(2).getStringCellValue();
System.out.println(firstname);
String middlename = row.getCell(3).getStringCellValue();
System.out.println(middlename);
String lastname = row.getCell(4).getStringCellValue();
System.out.println(lastname);
int physicalFitness = (int)row.getCell(25).getNumericCellValue();
System.out.println(physicalFitness);
String sql = "UPDATE fitness_details as p SET p.physicalFitness = ? "
+ " WHERE CandidateID_FK1 = ?";
pstm = (PreparedStatement) myConn.prepareStatement(sql);
pstm.setInt(1, physicalFitness);
pstm.setInt(2, id);
pstm.addBatch();
//Adding below line will update record one by one which is time consuming, so I commented this and added it after for loop.
//pstm.executeBatch();
System.out.println("Import rows " + i);
}
pstm.executeBatch();
System.out.println("Imported");
//myConn.commit();
//pstm.close();
//myConn.close();
input.close();
}
catch (Exception exc) {
exc.printStackTrace();
throw new ServletException(exc);
}
}
}
Has mentioned in my comment...
package com.company.testdata;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class UpdateDataCopy {
public static void main(String[] args) throws Exception {
String user = "root";
String pass = "test";
String jdbcUrl = "jdbc:mysql://172.16.206.197/db_bro_mumbai?useSSL=false"+
"&rewriteBatchedStatements=true";
String driver = "com.mysql.jdbc.Driver";
try {
PreparedStatement pstm = null;
Class.forName(driver);
Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
String sql = "UPDATE fitness_details as p SET p.physicalFitness = ? WHERE CandidateID_FK1 = ?";
pstm = myConn.prepareStatement(sql);
FileInputStream input = new FileInputStream("E:\\Work\\TestData.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(input);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row;
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
row = (XSSFRow) sheet.getRow(i);
int id = (int)row.getCell(0).getNumericCellValue();
String firstname = row.getCell(2).getStringCellValue();
String middlename = row.getCell(3).getStringCellValue();
String lastname = row.getCell(4).getStringCellValue();
int physicalFitness = (int)row.getCell(25).getNumericCellValue();
pstm.setInt(1, physicalFitness);
pstm.setInt(2, id);
pstm.addBatch();
System.out.println("Import rows " + I + "ID: " + id + " Middlename:" + middlename + " Lastname:" + lastname + " Physicalfitness:" + physicalFitness );
}
pstm.executeBatch();
System.out.println("Imported");
//myConn.commit();
pstm.close();
myConn.close();
input.close();
}
catch (Exception exc) {
exc.printStackTrace();
throw new ServletException(exc);
}
}
}
I got the solution. The reason for time delay was due to 'AutoCommit' set to 'true' by default. So I set 'myConn.setAutoCommit(false);' before loop and then run the code. It took about 8 seconds to update db for 2000 records which was about 2 minutes earlier. Below is the code for reference -
package com.company.testdata;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class UpdateDataCopy {
public static void main(String[] args) throws Exception {
String user = "root";
String pass = "test";
String jdbcUrl = "jdbc:mysql://192.1.2.1/db_bro_mumbai?useSSL=false";
String driver = "com.mysql.jdbc.Driver";
try {
PreparedStatement pstm = null;
Class.forName(driver);
Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
FileInputStream input = new FileInputStream("E:\\Work\\TestData.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(input);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row;
myConn.setAutoCommit(false);
/*final int batchSize = 1000;
int count = 0;*/
long start = System.currentTimeMillis();
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
row = (XSSFRow) sheet.getRow(i);
int id = (int)row.getCell(0).getNumericCellValue();
System.out.println(id);
String firstname = row.getCell(2).getStringCellValue();
System.out.println(firstname);
String middlename = row.getCell(3).getStringCellValue();
System.out.println(middlename);
String lastname = row.getCell(4).getStringCellValue();
System.out.println(lastname);
int physicalFitness = (int)row.getCell(25).getNumericCellValue();
System.out.println(physicalFitness);
String sql = "UPDATE fitness_details as p SET p.physicalFitness = ? "
+ " WHERE CandidateID_FK1 = ?";
pstm = (PreparedStatement) myConn.prepareStatement(sql);
pstm.setInt(1, physicalFitness);
pstm.setInt(2, id);
pstm.addBatch();
pstm.executeBatch();
System.out.println("Import rows " + i);
}
System.out.println("Time Taken="+(System.currentTimeMillis()-start));
myConn.commit();
myConn.setAutoCommit(true);
pstm.close();
myConn.close();
input.close();
}
catch (Exception exc) {
exc.printStackTrace();
throw new ServletException(exc);
}
}
}

how do i use select statment

I want to use select max from a table. I want to use a PreparedStatement. I have a composite primary key which consists of the t.v series and the epo number. When I add new epo it will for table and bring the t.v series code from guidline table the content of all the programs and the code for each and then add to the new table. I want it to get the last epo by getting the max and then increment +1 "an automation app".
So how can I select max where id =??
If you get me its like
pstm2=con.prepareStatement(max);
String max="select MAX(epono) as eponoo from archieve wwhere id like ? ";
This program would be helpful
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SelectRecordsUsingPreparedStatement {
public static Connection getConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:#localhost:1521:databaseName";
String username = "name";
String password = "password";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void main(String[] args) {
ResultSet rs = null;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
String query = "select deptno, deptname, deptloc from dept where deptno > ?";
pstmt = conn.prepareStatement(query); // create a statement
pstmt.setInt(1, 1001); // set input parameter
rs = pstmt.executeQuery();
// extract data from the ResultSet
while (rs.next()) {
int dbDeptNumber = rs.getInt(1);
String dbDeptName = rs.getString(2);
String dbDeptLocation = rs.getString(3);
System.out.println(dbDeptNumber + "\t" + dbDeptName + "\t" + dbDeptLocation);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

Using JAVA API update Rally Team membership

My Java API compares Team members from another application with Rally. The compared results is updated in Rally. It takes the reference of Project name and Res name.
The code throws "java.lang.IndexOutOfBoundsException: Index: 0, Size: 0" error. I coudn't spot the error. Could some one help? Following is the code and the output
package teammembership;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.UpdateResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.io.*;
import java.sql.*;
import javax.xml.parsers.DocumentBuilder;
import org.apache.soap.util.xml.*;
import org.w3c.dom.*;
//import org.json.*;
//import static projectteammembers.JsonUtil.getJsonValue;
public class TeamMembership {
public static Connection makeConnection(String propertiesFile) throws SQLException, Exception {
Connection conn = null;
DocumentBuilder docBuilder = XMLParserUtils.getXMLDocBuilder();
Document doc = docBuilder.parse(new File(propertiesFile));
// Retrieve database parameters
Element database = (Element) doc.getElementsByTagName("database").item(0);
String url = database.getAttribute("url");
String serviceId = database.getAttribute("serviceId");
String username = database.getAttribute("username");
String password = database.getAttribute("password");
String host = url.substring(url.indexOf("//"), url.indexOf(";"));
String connectString = "jdbc:oracle:thin:#" + host + "/" + serviceId;
// Load JDBC Driver
String driverClass = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverClass);
try {
conn = DriverManager.getConnection(connectString, username, password);
} catch (SQLException ex) {
throw new SQLException(ex);
} catch (Exception ex) {
throw new Exception(ex);
}
return conn;
}
public static void main(String[] args) throws URISyntaxException, IOException, SQLException {
String host = "https://rally1.rallydev.com";
String username = "username#abc.com";
String password = "password";
//String userRef = "";
String applicationName = "update team membership";
//int queryLimit = 4000;
Connection conn = null;
String propertiesFile = "";
propertiesFile = "c:/app/c/properties_prod.xml";
String projid = "";
String resid = "";
//String returnValue = "";
String selectString = "";
RallyRestApi restApi = new RallyRestApi(
new URI(host),
username,
password);
restApi.setApplicationName(applicationName);
System.out.println(restApi.getWsapiVersion());
try {
conn = makeConnection(propertiesFile);
// Select compared records of Team member present in table1 not in table2
selectString += "select Prj_name ";
selectString += ",res_name";
selectString += " from CUST_table1_v c ";
selectString += " WHERE NOT EXISTS( select 1 from CUST_table2_v r";
selectString += " where c.prj_name = r.Prj_name and c.res_name = r.res_name)";
// Create select statement
Statement st = (Statement) conn.createStatement();
// Execute select statement
ResultSet rs = st.executeQuery(selectString);
while (rs.next()) {
projid = rs.getString("Prj_name");
resid = "(" + rs.getString("res_name") + ")";
System.out.println(projid);
System.out.println(resid);
QueryRequest projectRequest = new QueryRequest("Project");
projectRequest.setFetch(new Fetch("Name"));
projectRequest.setQueryFilter(new QueryFilter("Name", "=", projid));
QueryResponse projectQueryResponse = restApi.query(projectRequest);
JsonObject projectObj = projectQueryResponse.getResults().get(0).getAsJsonObject();
QueryRequest userRequest = new QueryRequest("User");
userRequest.setFetch(new Fetch("UserPermissions", "TeamMemberships"));
userRequest.setQueryFilter(new QueryFilter("DisplayName", "contains", resid));
QueryResponse userQueryResponse = restApi.query(userRequest);
System.out.println(userQueryResponse);
JsonObject userObject = userQueryResponse.getResults().get(0).getAsJsonObject();
//JsonObject projectObj = new JsonObject(projid);
String userRef = userObject.get("_ref").toString();
System.out.println("Found User with Ref: " + userRef);
JsonArray existTeamMemberships = (JsonArray) userQueryResponse.getResults().get(0).getAsJsonObject().get("TeamMemberships");
// add or remove projects for user
existTeamMemberships.add(projectObj);
// Setup update fields/values for Team Membership
JsonObject updateUserTeamMembershipObj = new JsonObject();
updateUserTeamMembershipObj.add("TeamMemberships", existTeamMemberships);
UpdateRequest updateTeamMembershipsRequest = new UpdateRequest(userRef, updateUserTeamMembershipObj);
UpdateResponse updateTeamMembershipResponse = restApi.update(updateTeamMembershipsRequest);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
restApi.close();
conn.close();
}
}
}
Following is the error out put
v2.0
DT-E2E Automation
(tmanjunath)
com.rallydev.rest.response.QueryResponse#193d23b
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at com.google.gson.JsonArray.get(JsonArray.java:92)
at teammembership.TeamMembership.main(TeamMembership.java:125)
BUILD SUCCESSFUL (total time: 11 seconds)
You have a List (an ArrayList to be exact) which contains nothing (no single object) and you try to access the first object (which doesn't exist). That's what the error tells you. You try to access index 0 (the first position in the list) but there is no element in it (so the size is 0). It happens around line 125. Since your formatting in the question doesn't seem to be correct, I can only guess which line in your question is line 125 (and I don't want to read 125 lines of code by the way). So I think the exception occurs here:
JsonObject userObject = userQueryResponse.getResults().get(0).getAsJsonObject();
Try to track it down. Make sure the list returned from userQueryResponse.getResults() contains something:
list = userQueryResponse.getResults();
System.out.println(list.size());
If not, that's your problem. If you cannot solve it, ask a specific question about this problem without posting 150 line of code.
get("_ref").toString() used to work with the older versions, and the code you refer to is older. When using 2.0.4 jar (which by default uses the most recent version of WS API in production, v2.0) replace all instances of it with get("_ref").getAsString().
For example,
String userRef = userObject.get("_ref").toString();
will generate java.lang.NullPointerException: key == null