I have developed a program where I do some database connections and send some queries with JDBC.
I have used MySQL, NetBeans 6.9 under Ubuntu 11.04 as platform. When I run the app from NetBeans, it works perfectly but when I try to run it from terminal I get SQL Exception. This is the function that produces that SQL Exception. The program terminates before "Establish is ending" line.
public Connection Establish(String iname, String ipassword) throws SQLException
{
System.out.println("Establish...");
if(conn == null)
{
conn = DriverManager.getConnection("jdbc:mysql://localhost/ANU",
iname, ipassword);
}
else
System.out.println("Connection Already Established!");
System.out.println("Establish is ending...");
return conn;
} // End of Establish
Make sure the MySQL Connector .jar file is in your classpath environment variable. IDEs, like NetBeans, sometimes help you out with putting .jar files in the classpath while you're in the IDE. You'll either need to run your app with the -cp option, or add it to your classpath environment variable.
Related
I receive the following error msg when attempting to connect to a derby network server:
java.sql.SQLException: No suitable driver found for jdbc:derby://localhost/studentdb;create=true
Derby is properly installed and all environment variables set. I am able to start the derby NetworkServerControl from a Windows command prompt with the following command:
java org.apache.derby.drda.NetworkServerControl start -h localhost
,and I can do this from any location within my system's directory tree.
I can start the derby ij client from within a Windows command prompt with the command:
java org.apache.derby.tools.ij
,again, from any location within my system's directory tree.
But the code snippet below is unable to make this connection:
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:derby://localhost/studentdb;create=true";
//the error happens here, the program executes no further
conn = DriverManager.getConnection(url,null);
Statement stmt = conn.createStatement();
}
Placing the port value in the url string makes no difference.
Any suggestions would be much appreciated.
You must add the derby jdbc driver to your classpath (from derbyclient.jar, since this is the ClientDriver), then use this instruction to load the driver :
Class.forName("org.apache.derby.jdbc.ClientDriver");
So I encountered this error and it was quite an irritating and hectic task to resolve this. But in the end, I managed to find a perfect video that made me install derby from the start and guided me perfectly on how to install it. However, there is one more step after the video.
Watch this video if you have set up JavaFX packages and are able to run the program normally, but facing
"java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/DBNAME;create=true" issue when trying to run with the database.
Link to the tutorial -> https://www.youtube.com/watch?v=OKiBsWbgrMw
Now after this is set up you will now be able to start/stop the database (via the services tab) and will be able to connect with the DB. But the issue will still persist in trying to edit the DB.
To rectify this, follow the steps ->
Right click on project ---> Properties ---> Libraries ---> Click on '+' in Classpath ---> Add jar/folder ---> Go to the lib folder inside the derby and select derbyclient.jar
VERSIONS
JAVA - 17.0.1, Netbeans - 12.6
This question already has answers here:
SQLException: No suitable driver found for jdbc:derby://localhost:1527
(19 answers)
Closed 7 years ago.
I am trying to create a new SQL database with this Java program
import java.sql.*; //Needed for JDBC classes
public class BuildPhonebookDB {
public static void main(String[] args) throws Exception{
//Create a named constant for the URL
final String DB_URL = "jdbc:derby:Phonebook;create=true";
try {
//Create a connection to the database.
Connection conn = DriverManager.getConnection(DB_URL);
//Create a Statement object.
Statement stmt = conn.createStatement();
//Create the Entries table
stmt.execute("CREATE TABLE Entries (" +
"Name CHAR(20)"+
"Number INTEGER)"
);
System.out.println("Database Connected");
//Close the connection
conn.close();
}
catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
When I try to run the program I get an error that:
No suitable driver found for jdbc:derby:Phonebook;create=true
I have looked at various other similar posts on Stack Overflow, such as this one, but none help. I have seen things about a driver jar, but I don't know what this is, if I need to edit this, could someone help me through it?
Thanks for any help
Did you see this guide and have you complited all step of this guide?
Apache Derby
Download Derby Download the binary Apache Derby distribution from the
Derby web site at http://db.apache.org/derby/derby_downloads.html.
These tutorial instructions use version 10.12.1.1 and assume you
downloaded one of the binary distribution files listed in the table
below:
Operating System Download File Windows db-derby-10.12.1.1-bin.zip
UNIX, Linux, and Mac db-derby-10.12.1.1-bin.tar.gz If a more recent
release is available, download that, then substitute that version
number for 10.12.1.1 in the following instructions.
Install Derby Choose the directory into which you want to install the
Derby software. You must have write permissions to this directory. The
sample instructions below use C:\Apache for Windows and /opt/Apache
for UNIX; be sure to use your actual location. Copy the software
distribution to the location you choose, then extract it as shown
below.
Windows (use your extraction tool e.g. WinZip -- these instructions
use mks unzip):
mkdir C:\Apache copy db-derby-10.12.1.1-bin.zip
> C:\Apache cd C:\Apache unzip db-derby-10.12.1.1-bin.zip
UNIX:
mkdir /opt/Apache cp db-derby-10.12.1.1-bin.tar.gz /opt/Apache
> cd /opt/Apache tar xzvf db-derby-10.12.1.1-bin.tar.gz
In both cases, the software will now be extracted into a subdirectory
named db-derby-10.12.1.1-bin.
Set DERBY_INSTALL Set the DERBY_INSTALL variable to the location where
you installed Derby. Examples are shown below, but be sure to use the
actual location on your system:
Windows: C:\> set DERBY_INSTALL=C:\Apache\db-derby-10.12.1.1-bin
UNIX Korn Shell:
$ export
> DERBY_INSTALL=/opt/Apache/db-derby-10.12.1.1-bin
Configure Embedded Derby To use Derby in its embedded mode set your
CLASSPATH to include the jar files listed below:
derby.jar: contains the Derby engine and the Derby Embedded JDBC
driver derbytools.jar: optional, provides the ij tool that is used by
a couple of sections in this tutorial You can set your CLASSPATH
explicitly with the command shown below:
Windows:
C:\> set
> CLASSPATH=%DERBY_INSTALL%\lib\derby.jar;%DERBY_INSTALL%\lib\derbytools.jar
;.
UNIX:
$ export
> CLASSPATH=$DERBY_INSTALL/lib/derby.jar:$DERBY_INSTALL/lib/derbytools.jar:.
...
Step 3: Embedded Derby
When an application accesses a Derby database using the Embedded Derby
JDBC driver, the Derby engine does not run in a separate process, and
there are no separate database processes to start up and shut down.
Instead, the Derby database engine runs inside the same Java Virtual
Machine (JVM) as the application. So, Derby becomes part of the
application just like any other jar file that the application uses.
Figure 1 depicts this embedded architecture.
Set the environment
To set up the environment, follow the "Configure Embedded Derby"
instructions.
Use this before you get the connection from the driver:
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
I have an GUI application that connect to MySQL through xampp. Running with netbeans is fine, but when I wrap it to exe using Launch4j from the jar, it can't connect to the database (it reaches the exception even though the interface still working)
I've choose custom classpath and it contains the mysql library.
And this is what throws the exception:
try
{
// create our mysql database connection
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/vietcombank.db?useUnicode=yes&characterEncoding=UTF-8","admin", "123456789");
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
The output of the exception is com.mysql.jdbc.Driver. But if I run the jar itself by cmd with direct location as java -jar "D:\Network Programming\ServerATM\dist\ServerATM.jar" it run just fine. Is it because Launch4j didn't include the libraries?
Thank you.
instead of this :
private String url = "jdbc:mysql://localhost/your-database-name";
Try to use this :
private String url = "jdbc:mysql://127.0.0.1/your-database-name";
Move the executable file you created to the dist folder, it worked for me
I am having some trouble connecting my java script to my SQLite3 database.
I have my directory holding my script as followed
C:/PROG/JavaPROG/programs/java_database
Inside this directory I have 3 files
Query.java, Query.class and the database query.db
My java code looks as followed
import java.sql.*;
public class Query3 {
public static void main(String[] args) {
Connection conn = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:C:/PROG/JavaPROG/programs/java_database/query.db");
System.out.println("Connection Success");
} catch(Exception log) {
System.out.println("Connection Failed: " + log);
}
}
}
my classpath does contain
C:\Program Files\Java\jdk1.8.0\bin
and inside that bin directory is the sqlite jdbc driver
sqlite-jdbc-3.7.2.jar
I can connect my script to PostgreSQL database but I can't see why it isn't connecting to the SQLite3, I am getting the following error message
java.lang.ClassNotFoundException: org.sqlite.JDBC
have I done something wrong?
You need to explicitly provide the sqlite-jdbc-3.7.2.jar file in your classpath while compiling and running your classes using the -cp option. The java and javac commands will only look for .class files in the classpath and not jars.
Try this :
javac -cp <path_to_jar/sqlite-jdbc-3.7.2.jar>;. <your_class_name>.java
And if you have more than one thirdparty library, you can use a wildcard :
javac -cp <path_to_jar/*>;. <your_class_name>.java
Note that ; is the separator used in Windows. If you are on unix based systems, you need to use : instead of ;. Java maybe platform independant but the java and javac commands are not.
Also note that the . tells the java and javac commands to look in the current directory for classes. Don't forget to provide the classpath to the jars when running your program using the java command.
I have found the answer to fix this.
compile the script as normal
javac Query.java
Then specify the driver when executing
java -classpath ".;sqlite-jdbc-3.7.2.jar" Query
And it will run fine
I wrote a small java program in Netbeans. It compiles and runs perfectly. But I also need to compile it in javac in Linux because this homework is tested there. Whenever I attempt, I get the following compile error message. Do you have any idea about this message?
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../lib64/crt1.o: In function `_start':
/home/abuild/rpmbuild/BUILD/glibc-2.14.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
I just write the following line for import a library
import java.sql.*;
I am just using println except sql operations. The beginning of my code is below:
Connection conn = null;
try{
String username = ".....";
String password = "....";
String url = "jdbc:mysql://localhost:3306/.....";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, username, password);
System.out.println("Database connection extablished.");
}
catch(Exception e){
System.out.println("Cannot connect to database server");
}
After this part of code, nothing special, just ordinary lines.
It seems that error may be because you're not defining a main method, and the compiler therefore can't find it.
However, I have to ask why you're using GCC? Normal JDK is available on Linux and should be your preferred choice unless you have a very good reason otherwise! If Netbeans isn't compiling your application on Linux then it's probably because you haven't set something up properly or installed the JDK - you can (and should) use the JDK rather than GCJ, which is now largely unmaintained.
You can either grab it through your package manager or download it separately here.
You don't need to (re-)compile it for linux. It's Java. The generated class file (from windows) will run on Linux and Windows, you just need a JRE or JDK on the target host.
Compile with javac, not GCC (or GCJ if you really want something "GCCish").