How to import an existing database into netbeans - java

I had created a database using mysql workbench and i saved that database in one of the local drives. So in netbeans I need to design a database which is same as the earlier (created by using mysql workbench). So I would like to import that database into netbeans. Can anyone explain the procedure to do so?

A couple of days back I faced the same problem and found a easy solution. Though this question is asked 2 years back, I am posting this answer because it still doesn't have an answer here, so that it can provide help to other people with same problem in future.
I assume you know how to configure MySQL server. If no, visit https://netbeans.org/kb/docs/ide/mysql.html and follow the 'Configuring MySQL Server Properties' part.
After you create the server, right click on the 'MySQL Server'->'Create new database'. Give the database same name as the existing database has.
Now create a connection to the newly created database. If you dont know how, see the 'Starting the MySQL Server' and 'Creating and Connecting to the Database Instance' from https://netbeans.org/kb/docs/ide/mysql.html.
Now Expand the newly created connection which will look like 'jdbc:mysql://localhost:(portNumber)/(databaseName)'. Expand the database name node and right click 'table' and select 'Execute Command'
A new window will open. Assuming that you have a .sql file(Or may be a file without extension but consisting of SQL commands. You can export the database in file using your current admin tool if you have not already). Open that file in editor(notepad) and copy paste everything in the new opened command window in Netbeans.
Before Executing, just add one line before which states which database to use to execute query on.
USE <newly_created_database_name>;
as shown in the screenshot below (Red mark), it should be before the first line of the copy pasted SQL commands.
Now Execute the commands using the 'run SQL' button (green mark in image). To check, expand the tables node to see all the tables in original database are created.
Now you have your original database in place to work with.

You can use the built-in tools.
With your NetBeans project up, click new file and search for a persistence unit.
Follow the prompts entering the corresponding information(ie, host info, username, password, etc).
Once completed, go back in your project view and click on the services tab. You should see the database connection there.
To add them to your project, go back to your project tab and add a new file of type "Entities Classes from Database" the next prompts will ask you what tables to import and such.
Once completed, NetBeans will auto import all tables with getters and setters.
Here is a succinct example from Official Documentation: https://platform.netbeans.org/tutorials/nbm-crud.html#creating-app

Related

JavaFx application with database executable

So, i've just finished a small javafX application, with database and stuff... I used Netbeans and SQl developer, now i want to export my project so i can use it anywhere i want; any computer, So, i've tried some programs like Launch4j or something... but the main problem is, even if i make the .exe file, what's gonna happen with the database? it's located in my PC, so if somebody try to use my application, he can't access to the database, so the application won't work...
In other words...What is the solution that i can use to like "Combine" the database with the application, if it is possible? or create the .exe file with the database... I hope that my problem is clear, and thank you for your answers .
as per my understanding you can do two things.
You can deploy or host your database to any online server and create some web services to fetch and insert data to your application.
You can create one startup class to create database but here you need to use lite version of database like sql-lite.
if you need more information about sql lite then click here

Eclipse AWS Explorer can't see Dyanmodb tables created with Java

I'm trying to use AWS Explorer in Eclipse but it doesn't show newly created tables. When I use this Java sample code to create a table AWS Explorer doesn't show the new table after refreshing. It doesn't show it in either the local or us-west-2 region. If I run the code again it throws an Exception saying "Cannot create preexisting table ..." so the Movies table was created. If I change the second parameter in
new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")
by deleting the '2' I can run again successfully. Running a second time gives the same error.
What does the second parameter to EndpointConfiguration() do? It doesn't have to be a valid region name. Is this supposed to correspond to one of the regions in the AWS Explorer region drop list? Why do I need a region if I'm running locally? The second parameter does something since changing it allows me to create another table, but having it be a region name just muddies the waters. Apparently it can be anything. The JavaDoc is no help on this.
The Java code apparently is creating the Movie table somewhere else than AWS Explorer is looking. I've also tried RazorSql and it doesn't see the Movie table either. Both see tables created with AWS Explorer though.
Where does the Java code create its tables?
Try this for the client line:
new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "local")
I figured it out by checking what .db files were being created for the version of DynamoDB that I'm using. I saw one with "us-west-2" and another with "local". I decided to give "local" a try as the region and it worked! I can now see tables that were created using the "local" region.
I've posted a comment on the Amazon DynamoDB Developer guide explaining the need for an update to their code examples.

