Difficulty starting the database in Netbeans using DB Derby and DBeaver - java

I am trying to make a DB Derby database in Netbeans called eportdatabase, and connect it to DBeaver. However, I am having trouble starting the database at all.
try
{
String databaseURL = "jdbc:derby:eportdatabase;create=true";
Connection con = DriverManager.getConnection(databaseURL);
Statement st = con.createStatement();
if (!doesTableExists("Accounts", con))
{
String sql = "CREATE TABLE Accounts (Username varchar(250), Password varchar(250)) ";
st.execute(sql);
System.out.println("Table Does Not Yet Exist!");
}
else if(doesTableExists("Accounts", con)) {
System.out.println("Table Already Exists!");
}
con.close();
}
catch (Exception e)
{
System.out.println(e);
}
However, it does not work. When I run the application, this IOException Error Comes Up:
java.sql.SQLException: Failed to start database 'eportdatabase' with class loader jdk.internal.loader.ClassLoaders$AppClassLoader#45ee12a7, see the next exception for details.
Is there something evidently wrong with my code? Or do I need to update my drivers (which I am having trouble doing)?
UPDATE: Thanks to help with printing errors from comments, I figured out the issue; I still had the old database folder in my application folder, which was present from before I updated to JDK 15, hence not being compatible with Netbeans' software. many thanks!

Related

No database selected, despite code testing seeming to work

I'm at a complete loss, I don't understand what is wrong here. I'm writing a Java program to take a few databases and put them into a mySQL database. I've got the JConnector in my build path:
Build path screenshot
try {
String driver = "com.mysql.cj.jdbc.Driver";
String address = "jdbc:mysql://localhost:3306/?user=root/Exercise";
Class.forName(driver);
con = DriverManager.getConnection(address, username, password);
System.out.println("Connection Success");
Statement st = con.createStatement();
int c =st.executeUpdate("CREATE TABLE test (Name VARCHAR(30))");
System.out.println("Table have been created.");
System.out.println(c+" Row(s) have been affected");
return con;
} catch (Exception e) { System.out.println(e);}
When I run this code, the output is:
Connection Success
java.sql.SQLException: No database selected
When I delete the "?user=root" part of the address, it will instead give me:
java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the main URL sections.
This implies that, if I can't connect to a database, that it'll throw the exception, so apparently it is connecting, but then it's saying no database is selected despite that I'm literally connecting to it just a few lines back. In fact, the Statement line to the return line are code I took from another question's solution about this specific issue to test it, and their code seemed to be almost exactly the same as my own. What in the world am I doing wrong here?
Edit: I just tried running it again without the "?user=root" because of g00se's answer, and I somehow got A DIFFERENT error message than the one I already posted that I was getting.
java.sql.SQLSyntaxErrorException: Unknown database 'exercise'
EDIT 2:
I just had a thought, Eclipse is on an external harddrive, and I have no idea where the SQL database is stored, but could that be the issue? They're on different drives?
For unknown reasons, the exercise database (as well as others I had) went missing or were deleted. Thank you to g00se for suggesting I look at the databases and see what was actually there, readding it fixed the problem.

Can't connect my SQL Server base to Java app on NetBeans

