Trying to add a database table to my JTable in Netbeans - java

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

Related

Primary key is not loaded from PostgreSQL to JTable

I'm new to PostgreSQL and Java Swing.
In PostgreSQL company database, I have a users table and it has 4 fields: user_id, username, phone, and address.
CREATE TABLE users
(
user_id serial primary key,
username VARCHAR(40) not null,
phone VARCHAR(14) not null,
address VARCHAR(50)
);
I'm trying to load all fields from users table to JTable in Java Swing using Bound (Figure 1).
Then I bind the elements from the table (Figure 2).
As you can see in Figure 2, it shows only 3 fields except user_id. I need to load this user_id field as well because I need to perform CRUD data.
How can I achieve that?
I finally got the solution.
When I import the data from the database to the form, the Model (for example; Users.java) file is generated.
The problem was I firstly generated that file and later I added the user_id column to the database, so guess what, the Model file is somehow not updated and so the user_id is not there.
Therefore, I had manually added getter and setter for the user_id field, and now ok.

Can you execute a create table from Datastax driver with session.execute?

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))

write persian or arabic alphabets in mysql

I am new in java and I am trying to do a simple project to get familiar with that.
So, I am working on a dynamic web application which I use tomcat as server and MySQL with hibernate provider.
I want to be able to write persian or arabic alphabets in my tables. but unfortunately I cannot.
I have written this query for my database tables:
DROP DATABASE IF EXISTS myDb;
CREATE DATABASE myDb CHARSET = utf8 COLLATE = utf8_general_ci;
USE myDb;
drop table if exists user;
drop table if exists resume;
create table resume(
resumeId INTEGER NOT NULL AUTO_INCREMENT,
resumeDescription NVARCHAR(255),
PRIMARY KEY (resumeId)
) charset = utf8;
create table user(
userId INTEGER NOT NULL AUTO_INCREMENT,
username NVARCHAR(40) NOT NULL,
password varchar(20) NOT NULL,
email varchar(50) NOT NULL,
resumeId INTEGER UNIQUE,
PRIMARY KEY (userId),
FOREIGN KEY (resumeId) REFERENCES resume(resumeId)
) charset = utf8;
I have tried to insert persian alphabets. I have created a form and made a servlet for that to handle the request. I've got parameters from the request and tested them. At that moment they were ok and they were shown properly. but when I insert them in database I face with this:
(the question marks are persian alphabets)
I dont know what to do and what is the problem.
I have searched for this problem on the internet and tested different ways but none of them worked in my case.
Can anyone please help me to write persian alphabets properly in mySql database?
by the way my connection url is this:
jdbc:mysql://localhost:3306/myDb?zeroDateTimeBehavior=convertToNull
Try using the following connection url:
jdbc:mysql://localhost:3306/myDb?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8
This will force the driver to use UTF-8.

#1467 - Failed to read auto-increment value from storage engine

I was trying to create a new object and this error appeared:
java.sql.sqlexception failed to read auto-increment value from storage engine
So I went to the phpMyAdmin to create the object there and the same showed up:
MySQL said: Documentation
1467 - Failed to read auto-increment value from storage engine
then I clicked on edit, and it was there:
INSERT INTO `reservation`.`room` (`idroom`, `number`, `floor`, `description`, `characteristics`, `cost`, `status`, `type`) VALUES (NULL, '114', '3', 'ss', 'ss', '550.00', 'Available', 'ss')
(idroom is supposed to be auto-incremented.)
I already read other posts where they say I have to put this:
ALTER TABLE `table_name` AUTO_INCREMENT = 1
but I have no idea where to put that. Is there a better solution?
Your INSERT statement is wrong. Since idroom is AUTO_INCREMENT; you must not include it in the column list on your insert command. Your insert statement should look like below. Notice that I have removed idroom column from insert column list and not passing NULL as well in value list.
INSERT INTO `reservation`.`room` (`number`, `floor`, `description`,
`characteristics`, `cost`, `status`, `type`)
VALUES ('114', '3', 'ss', 'ss', '550.00', 'Available', 'ss')
I also struggled with this problem and searched, and didn't find anything. Then the following worked for me; I guess it might work for your problem. Thx.
1st:
-delete (before backup)->all data from your database.
-try to run your Java program again, or any program you want.
If it fails then go to 2nd.
2nd:
- backup all data from your table
- delete table completely
- create table again; example shown below:
CREATE TABLE `users` (
`id` int(6) NOT NULL,
`f_name` varchar(30) NOT NULL,
`l_name` varchar(30) NOT NULL,
`address` varchar(50) DEFAULT NULL,
`phone_num` varchar(12) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL
);
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
AUTO_INCREMENT for table `users`
ALTER TABLE `users`
MODIFY `id` int(6) NOT NULL AUTO_INCREMENT;

Java SQL simple update syntax issue

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')

Categories

Resources