在按下jsp中的按钮后,如何在java中运行main方法?
我是新来的。我需要制作一个jsp来上传一个CSV文件,并将其中的数据发送到mysql数据库。我用Java编写了一段代码来完成后一部分,但不确定如何将其连接到jsp,因此当用户按下uploads时,Java代码将运行 我的java代码:在按下jsp中的按钮后,如何在java中运行main方法?,java,mysql,jsp,Java,Mysql,Jsp,我是新来的。我需要制作一个jsp来上传一个CSV文件,并将其中的数据发送到mysql数据库。我用Java编写了一段代码来完成后一部分,但不确定如何将其连接到jsp,因此当用户按下uploads时,Java代码将运行 我的java代码: import java.io.*; import java.sql.*; public class dbInserter { public static void main(String[] args) { String jdbcURL
import java.io.*;
import java.sql.*;
public class dbInserter {
public static void main(String[] args) {
String jdbcURL = "jdbc:mysql://localhost:3306/testDb";
String username = "root";
String password = "1234";
String csvFilePath = "C:/Users/Name/Desktop/Attendance.csv";
int batchSize = 20;
Connection connection = null;
try {
connection = DriverManager.getConnection(jdbcURL, username, password);
connection.setAutoCommit(false);
String sql = "INSERT INTO csv (name, `in-time`, `out-time`) VALUES (?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
BufferedReader lineReader = new BufferedReader(new FileReader(csvFilePath));
String lineText = null;
int count = 0;
lineReader.readLine(); // skip header line
while ((lineText = lineReader.readLine()) != null) {
String[] data = lineText.split(",");
String name = data[0];
String inTime = data[1];
String outTime = data[2];
statement.setString(1, name);
statement.setString(2, inTime);
statement.setString(3, outTime);
statement.addBatch();
if (count % batchSize == 0) {
statement.executeBatch();
}
}
lineReader.close();
// execute the remaining queries
statement.executeBatch();
connection.commit();
connection.close();
} catch (IOException ex) {
System.err.println(ex);
} catch (SQLException ex) {
ex.printStackTrace();
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
我尝试使用servlet,但由于我使用它的经验很少,所以遇到了很多错误。如果有人能帮我,我将不胜感激。这应该对你有帮助-这应该对你有帮助-