The purpose of this project is to check the redirections of a URL using curl command and then print the output. If the output is same as the provided URL (i.e. demo URL), then the program should give true value. The links to be checked are in the database table called "store" and the column containing the link values is called "link." I tried taking the link values from the database and adding it using the curl command but I get an internal error. It seems like the values are not being processed properly. I am also having difficulty with printing the output and checking to see if output is same as the "demo" URL. Any help would be greatly appreciated.
public static void main(String args[]) {
String link = null;
String command1 = null;
try
{
String myDriver = "com.mysql.cj.jdbc.Driver";
String myUrl = "myurl";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "username", "password");
String query = "SELECT * from store;";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
{
link = rs.getString("link");
demolink = rs.getString("demo link");
System.out.format("%s\n", link);
}
st.close();
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
//USING CURL COMMAND
String host="host";
String user="username";
String password="password";
System.out.println("generating CURL request");
String command1="curl -v -L "+link+"";
try{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
}catch(Exception e){
e.printStackTrace();
}