如果像数据库中存储空值“”或者null,那么他存入数据库中以后会以什么形式存储,如果你想从数据库中取出这条值,那么他的整值又会以什么形式展现。关于这个疑问是在我做一个项目的时候,我像数据库中存入“”值,但是取出这条记录的时候再作判断,然后输出。但是就是这个过程浪费了将近一下午的时间,原因是太想当然了。所以总结了一下,结论大概是这样的:
1.在mysql数据库中:如果向mysql中存入null值,那么在数据库中还是null值,打印的结果还是null值;如果向mysql中存入“”,那么存入数据库的还是null,打印的结果还是null。
下面会给出测试代码和测试结果。
2.在access数据库中:如果向access数据库中存入null值,那么数据库中还是null值,打印出的结果还是null值;如果向access中存入“”,那么存入数据库是“”字符串对象,大一的结果是空。
mysql中测试代码:
存入null值:
package mysql; import java.sql.*; public class Client1 { public static void main(String[] args) { final String DBDRIVER = "com.mysql.jdbc.Driver"; final String DBURL = "jdbc:mysql://localhost:3306/test"; final String USER = "root"; final String PASS = "wangkang"; PreparedStatement pstmt = null; Connection conn = null; try { Class.forName(DBDRIVER); conn = DriverManager.getConnection(DBURL, USER, PASS); String sql = "insert into test(str) values(str=?)"; pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); Statement stmt = conn.createStatement(); pstmt.setString(1, null); pstmt.executeUpdate(); ResultSet rs = pstmt.getGeneratedKeys(); rs.next(); int key = rs.getInt(1); stmt.close(); rs.close(); pstmt.close(); String sql1 = "select * from test where id=?"; pstmt = conn.prepareStatement(sql1); pstmt.setInt(1, key); rs = pstmt.executeQuery(); rs.next(); String str = rs.getString("str"); // 判断当存储null值时,从数据库取出了什么 if (str == null) { System.out.println("打印信息:" + "null"); } else if (str == "") { System.out.println("打印信息:" + "/"/""); } else if (str.equals("")) { System.out.println("打印信息:对象" + str); } else if (str.equals(null)) { System.out.println("打印信息:对象" + str); } else { System.out.println("other信息" + str); } rs.close(); pstmt.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }
打印的结果:
打印信息:null
存入“”值:
package mysql; import java.sql.*; public class Client2 { public static void main(String[] args) { final String DBDRIVER = "com.mysql.jdbc.Driver"; final String DBURL = "jdbc:mysql://localhost:3306/test"; final String USER = "root"; final String PASS = "wangkang"; PreparedStatement pstmt = null; Connection conn = null; try { Class.forName(DBDRIVER); conn = DriverManager.getConnection(DBURL, USER, PASS); String sql = "insert into test(str) values(str=?)"; pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); Statement stmt = conn.createStatement(); pstmt.setString(1, ""); pstmt.executeUpdate(); ResultSet rs = pstmt.getGeneratedKeys(); rs.next(); int key = rs.getInt(1); stmt.close(); rs.close(); pstmt.close(); String sql1 = "select * from test where id=?"; pstmt = conn.prepareStatement(sql1); pstmt.setInt(1, key); rs = pstmt.executeQuery(); rs.next(); String str = rs.getString("str"); //判断当存储null值时,从数据库取出了什么 if(str == null) { System.out.println("打印信息:" + "null"); } else if(str == "") { System.out.println("打印信息:" + "/"/""); } else if (str.equals("")) { System.out.println("打印信息:对象" + str); } else if (str.equals(null)) { System.out.println("打印信息:对象" + str); }else { System.out.println("other信息:" + str); } rs.close(); pstmt.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }
打印结果:
打印信息:null
access中测试代码:
存入null值:
package access; import java.sql.*; class Client1 { public static void main(String[] args) { String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D://My Project//access//test1.mdb"; Connection conn = null; int id = 7; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection(url, "", ""); String sql = "insert into test(id, str) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setString(2, null); pstmt.executeUpdate(); pstmt.close(); String sql1 = "select * from test where id=?"; pstmt = conn.prepareStatement(sql1); pstmt.setInt(1, id); ResultSet rs = pstmt.executeQuery(); rs.next(); String str = rs.getString("str"); // 判断当存储null值时,从数据库取出了什么 if (str == null) { System.out.println("打印信息:" + "null"); } else if (str == "") { System.out.println("打印信息:" + "/"/""); } else if (str.equals("")) { System.out.println("打印信息:对象" + str); } else if (str.equals(null)) { System.out.println("打印信息:对象" + str); } else { System.out.println("other信息" + str); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }
打印结果:
打印信息:null
存入“”值:
package access; import java.sql.*; class Client2 { public static void main(String[] args) { String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D://My Project//access//test1.mdb"; Connection conn = null; int id = 8; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection(url, "", ""); String sql = "insert into test(id, str) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setString(2, ""); pstmt.executeUpdate(); pstmt.close(); String sql1 = "select * from test where id=?"; pstmt = conn.prepareStatement(sql1); pstmt.setInt(1, id); ResultSet rs = pstmt.executeQuery(); rs.next(); String str = rs.getString("str"); // 判断当存储null值时,从数据库取出了什么 if (str == null) { System.out.println("打印信息:" + "null"); } else if (str == "") { System.out.println("打印信息:" + "/"/""); } else if (str.equals("")) { System.out.println("打印信息:对象" + "/"/""); } else if (str.equals(null)) { System.out.println("打印信息:对象" + "null"); } else { System.out.println("other信息" + str); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }
打印结果:
打印信息:对象""