TBrauwers TBrauwers - 2 months ago 11
Java Question

JDBC statement is not executed (No errors)

im working on a little Minecraft Plugin, im trying to insert data into the database with my DatabaseManager class but for some reason its not working. There are no error messages and the password and user name are correct. Also ive tried to insert the sql code manualy into the database and it works so there are no sql syntax errors. For me it seems like the statement is not executed for some reason but I cant find the failure. Below is the code of my class.



import java.sql.*;


/**
*
* @author timob
*/
public class DatabaseManager {
Connection conn = null;
Statement stmt = null ;
ResultSet result = null;

String user = "root" ;
String pass = "";

public ResultSet executeQuery(String query) throws SQLException{
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/metrocraft",user,pass);
stmt = conn.createStatement();
result = stmt.executeQuery(query);

}catch (Exception ex){
ex.printStackTrace();
}finally{
if(result != null)
result.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
}
return result ;
}
//Metrocraft relevant code

public void addZone(double p1X , double p1Y,double p2X,double p2Y) throws SQLException{
String sql = "INSERT INTO `zones`( `p1X`, `p1Y`,`p2X`, `p2Y`) VALUES (" + p1X + ","+ p1Y+","+p2X+","+p2Y+"); ";
System.out.print(sql);
executeQuery(sql);
}
}




Answer

Use executeUpdate for database write operations

PreparedStatement stmt = conn.prepareStatement(query);
result = stmt.executeUpdate();