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.
Related
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.
I have a Spring new web application(Java), and after having the user login (using username and password), I want to make the user able to add a new user, till then all is good. Each user have a primary Key iduser which autoincrimented (AI), and a foreign key idprofile. The table Profile contain informations about the user, and it's idprofile is the primary key (which have to be the same as the ipdrofile in User table).
As you may know, MySQL don't let you make more than one AI per table (MySQL 5 workbench as platform), and when my code add's an user, it has to add a profile in the same time, but with three fields that are unknown, which are iduser, idprofile (user table) and idprofile (profile table). As the ipdrofile can't be an AI, how can I manage to put the right key on it when saving the User and Profile (Adding) ?
You insert the profile first, returning the new profile id, then you insert the user, using the returned profile id, optionally returning the new user id if you need it.
You'll need to use these two methods to get the auto-generated key returned:
Connection.prepareStatement(String, String[])
Statement.getGeneratedKeys()
In my database I have table "Announcement" with fields:
id int,
text varchar,
sent_date date.
Also table "Group" with fields:
id int,
name varchar.
Each announcement submitted to any number of groups(from 1 to all). How to store this relation in database?
I'm using MySQL and Hibernate in java web project.
Create a third table (I call it announcement_group) with two columns:
announcement_id REFERENCES Announcement(id)
group_id REFERENCES Group (id)
Read more about Foreign Keys from the documentation here http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
In your query you will want to use a
SELECT group_id WHERE announcement_id=<your id here> from announcement_group;
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 want to create an Entity class with database in Netbeans.
When I select a Data source jdbc/Ionbank (custom Jdbc connection Using JDBC-ODBC bridge with Ms SQL 2005 as database).
I see all the tables from that database.
All tables show no primary key, but they have primary keys in them.
Things I have tried :-
Created new 4-5 data source.
Created tables using query, and not the New table option.
Tried changing Odbc connection.
Tried using different drivers for the Jdbc-Odbc bridge like Sql4jdbc.jar, Jdts.jar.
I had same issue, but i solved it using the following: "New Entity Classes from Database" cannot process some tables, saying "no primary key"
A quote from that link helped me:
The problem will happen if you have foreign keys where upper case and lower case table names don't match the referenced table's definition.
For instance:
create table OkTable (
id int not null auto_increment
, primary key (id)
);
create table MisunderstoodTable(
id int not null auto_increment
oktable int not null
, primary key (id)
, foreign key ok (oktable) references oktable (id)
);
The MisunderstoodTable has a foreign key where the target table name doesn't match the lower/uppercase name of the referenced table.
To avoid this problem, just make sure you type your foreign key definitions while matching upper/lower casing for the targeted table.