I have installed NetBeans IDE 12.3 on Lubuntu and SQL Server thanks to the microsoft documentation.
I have created my database using the command line tool sqlcmd (I don't have a GUI for sql server like you have on windows).
In the netbeans interface, I connected my sql server base to my java project, I see the base, I see the tables. Using the SQL tool of netbeans I can request them, add/delete entries etc. no problem.
But I would like to make request in the java code itself, not in the sql tool of netbeans. The documentation says I should create a Connection object, but can't I just use the connection I created through the graphical netbeans interface ?
I get this error: java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost\db:1433;databaseName=ConservatoireDB
Here is the code in main:
ResultSet resultSet = null;
try (Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost\\db:1433;databaseName=ConservatoireDB");
Statement statement = connection.createStatement();) {
// Create and execute a SELECT SQL statement.
String selectSql = "select * from salle";
resultSet = statement.executeQuery(selectSql);
// Print results from select statement
while (resultSet.next()) {
System.out.println(resultSet.getString(2) + " " + resultSet.getString(3));
}
}
catch (SQLException e) {
e.printStackTrace();
}
By the way, my project is a graphical app using Swing library.

SQLite error or missing database (no such table) in Netbeans connection to SQLite

I am getting the above error when trying to do an insert(or select) to a SQLite file from Java in Netbeans. I have created the db file manually from SQLite Database Browser and put it in the source package. Below is the code and logs:
public void DBInsertServerConfig(ServerConfig serverconfig) throws SQLException {
Connection conn = DBConnect();
Statement statement = null;
conn.setAutoCommit(false);
statement = conn.createStatement();
String sql = "INSERT INTO serverconfig(ip,port,db_name,db_user,password,fcm_server_key) " +
"VALUES (?,?,?,?,?,?)"; //
try{
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setString(1,serverconfig.getIp());
pstm.setString(2,serverconfig.getPort());
pstm.setString(3,serverconfig.getDb_name());
pstm.setString(4,serverconfig.getDb_user());
pstm.setString(5,serverconfig.getPassword());
pstm.setString(6,serverconfig.getFcm_server_key());
//statement.execute(sql);
pstm.executeUpdate();
statement.close();
conn.commit();
conn.close();
The database is opened correctly but it seems it doesn't find the table although it exist.
compile:run:
Opened database successfully
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: serverconfig)
BUILD SUCCESSFUL (total time: 9 seconds)
Attached is a screenshot of the db file from SQLite Database Browser :
I have seen and tried other posts like in here but I didn't get a solution.
Can anyone help me figuring out this?
I found the answer and I am posting if anyone run into the same problem/confusion.
I had put my db file under the src package while the url path was pointing into the project root folder(outside src). An empty db file was created by netbeans, and of course it hadn't any table in it. That's what happen when you follow tutorials that haven't been tested by their own creators. :D
The example that I used was
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Tolga\\Documents\\NetBeansProjects\\dpmlzmtkb\\depom.sqlite");
Which is wrong.
I changed depom.SQLite to depom.db and it worked. So as I understand if the path is correct NetBeans creating another empty SQLite database to the given path.
Please use the absolute path,like(on my Ubuntu)
private static String url ="jdbc:sqlite:/home/yourname/study/eclipse/hk/ss.db";
At first i get same error like yours,
i can link sql in ordinary class,but in the servlet/jsp can't

Primary key violation error code using ODBC with MS Access

I am using Java with Microsoft Access through an ODBC driver. When I insert a duplicate entry for primary key it gives me an error: java.sql.SQLException: General error. I want to show a message to the user that this record already exists, but I think that this exception can be thrown by ODBC in some other cases also. So I found that there are error codes against each message (ref), but I found no error code for primary key violation. Can anyone tell me what error code is for primary key violation for ODBC with MS Access?
Here is basic code
String qry = "INSERT INTO customers VALUES ('" + txtReg.getText()
+ "' ,'" + txtName.getText() + "', '" + txtCity.getText() + "' ,'" + txtCell.getText() + "')";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:MyDB");
Statement st = con.createStatement();
st.executeQuery(qry);
st.close();
con.close();
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex, "Error!", JOptionPane.ERROR_MESSAGE);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex, "Error!", JOptionPane.ERROR_MESSAGE);
}
These txtName and so on are JTextFields. Here is complete Stack trace
connected
st created
Error code: 0
SQLState: S1000
Messsage: General error
java.sql.SQLException: General error
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6986)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3110)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:288)
at gui.InsertFileCode.btnInsertActionPerformed(insertFileCode.java:399)
The problem is in this line st.executeQuery(qry);
executeQuery(query) method is used mostly for SELECT statement and it returns the in form of ResultSet object.
Sence the statement is INSERT statement you have to use executeUpdate(query) , this method is generally used by INSERT, UPDATE, And DELETE statements. When table updated successfully then it returns 1.
For example
int result = st.executeUpdate(qry);
System.out.println(result);
UPDATE:
Due to the comments, I've fegured out you have another problem rather than the SQL statement. You must beware of when using java to Ms Access, you're actually connecting to a middleware server, so that, you must expect un-expected exception(s) while running the SQL statement, for example:
CRATE TABLE FOO (ID varchar (50) NOT NULL , NAME varchar (255) DEFAULT NULL)
This query runs on SQLite and MySQL (maybe SQL Server too as I didn't test it), gives Syntex error on Access, as DEFAULT NULL should be removed for running the statement.A nd maybe there are many other problem you have to prepare facing it with Access "database" file.
So, I am telling you to leave it, MS Access is suitable for its users, not for us as a programmer, we have to find best-general way because we must consider that some user uses this application that don't know anything neither about Programming Language nor Database.
Then, what should I do?
I am not an expert in database, but take my advice :
If your application need to share its database: MySQL, Oracle and SQL Server used for that purpose.
If your application is used only for some purposes and not need to share its records to other users, use an actually serverless database engine such as SQLite. This seems to be the best option for you as it's a file like Access, only needs an external driver for Java, see this.
I think there is a FireFox extension for designing the SQLite database if you search on google maybe you find it.

How to access a java Derby database? I could really do with a few helpful pointers

I am new to using Derby and databses in eclipse, and I have become a tad lost and in need to a bit of help. I have established a database connection, created a new database, and a new schema, within which I have some tables containing some test data. I don't have any problem with the sql queries to select the relevant data. The problem I have is getting to a point where I can use queries. I am trying to create a class which connects to the database, and for testing purposes, uses a simple query to select some data. This is what I have so far:
public void getExerciseInfo() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
connect = DriverManager.getConnection("jdbc:derby://localhost/c:/TestDatabase");
PreparedStatement statement = connect.prepareStatement("SELECT * from TESTSCHEMA.TESTTABLE");
resultSet = statement.executeQuery();
while (resultSet.next()) {
String name= resultSet.getString("NAME");
String type = resultSet.getString("TYPE");
System.out.println(name);
System.out.println(type);
}
} catch (Exception e) {
} finally {
close();
}
}
All I am trying to do is output the data in the table to the console, but I cant even do this simple task :( Im guessing my connection url is invalid, is it supposed to be the file path to the database folder in my eclipse workspace?
Anyhow, I am very lost, and any help would be greatly appreciated.
Did you take a look over: http://db.apache.org/derby/integrate/plugin_help/derby_app.html ? You seem to be using the network server but your db URL is wrong.
If you are not running the Derby server, you can establish an embedded database connection or use an EmbeddedDataSource, shown here.

Categories

Resources