public static void insert (String signal)
{ System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/activation","root", "piisimba");
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
try
{
String query = "INSERT INTO `test1`( `Time`, `Signal`) VALUES (now(), 'hallosignal');";
// create the mysql insert preparedstatement
PreparedStatement preparedStmt2 = connection.prepareStatement(query);
// execute the preparedstatement
preparedStmt2.executeUpdate(query);
connection.close();
}catch (Exception e)
{
System.out.println("exception caught");
}
}
catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
catch (Exception e)
{
System.out.println("exception caught");
}
}
public void run() {
String s ="";
try {
int c = 0;
while( ( c = System.in.read() ) > -1 ) {
this.out.write( c );
s=s+c;
}
System.out.println("s = " + s);
insert(s);
}
catch( IOException e ) {
e.printStackTrace();
}
} }
我正在使用上面的run()方法的SerialPort。这个方法向端口发送一个特定的信号,我需要一种方法来读取这个信号并将其插入一个mysql数据库。这就是为什么我创建了一个方法插入,它创建了与数据库的连接,并将此信号字符串作为输入并插入它。这种方法可以单独使用。但是,当我在我的run方法中调用它时,数据库没有发生任何变化。任何人都可以帮我解决这个问题。