2

This code hasn't error or exception, but after running the table features in the data base remain empty

try {
    Class.forName("com.mysql.jdbc.Driver");
    con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/sounds","root","");
    statment = (Statement) con.createStatement();
    String query = "INSERT INTO featuers (avg_Bandwidth) values (?)";   
    PreparedStatement preparedStmt = (PreparedStatement)con.prepareStatement(query);
    preparedStmt.setDouble(1, avarage_Bandwidth);

    preparedStmt.executeUpdate();

    con.close();
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
7
  • 1
    I think you also have a typo in your "INSERT" statement. Shouldn't there be a "features" instead of "featuers "? Commented Apr 16, 2015 at 7:24
  • @BranislavLazic but then it should throw SqlException !! Commented Apr 16, 2015 at 7:25
  • if there is no typo , Try using Connection#commit() method Commented Apr 16, 2015 at 7:26
  • 1
    @NeerajJain Newly created connections (at least through DriverManager) are required (by the JDBC spec) to be autoCommit=true. Commented Apr 16, 2015 at 7:28
  • 2
    One small tip too: Close connection and statements in finally block. Commented Apr 16, 2015 at 7:31

2 Answers 2

1

Two things of note :

  1. statment = (Statement) con.createStatement();
    This variable is not used if I see it correctly. But this should not be the problem in itself
  2. preparedStmt.executeUpdate();
    This executeUpdate method should return a int based on the query itself. So it should return 1 in your case for the no of rows affected. So try to check what is returned.

    This may not be the exact solutions but do try these and you may get some idea on what is happening
Sign up to request clarification or add additional context in comments.

1 Comment

it doesn't enter to the try scope in any case :(
1

Hi try to use this one.

int status = 0; // to get the status to know if statement is executed properly or not

try {
    Connection con = DBConnection.getConnect();//Your database Connection
    Statement st = con.createStatement();
    status = st.executeUpdate(query); //After executing this it will return an int value
} catch (Exception ex) {
    ex.printStackTrace();
}

I hope this will help you.

5 Comments

it doesn't enter to the try scope in any case :(
two questions 1. is it printing any exception 2. or please post more code because it have to reach to try block to execute command, if it is not coming here it means there is something wrong with your code.
I use threading principle public void run(){ double avarage_Bandwidth=0.1; //the previous code }
@user3800610 sorry but i'm not able to understand your issue properly using your last comment
I mean: the previous code was written in the post, existing into function called "run()" this function works as thread and it reject "throw exception" in its header are you understanding? :$ I think the problem from the threading principle :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.