Question:
how can I insert values in a table in MySQL in java code? the value may be null.?
sweetbrier_t
2010-03-13 01:19:40 UTC
how can I insert null values in a table in MySQL. I am sure that Allow Null is checked true while I am creating the table

the code is:
Statement stmt = dbconnection.createStatement();
Statement stmt2 = dbconnection.createStatement();
ResultSet resultSet = stmt.executeQuery("SELECT * FROM table1 a");
Long var2=null;
while (resultSet.next()) {
stmt2.execute("insert into table2 value (null,'"+ resultSet.getLong("var2")+ ");");
}

the error is : Data truncation: Out of range value adjusted for column 'var2'
Three answers:
AnalProgrammer
2010-03-13 01:31:21 UTC
Use column names for both the select and the insert.



Have fun.
Nigel
2010-03-14 07:46:58 UTC
It would be better to use bind variables, this allows the database to re-use statements when it can ( or you can use prepared statements )

An example of using nulls and prepared statements is at http://www.java2s.com/Code/Java/Database-SQL-JDBC/DemoPreparedStatementSetNullforintvaluecolumn.htm
?
2016-11-29 00:39:51 UTC
For eg: $call = $_post['call']; $handle = $_post['handle']; $telephone = $_post['telephone']; $effect= mysql_query("insert into table_name (identity,call,handle,telephone) values (' ', '$call', '$handle', '$telephone').mysql_error()"); if($effect) { echo "325c0ded576131ea255ad2bd38bfb2alert('effectively inserted'); } else { echo "did no longer insert"; }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...