I am making an application that returns the tweets of the specific query and moves it to the database as well as excel file. I am not able to use the twitter 4j jar file. i have added it to the build path but it is not working. I have tried if the apache poi jar files are working and that is good. it is reading data from the excel. There is some issue with the twitter 4j jar files and version.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.apache.poi.ss.usermodel.*" %>
<%@ page import="org.apache.poi.xssf.usermodel.XSSFWorkbook" %>
<%@ page import=" org.apache.poi.xssf.usermodel.XSSFSheet"%>
<%@ page import="twitter4j.conf.ConfigurationBuilder" %>
<%@ page import="twitter4j.TwitterException" %>
<%@ page import="twitter4j.TwitterFactory" %>
<%@ page import="twitter4j.Twitter" %>
<%@ page import="twitter4j.Status" %>
<%@ page import="twitter4j.Query" %>
<%@ page import="twitter4j.QueryResult" %>
<%@ page import="java.io.FileOutputStream" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Twitter Search</title>
</head>
<body>
<h1>Search tweets </h1>
<form action="search.jsp" method="post">
<input type="text" name="query" placeholder="Enter your search query">
<input type="submit" value="Search">
</form>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%
// Retrieve the search query parameter
String query = request.getParameter("query");
System.out.println("Query: " + query);
// Store the search query in the database
String url = "jdbc:mysql://localhost:3306/mysql";
String username = "siyanarang";
String password = "Siyanarang@31";
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO SearchQueries (query) VALUES (?)";
statement = connection.prepareStatement(sql);
statement.setString(1, query);
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
//Display search results here
// Twitter4j code to retrieve tweets
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("**")
.setOAuthConsumerSecret("**")
.setOAuthAccessToken("**")
.setOAuthAccessTokenSecret("**");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
/* String query = request.getParameter("query"); */
Query twitterQuery = new Query(query);
QueryResult result = twitter.search(twitterQuery);
// Create Excel workbook and sheet
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("tweets");
int rowNum = 0;
for (Status status : result.getTweets()) {
Row row = sheet.createRow(rowNum++);
Cell cell = row.createCell(0);
cell.setCellValue(status.getText());
}
// Save the workbook to a file
String filePath = "/Users/siyanarang/Desktop/tweets.xlsx";
FileOutputStream fileOut = new FileOutputStream(filePath);
workbook.write(fileOut);
fileOut.close();
out.println("Tweets saved to Excel file successfully!");
%>
</body>
</html>
I tried downloading twitter 4j jar files from a lot of sites but its not working.I want to know if my code is correct and from where I can get the jar file as on the official site there is a zip file with no jar files.