If a table column can have a null value or not? : Column « Database SQL JDBC « Java






If a table column can have a null value or not?

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class Main {
  private static final String DRIVER = "com.mysql.jdbc.Driver";

  private static final String URL = "jdbc:mysql://localhost/testdb";

  private static final String USERNAME = "root";

  private static final String PASSWORD = "";

  public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users");
    ResultSetMetaData metadata = resultSet.getMetaData();
    int nullability = metadata.isNullable(1);

    if (nullability == ResultSetMetaData.columnNullable) {
      System.out.println("Columns ID can have a null value");
    } else if (nullability == ResultSetMetaData.columnNoNulls) {
      System.out.println("Columns ID does not allowed to have a null value");
    } else if (nullability == ResultSetMetaData.columnNullableUnknown) {
      System.out.println("Nullability unknown");
    }
  }
}
 

   
    
  








Related examples in the same category

1.Get Column Corresponding Class Name
2.Get Column Detail Information
3.Get Column Display Size.zip
4.Get Column Label
5.Get Column Name
6.Use DatabaseMetaData to get table column names
7.Get Column Name And Type For A Table
8.Get Column Name From ResultSet Metadata
9.Get Column Number Of Digits To Right Of The Decimal Point
10.Get Column Number Of Presions Number Of Decimal Digits
11.Get Column ORDINAL POSITION
12.Get Column Privileges
13.Get Column Size
14.Get Column Sql Data Type
15.Get Column SQL Type Name
16.Get Column Type
17.Get Table Optimal Set Of Columns
18.Is Column A Cash Value
19.Is Column Auto Increase
20.Is Column Case Sensitive
21.Is Column Definitely Writable
22.Is Column Nullable
23.Is Column Nullable From ResultSet Metadata
24.Is Column Readonly
25.Is Column Searchable
26.Is Column Signed Number
27.Is Column Writable
28.Make Unique Column in Database Table
29.Remove Unique Column in Database Table
30.Arrange a Column of Database Table
31.Change Column Name of a Table
32.Sum of Column in a Database Table
33.Delete a Column from a Database Table
34.Adding a New Column Name in Database Table
35.Designated column's table name
36.If a table column value is auto-increment?
37.Get Column Names From ResultSet for MySQL