So I try to start a Java program on a raspberryPi via jsch from my visual studio code on my pc. The program connects to a mariadb database and does a simple Select statement.
If I'm logged in on the raspberryPi, via putty, the connection between the java program and mariadb works fine. If I try to execute the program via another java program with jsch, the program will start but won't execute the line connection = verbindung_datenbank.getConnection(dbURL, user, password);
I placed some System.out.println();
lines to find the line where the program stops working. Befor the line it worked, after that it didn't.
What I don't understand is, why does the program work if I'm logged in over putty but won't work if a program is loged in via SSH?
Here the full code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;
import java.sql.*;
public class connection_db {
public static Connection getConnection(String dbURL, String user, String password) throws SQLException {
Connection connection = DriverManager.getConnection(dbURL, user, password);
connection.setAutoCommit(true);
return connection;
}
public static void main(String[] args) {
Connection connection = null;
Integer aId=null;
String dbURL = "jdbc:mariadb://192.168.178.39:3306/temperature";
String user = "pi";
String password = "test";
try {
connection = verbindung_datenbank.getConnection(dbURL, user, password);
String query = "SELECT MAX(id) FROM table1" ;
System.out.println(query);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
Integer id = rs.getInt("MAX(id)");
System.out.println(id);
aId = id;
}
double temperature = -15;
String query2 = "UPDATE table1 SET theke_1 = "+temperature +" WHERE id = "+ aId;
System.out.println(query2);
Statement stmt2 = connection.createStatement();
stmt2.executeQuery(query2);
stmt2.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// connection.close();
}
}
}
Hopefully someone can help me.
Edit: the code from the jsch program:
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class main_slave {
public static void main(String[] args) throws JSchException, InterruptedException, IOException{
String host = "192.168.178.46";
String user = "pi";
String password = "password";
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setUserInfo(new main_slaveUserInfo(user, password));
session.connect();
Channel channel = session.openChannel("exec");
InputStream in = channel.getInputStream();
((ChannelExec) channel).setCommand("java
connection_db");
channel.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
channel.disconnect();
session.disconnect();
}
}```