How to allow other people to use desktop application that relies on database?

So I've created a java desktop application using Swing. It stores data entered into the application in a MySQL database (localhost). Now how would someone else be able to use the application ? Would they require a MySQL database as well ? What are the best practices for doing this. I do eventually hope to submit the entire project folder to github.
I guess I would have to ask first whether this program is a demo or classroom project or if it's a real application, because the answer would be different.
For a demo project, it would be fine to post your code to github, and also dump the database to a .sql file and commit that as well. MySQL ships with a handy tool called mysqldump that will do just that - export the entire contents of your database. Then a person can clone your github repo, install MySQL locally, and run your sql script to get a copy of your database. Once they follow those steps, they should be able to run a copy of your swing app on their machine just like you can. One caveat here though is it's best practice to avoid putting very large files (especially binary files) in git. I'm not sure what you need, but if you can put a small sql file out there that's definitely preferred.
That's a segue into the other answer which is hosting your MySQL database somewhere. For a real application, making copies just won't do. Then what you need to do is host your database centrally using a service like this one and allow your users to connect to it. You can still use mysqldump to get your database out there on the web after creating it on localhost. If you go this route though, you'll definitely want to avoid putting your database connection strings on github. Again, this scenario is really only useful if your app is intended to be used for real - don't bother with hosting if you don't need it.
Hope this helps!
Firstly, you should install mysql with a public ip, then alter the connection to mysql with the ip,the example follows below
String url = "jdbc:mysql://ip:3306/db";
Connection conn = DriverManager.getConnection(url, user, password);
if you want to run java program like exe file, package the java program to a jar file with jre

JPA Cannot resolve column/IntelliJ

