我正在编写一个简单的程序来连接到本地托管的 MySQL 服务器。我已确保服务器正在运行。 我尝试执行的代码如下:
void CSlaveController::fnDatabaseConnection(){
sql::Driver *driver;
sql::Connection *connection;
sql::ResultSet *result;
sql::Statement *statement;
sql::PreparedStatement *prepared;
driver = get_driver_instance();
connection = driver->connect("tcp://127.0.0.1:3306","DBServer","root"); /*Where the program crashes*/
statement = connection->createStatement();
statement->execute("SHOW DATABASES");
delete statement;
delete connection;
}
我注意到的问题之一是当我运行程序时找不到头文件mysql_connection.h。当我构建项目时,没有出现任何问题。 但是当我运行程序时,它崩溃了,这就是我所看到的:。我已经仔细检查了包含内容,甚至手动添加了该特定文件。
关于我可能做错了什么有什么想法吗?如果图像太多,我很抱歉,我觉得这将是传达最多信息的最佳方式。
@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 160000) // upload file's size up to 16MB /
// 16000000 byte
public class FileUploadToDataBaseServlet extends HttpServlet {
/*
* FileUploadToDataBaseServlet: files upload to database with Servlet, JSP
* and MySQL. Creating MySQL database table. Coding upload form page. Coding
* file upload servlet. Coding message page. Testing the application and
* verifying file stored in database.
*/
private String databaseURL = "jdbc:mysql://localhost:3306/MyDatabase";
private String databaseUser = "root";
private String databasePassWord = "2888";
/*
* This method is called by servlet service method to handle the POST
* request from client. The HTTP POST method allows the client to send data
* of unlimited length to the Web server a single time and is useful when
* posting information to the server.
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// gets values of text fields
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String country = request.getParameter("country");
String photo = request.getParameter("photo");
// InputStream's are used for reading byte based data
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
Part filePart = request.getPart("photo");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
Connection connection = null; // connection to the database
String message = null; // message will be sent back to client
try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
connection = DriverManager.getConnection(databaseURL, databaseUser, databasePassWord);
// constructs SQL statement
String sql = "INSERT INTO registration (first_name, last_name, phone, email, country, photo )"
+ "values (?, ?, ?, ?, ?, ?)";
// The performance of the application will be faster if you use
// PreparedStatement,
// which gives better performance than Statement class.
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, firstName);
statement.setString(2, lastName);
statement.setString(3, phone);
statement.setString(4, email);
statement.setString(5, country);
if (inputStream != null) {
// bring input stream of the upload file for the blob column
statement.setBlob(6, inputStream);
}
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "File uploaded and saved into database";
}
} catch (SQLException exception) {
message = "ERROR: " + exception.getMessage();
exception.printStackTrace();
} finally {
if (connection != null) {
// closes the database connection
try {
connection.close();
} catch (SQLException exception) {
exception.printStackTrace();
}
}
// sets the message in request scope
request.setAttribute("Message", message);
// forwards message to the page
getServletContext().getRequestDispatcher("/Alert.html").forward(request, response);
}
}
}