com.datastax.driver.core.Session
for example:
//This works
session.execute ("select * from table");
//This returns a nullpointer
session.execute ("create table testtable ( number int, string varchar)");
Do I have to use some sort of schema builder?
NOTE: im connected to the cassandra instance and can query it no problem. I just want to be able to create tables from the datastax driver
What error do you get when trying to CREATE a table?
As for some quick things to try, it's possible that your user might be missing the CREATE permission.
session.execute ("create table testtable ( number int, string varchar)");
Another thing I noticed about this statement, was that you don't seem to be specifying a PRIMARY KEY. All tables in Cassandra must have a primary key.
Try altering your CQL to this, and see if it helps:
create table testtable ( number int, string varchar, PRIMARY KEY (number))
Related
I'm new to java . i've two questions . i'm using flyway and h2 db i added two file sql one of them to create table with two columns like that
CREATE TABLE contacts (
id bigint auto_increment NOT NULL,
name varchar(128) NOT NULL,
PRIMARY KEY(id)
);
and the other is to alter new column like that
ALTER TABLE contacts
ADD COLUMN contacts Varchar(255);
1- i used flyway.migrate worked fine but i faced mismatch so i used flyway.repair() is that normal to use it every time ?
2- when i wrote statment sql for executing insert sql command like that
stmt.execute("INSERT INTO contacts(name,contacts) VALUES('ABC','ABC#yahoo.com')");
i got
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Column "CONTACTS" not found; SQL statement:
INSERT INTO contacts(name,contacts) VALUES('ABC ','ABC#yahoo.com') [42122-173]
You need to add AFTER in your second sql file.
ALTER TABLE contacts
ADD COLUMN contacts Varchar(255) AFTER name;
I am trying to add row numbers to a sql query to get a return resultset, but the JDBC does not support BIGINT it says. I look up https://db.apache.org/derby/docs/10.9/ref/rreffuncrownumber.html and https://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.jdbc_pg.doc/ids_jdbc_141.htm.
The code:
String query = new StringBuilder("SELECT ROW_NUMBER() OVER() AS id, * FROM "+tableName).toString();
Error:
[Informix JDBC Driver][Informix]The data type bigint is not supported for current client/server configuration.
The IBM solution tells you to use getBigSerial() to get the BIGINT after the insert. However, I want to find a way to be able to add some auto increment numbers when it queries the table without creating an actual column. Is there a way?
You can cast it to VARCHAR in the query.
You can cast that String into BigInteger in Java code if you use it for something more than present it to the client.
Casting is good:
String query = "SELECT CAST(ROW_NUMBER() OVER() AS INT) AS id, * FROM "+tableName;
I test it with my informix database and it works.
As the title question states, I am attempting per the instructions of this tutorial: https://blogs.oracle.com/NetBeansSupport/entry/populating_jtable_from_mysql_database
And the message I keep receiving is: "Tables without primary keys are not supported by the Java Presence API"
The thing that is driving me crazy is that the table that I created myself using SQL does have a primary key...
Are there other(working) methods to do this, because I have looked and tried and have failed time after time again...
btw this is the SQL for those who are wondering(the server is running via localhost)
CREATE TABLE Customers
(
KL_NR INT NOT NULL AUTO_INCREMENT,
NAAM VARCHAR(30) NOT NULL,
EMAIL VARCHAR(30),
ADRES VARCHAR(30),
PLAATS VARCHAR(100),
POSTCODE VARCHAR(6),
TELEFOON VARCHAR(13),
BEDRIJFSNAAM VARCHAR(30),
PRIMARY KEY (KL_NR)
);
Just FYI I have a form setup in my application that can add rows in the database, so it's not a connection issue.
Thanks in advance.
EDIT: changed it to only 1 PK but as i stated in the comments it doesn't change anything. even when i try to import it using: the table contents and binding it.
I fixed it by making an updateTable class
String sql = "select * from customers";
preparedStatement=connect.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
Table_customer.setModel(DbUtils.resultSetToTableModel(resultSet));
//this part forces the premade pallet Jtable to morph
//into the model of my mysql server database table
I have a table named books with bookID, bookName, count , orderCount
i'd like to write an sql query that will update all books.orderCount to books.orderCount+1.
How shall i do that using executeQuery("UPDATE books...."); ?
I'm having troubles with the syntax.
I've tried to search info on the net however most articles are about INSERT or DELETE commands and the only article that was related suggested to retrieve orderCount to Java, update it and then write it back to SQL. if possible i prefer to avoid it as it may cause serious problems (Locks on records are not needed for this task so i can not use them to avoid problems)
this should be pretty straight forward,
UPDATE books
SET orderCount = orderCount + 1
If it's about a primary key:
Also, you can AUTO INCREMENT.
CREATE TABLE Persons
(
P_Id int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:
ALTER TABLE Persons AUTO_INCREMENT=100
To insert a new record into the "Persons" table, we will not have to specify a value for the "P_Id" column (a unique value will be added automatically):
INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen')
Am using JPA into my application and then the when insert different objects the database use sequential ids.
For Example:
If i have table member and table user , when inserting object of type user will take id = 1 then add object into table member will take id = 2 .
The problem is : i imported a .sql file into the database.
Then when i insert a new record from my application it cause an exception because it use ids already added.
How can i solve this problem ?
You can set the start value for AUTO_INCREMENT as
ALTER TABLE tbl AUTO_INCREMENT = 100;
See also:
3.6.9. Using AUTO_INCREMENT