I'm trying to map some Java classes using the Hibernate JPA implementation. My problem is that I can't use hardcoded Strings als column namens. You can see the error message I get in the picture below.
I'm using OpenJPA as my Default JPA Provider and have already tried to change it.
IntelliJ Version 14.0.3
regards,
You have to associate your data source (create a data source in database view first - aka your real JDBC database connection) with a persistence unit or session factory (hibernate.cfg.xml).
Follow below steps and the warnings will disappear:
Open the Persistence tool window (View | Tool Windows |
Persistence).
Right-click the necessary module, persistence unit or
session factory, and select Assign Data Sources in the context menu.
In the Assign Data Sources dialog that opens, click the Data Source
field and select the necessary data source. (To remove the
association with the data source, select none.)
See here: Associating data sources with session factories and persistence units
For those who just want to disable this check, go to
Intellij IDEA -> Preferences -> Search "Unresolved database references in annotations" and uncheck it.
(macOS Mojave and Intellij Ultimate 2019.3)
This will disable the inspection and remove all the "Cannot resolve column…" errors on the #Column annotations. Intellij will stop checking if the String column names exist in the database tables. Do it at your own risk.
I found Panos' answer useful, but I didn't see anybody mention adding the database as a data source. Perhaps that's routine enough to be assumed, but if not, here's what I had to do:
Select View/Tool Windows/Database
The Database window usually appears on the top right.
In the Database window, click the green + sign and select Data Source/MySQL (or whatever flavor of Data Source you're using).
The Data Sources and Drivers window pops up.
If your database isn't listed there, use the empty form to add the following:
Host: usually localhost, but if your test database is living
on another machine, put that address in.
Username: for your database use.
Password: for your database user's password.
IDEA might need some fiddling to find the JDBC driver. I was told that in theory it should have found it in the maven build process for the project I was working on, but it did not. I reopened View/Tool Windows/Database and looked at my MySQL entry. At the bottom it had an error message next to Driver:, and also a MySQL link. I clicked the MySQL link and IDEA brought up a popup to fetch Connector/J.
Despite the fact that the checkboxes for Auto commit and Auto sync defaulted to checked and I left them that way, IDEA seemed to need a nudge. Or perhaps it was just taking a while and I was impatient.
In any event, first I double-clicked on the line for my database in Database. That seemed to do it, but I didn't realize I needed Persistence yet, and while sorting that out, at a coworker's suggestion, I also clicked the Synchronize button (two arrows in a circle) on Database.
You can also right-click on your database in Database and select Synchronize.
It may take a few seconds, but you should see IDEA filling in the database schema under the entry in Database.
Finally I found Panos's answer and fixed Persistence.
Select View/Tool Windows/Persistence
The Persistence window usually appears on the top left.
In the Persistence window, right-click on your project and select Assign Data Sources.
IDEA pops up a dialog with two columns, your project in the left column and in my case an empty cell in the right column. Click on the empty cell and IDEA should give you a dropdown that allows you to select the database you just added.
Again, it may take a few seconds for IDEA to finish analyzing the data source and redo all the inspections.
Just for anyone else whom this didn't solve and comes across via google (like myself) .. setting the table name via the #Table Annotation fixed it for me.
Actually this is not any error that prevents your code to compile. Probably your spell-checker is on and which gives you the spelling mistake. If you can compile your code then you may ignore these type of scenario.
Look you have not getting any error for other text like #Column, #GeneratedValue etc. That means jars using these kind of stuff are in your build path. So I think you can ignore these type of error.
maybe this solution also help someone...
Open Database dialog window from the right side of Intellij
Go to DB Data Source Properties (find it in top menu)
Go to Schemas
Uncheck "Default database"
Check your specific DB and inside also check Default schema(public)
Good luck!
If you are using the JPA, it is also important to set up the SQL Dialect as
HSQLDB instead of MySQL. This is often confusing because it feels like MySQL is the correct dialect, but in fact what you are dealing with hibernate is HSQL.
If you have just upgraded to IntelliJ 2017.13 or other derivatives of this version (e.g., Webstorm), or you've just re-imported your project, it might mess up the language injection and need to manually correct this.
This can be accomplished via the Project
Settings -> Languages and Frameworks
part of the IDE.
If there is no real mismatch and this warning happens just because of IDE, you can easily link your project with the data source that you connect. Intellij already suggests the way to do it.
I'm using Intellij and connecting DB by JPA. I've met problem just like you and this is my solution.
You need to connect your database following these steps:
View -> Tool Windows -> Database -> then add database you are using to Intellj
After connect your database, you need to assign data source to Intellj by following these
View -> Tool Windows -> Persistence -> then add data from database you just added from 1 step
OK. That's all!
P/s: make sure your code work well!

Do I need to install SQLite so that SQLiteJDBC works?

I guess I'm just not "getting it". If I don't have SQLite already installed on my computer, and I want to write a Java app that uses an embedded database, and I download/import the SQLiteJDBC JAR into my project, is that all I need? Or, do I need to first install SQLite before and create a database file for my SQLiteJDBC code to connect to and run queries from?
If that's the case, and its not sufficient to just download/import SQLiteJDBC, then doesn't that mean that I'll have to make sure SQLite is installed on every system that I want to run my Java app on? And doesn't that defeat the purpose of a portable/embedded database?
Basically, I'm getting hung up on the SQLiteJDBC tutorials because:
They don't tell you how HelloWorld.sqlite gets created (does SQLiteJDBC create it for you, do you have to create it in SQLite first from the command prompt, etc.); and
They never clarify whether SQLiteJDBC is dependent on SQLite for the API calls to work
Any help here is greatly appreciated!
You have to put the SQLLite JAR in the CLASSPATH of your app. There's no "install" beyond that.
Maybe this tutorial can help you.
Here's another that shows how to create a database and tables.

Categories