Get Primary Key Column From A Table

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

public class Main {

  public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar, PRIMARY KEY (id) );");
    st.executeUpdate("insert into survey (id,name ) values (1,‘nameValue‘)");

    ResultSet rs = null;
    DatabaseMetaData meta = conn.getMetaData();
    // The Oracle database stores its table names as Upper-Case,
    // if you pass a table name in lowercase characters, it will not work.
    // MySQL database does not care if table name is uppercase/lowercase.
    //
    rs = meta.getPrimaryKeys(null, null, "survey");

    java.util.List list = new java.util.ArrayList();
    while (rs.next()) {
      String columnName = rs.getString("COLUMN_NAME");
      System.out.println("getPrimaryKeys(): columnName=" + columnName);
    }

    st.close();
    conn.close();
  }

  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }

  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/octopus";
    String username = "root";
    String password = "root";

    Class.forName(driver); // load MySQL driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";

    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

}
时间: 2024-10-09 23:03:05

Get Primary Key Column From A Table的相关文章

Oracle Java JDBC: Get Primary Key Of Inserted Record

Here is a small write-up which should help those who still write plain Java JDBC code. I know we have some wonderful persistence frameworks like Hibernate that make ones life comfortable but the reality is we still have to deal with plain old JDBC ap

Database Primary key and Foreign key [From Internet]

Database Primary key and Foreign key --Create Referenced Table CREATE TABLE Department ( DeptID int PRIMARY KEY, --define primary key Name varchar (50) NOT NULL, Address varchar(100) NULL ) --Create Referencing Table CREATE TABLE Employee ( EmpID int

postgresql----数据库表约束----PRIMARY KEY

五.PRIMARY KEY ---- 主键约束 主键可以是单个字段,也可以是多个字段的组合.主键约束其实是UNIQUE和NOT NULL约束的组合,即主键必须是唯一,且各字段都是NOT NULL的. 1.创建测试表 create table tbl_primary( a int not null, b int, c int, constraint pk_tbl_primary_a_b primary key (a,b) ); 其中(a,b)是组合主键,即a和b的组合必须是唯一的,且a是not n

Create Primary Key using Entity Framework Code First

原文:http://www.codeproject.com/Articles/813912/Create-Primary-Key-using-Entity-Framework-Code-Fir Introduction This article describes the effect of Entity Framework Code First convention and configuration for creating Primary Key column. Entity Framew

SQL PRIMARY KEY 约束\SQL FOREIGN KEY 约束\SQL CHECK 约束

SQL PRIMARY KEY 约束 PRIMARY KEY 约束唯一标识数据库表中的每条记录. 主键必须包含唯一的值. 主键列不能包含 NULL 值. 每个表都应该有一个主键,并且每个表只能有一个主键. SQL PRIMARY KEY Constraint on CREATE TABLE 下面的 SQL 在 "Persons" 表创建时在 "Id_P" 列创建 PRIMARY KEY 约束: MySQL: CREATE TABLE Persons ( Id_P i

SQL PRIMARY KEY 约束

SQL PRIMARY KEY 约束 PRIMARY KEY 约束唯一标识数据库表中的每条记录. 主键必须包含唯一的值. 主键列不能包含 NULL 值. 每个表都应该有一个主键,并且每个表只能有一个主键. 1.ALTER TABLE Persons ADD PRIMARY KEY (P_Id) 2.ALTER TABLE Persons ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName) 3.ALTER TABLE Persons DRO

1503 - A PRIMARY KEY must include all columns in the table's partitioning function

1503 - A PRIMARY KEY must include all columns in the table's partitioning function 错误的原因:表的主键字段必须包含分区字段.为什么? 举例来说,Id为auto_increment primary key,按日期分区.考虑下面的场景,插入一条Id为100的记录,mysql根据日期,当然知道插入到那个分区中,但是要检查所有的分区中是否已经包含Id为100的记录,显然效率很低.如果不检查所有的分区,只检查当前插入的分区

ERROR 1105 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function

ERROR 1105 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function CREATE TABLE `sample` (  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',  `trade_no` varchar(32) NOT NULL COMMENT 'xxx',  `trade_type` varchar(32) D

mysql update 报 You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode

UPDATE i18nresource SET languageId = 'en-us' Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.