FROM information_schema.tables
WHERE tables.table_schema = 'public'
AND tables.table_name != 'schema_version'
AND tables.table_type = 'BASE TABLE';
SELECT columns.table_name,
columns.column_name,
columns.data_type,
columns.column_default,
columns.is_nullable
FROM information_schema.columns;
SELECT kcu.constraint_name,
kcu.table_name,
kcu.column_name
FROM information_schema.key_column_usage kcu
LEFT JOIN information_schema.table_constraints tc ON tc.constraint_name = kcu.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY';
SELECT constraint_name, table_name, column_name
FROM information_schema.constraint_column_usage;
CREATE TABLE ANOTHERTHING (ID NUMBER(10) NOT NULL, TEXT VARCHAR, PRIMARY KEY (ID));
alter table anotherthing rename to anotherthing_1;
create table anotherthing (id number(10) not null, sometext varchar, primary key (id));
insert into anotherthing(id, sometext) select id, text from anotherthing_1;
drop table anotherthing_1;
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.denevell.tomcat.entities.write.AnotherThing</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:/var/lib/tomcat7/dbs/test.db" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
</persistence>
package org.denevell.tomcat.entities.write; // Referenced in persistence.xml
@Entity
public class AnotherThing {
@Id @GeneratedValue
private int id;
private String text;
public AnotherThing() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
// Setup the entity manager
EntityManagerFactory factory = Persistence.createEntityManagerFactory("example");
EntityManager em = factory.createEntityManager();
// Create it
AnotherThing t = new AnotherThing();
t.setText("Heya");
// Add it
EntityTransaction trans = em.getTransaction();
trans.begin();
em.persist(t);
trans.commit();
// Fetch them
TypedQuery<AnotherThing> q = em.createQuery("select ting from AnotherThing ting", AnotherThing.class);
List<AnotherThing> results = q.getResultList();
for (AnotherThing thing : results) {
writer.println(thing.getId() + ": " + thing.getText());
}
// Close the entity manager
em.close();
factory.close();
java jpql jpa tomcat sql sqlite
<Context>
<Resource name="jdbc/sqlite"
type="javax.sql.DataSource"
driverClassName="org.sqlite.JDBC"
url="jdbc:sqlite:/var/lib/tomcat7/dbs/test.db"
/>
</Context>
...
<resource-ref>
<res-ref-name>jdbc/sqlite</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
...
Connection conn = null;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/sqlite");
conn = ds.getConnection();
Statement statement = conn.createStatement();
try {
statement.executeUpdate("create table thing(x integer)");
} catch (Exception e) {
// Could well be already there
}
statement.executeUpdate("insert into thing values(42)");
ResultSet rs = statement.executeQuery("select * from thing");
while (rs.next()) {
writer.println("id = " + rs.getInt(1));
} catch (Exception e) {
writer.println(e.getMessage());
} finally {
try {
if (conn != null) conn.close();
} catch (SQLException e) {
writer.println(e);
}
}
java tomcat-jndi tomcat-jdbc jdbc sql sqlite tomcat
Page 1 of 1