I have looked at every possible answer on this site, nothing quite covers the issue.
I followed a tutorial and tried using the Firefox SQLite Manager AND using the SQLite3 shell to create the same database.
public class DBConnector {
static Connection conn = null;
public static Connection connect()
{
try{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\John\\Desktop\\database.db");
return conn;
}
catch(Exception e){
e.printStackTrace(System.out);
return null;
}
}
}
Whenever I run this i get:
java.sql.SQLException: path to 'C:\Users\John\Desktop\database.db': 'C:\Users\John\Documents\NetBeansProjects\JBook\C:' does not exist
I'm new enough to this to where I have no idea what the issue is.
Any help is greatly appreciated. And yes, I have looked at all of the other questions posted and tried using their answers, to no avail.
Thanks again
EDIT: A couple of the possible answers already listed have included to change the .db extension to .sqlite
This did NOT work.
Another simply showed how to use an absolute path, once again, I had that covered and it didn't work.
Another talked about JUnit test and some issue they were having which was irrelevant to what I am doing
Finally got the issue:
Copy the path you specified in notepad++ and set encoding to ansi and you will see some special character before C: which is causing issue .
conn = DriverManager.getConnection("jdbc:sqlite:‪C:\\Users\\ravi\\Desktop\\database.db");
conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\ravi\\Desktop\\database.db");
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I am new to Netbeans and I decided to use Swing Forms with Java MySQL CRUD operations I have tested in Intellij before. At this point, while applying MySQL snippet as you can see below. I got "Nullpointer Exception" for the line "rs = statement.executeQuery("SELECT * FROM cities");". I also tested whether there is any database connection establishment issues with try catch phrases using "e.getMessage()" and "e.getErrorCode()" for SQLException and ClassNotFound cases but no exception returned. I have been trying understand the reason but I am having a trouble to figure it out. I will be appreciated for any help. Thanks in advance.
private static void TestWorldTable(){
//connection presets
String url = "jdbc:mysql://localhost:3306/world";
ResultSet rs = null;
try {
//connection establishment
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, null, null);
Statement statement = con.createStatement();
rs = statement.executeQuery("SELECT * FROM cities");
}
catch (ClassNotFoundException| SQLException ignored){}
try {
//record retrieval observation for CRUD
while(rs != null && rs.next()){
System.out.println(rs.getString("name"));
}
catch (SQLException ignored) {}
}
Edit: The problem has been solved. The issue is solved after creating a new library and adding mysql_connector to the classpath of that library, adding dependencies of that mysql_connector to pom.xml then changing database url to ssl and servertimezone dependent version as
"jdbc:mysql://localhost:3306/< DatabaseName >?useSSL=false&serverTimezone=UTC"
catch (ClassNotFoundException| SQLException ignored){}
Don't do this. You're asking java to take useful information about problems and just toss it in the toilet, and now you're on SO asking, bewildered, why your code isn't work. Do not ignore exceptions unless you really, really, really know what you are doing. The right ¯\(ツ)/¯ I dunno what to do exception handler is this:
catch (SQLException e) {
throw new RuntimeException("uncaught", e);
}
though, even better is to just declare your methods to 'throws' whatever seems relevant, and certainly SQLException is relevant here.
So what's the problem?
When running on the command line, the MySQL jdbc driver isn't on the classpath. You don't need that Class.forName bit, by the way. java will figure it out on its own. Make sure you use the -cp option to add that jdbc driver.
This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 2 years ago.
I've seen this question many times here but I couldn´t make my program work with any answer!
The problem is basically that i can't connect to my XAMPP server with Eclipse.
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql//localhost:80/glosario";
String user = "root";
public void conexion() {
{
try {
Class.forName(driver);
java.sql.Connection conexion = DriverManager.getConnection(url, user, "");
if(conexion != null) {
System.out.println("Conectado a la base de datos");
}
} catch (Exception e) {
System.out.println("Error al conectarse a la base de datos");
e.printStackTrace();
}
}
}
This is my code, I tried some recommended variations but neither of them worked for me.
I have mysql connector in my classpath also.
I am really noob in this area since I am just starting, this is a practice database but I can't even connect to it hehe. I also checked if my XAMPP connection is fine, and it seems that I can enter to the server, so I think the problem is not there. I have the feeling that im making some dumb mistake and I can't see it.
Any help would be very appreciated, thank you!
You don't need the Class.forName line; it accomplishes nothing.
The mysql driver needs to be on the classpath as you run your code. If it is, you can connect. If it is not, you can't - the Class.forName line doesn't change the result either way.
You should post another question if the classpath isn't the issue. In general, getting an error and not pasting the full stacktrace is highly unlikely to lead to useful answers - paste the error next time.
Instead of com.mysql.cj.jdbc.Driver please try with com.mysql.jdbc.Driver
One issue is your url is missing a colon (see syntax here; example here). Try this:
String url = "jdbc:mysql://localhost:80/glosario";
If that doesn't work, there's this question, to which the present one seems perhaps a duplicate.
I am trying to access a database that is stored in the classpath. I have installed ucanaccess 3.0.0 and all the required .jars.
My project hierarchy:
:
Here is the code I have so far:
public void login()
{
Connection conn;
try {
conn = DriverManager.getConnection("jdbc:ucanaccess:/database/theDB.accdb");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT Student_Number FROM User");
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It's a simple login screen and I'm just testing the connection to the databse on a button click. I understand that referencing an absolute file-path is not good, so I thought having the file in the classpath would be better.
I am getting the error
No suitable driver found for jdbc:ucanaccess:file:/C:/Users/Gandalf/workspace/FubbleApp/bin/database/theDB.accdb
So I think it must be the "/database/theDB.accdb" but I am not sure how to fix this issue.
Any help is appreciated. Thanks in advance
The path to the database file (.accdb or .mdb) that you provide in your connection URL must be either
an absolute path, or
a relative path from the current working directory that is in effect when your application is running, which in your case appears to be "C:/Users/Gandalf/workspace/FubbleApp/bin/".
If you want your application to automatically search the CLASSPATH for the database file you will need to either provide your own code to do that or include some third-party code to do the search for you.
I think that the accdb has to be outside of the jar file. and I'm saying this because jdbc is a protocol and you must be able to write in the db and writing in a db inside an archive you have to unarchive and archive the db again. I don't think you can do it easily... the solution is... relative to jar or absolute path. (in the same folder with the jar file)
long time lurker, first question time.
I tried searching for how to get all of the tables from a database created with OpenOffice using JDBC, and while I found answers that work for others, they do not work for me. The code itself actually returns something, but it returns something completely unexpected.
My code:
try {
DatabaseMetaData md = conn.getMetaData();
rs = md.getTables(null, null, "%", null);
while (rs.next()) {
tableNames.add(rs.getString(3));
System.out.println(rs.getString(3));
}
}
catch (Exception e) {
System.out.println("error in sendConnection()");
}
And the output:
SYSTEM_ALIASES
SYSTEM_ALLTYPEINFO
SYSTEM_AUTHORIZATIONS
SYSTEM_BESTROWIDENTIFIER
SYSTEM_CACHEINFO
SYSTEM_CATALOGS
SYSTEM_CHECK_COLUMN_USAGE
SYSTEM_CHECK_CONSTRAINTS
SYSTEM_CHECK_ROUTINE_USAGE
SYSTEM_CHECK_TABLE_USAGE
SYSTEM_CLASSPRIVILEGES
SYSTEM_COLLATIONS
SYSTEM_COLUMNPRIVILEGES
SYSTEM_COLUMNS
SYSTEM_CROSSREFERENCE
SYSTEM_INDEXINFO
SYSTEM_PRIMARYKEYS
SYSTEM_PROCEDURECOLUMNS
SYSTEM_PROCEDURES
SYSTEM_PROPERTIES
SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS
SYSTEM_SCHEMAS
SYSTEM_SCHEMATA
SYSTEM_SEQUENCES
SYSTEM_SESSIONINFO
SYSTEM_SESSIONS
SYSTEM_SUPERTABLES
SYSTEM_SUPERTYPES
SYSTEM_TABLEPRIVILEGES
SYSTEM_TABLES
SYSTEM_TABLETYPES
SYSTEM_TABLE_CONSTRAINTS
SYSTEM_TEXTTABLES
SYSTEM_TRIGGERCOLUMNS
SYSTEM_TRIGGERS
SYSTEM_TYPEINFO
SYSTEM_UDTATTRIBUTES
SYSTEM_UDTS
SYSTEM_USAGE_PRIVILEGES
SYSTEM_USERS
SYSTEM_VERSIONCOLUMNS
SYSTEM_VIEWS
SYSTEM_VIEW_COLUMN_USAGE
SYSTEM_VIEW_ROUTINE_USAGE
SYSTEM_VIEW_TABLE_USAGE
What is being returned, and how can I work around or resolve this? Thank you in advance!
Edit: The Databases created buh OpenOffice appear to be Embedded Databases by default. This may be causing the problem. Going to try and convert it to something else and see what happens.
I found a way to fix this, in case others come across this problem as well. The problem was OpenOffice was saving the database as a base file, with hsqldb under it. You need to make it just a regular hsqldb.
I used both of these links as resources:
http://programmaremobile.blogspot.com/2009/01/java-and-openoffice-base-db-through.html
https://forum.openoffice.org/en/forum/viewtopic.php?f=83&t=65980
In short, you need to extract the .odb file, go into the directories and find the database directory holding 4 other files. Add a prefix to them and then access it like normal.
I am still getting the monstrosity of the SYSTEM_* tables, but now I am actually getting the tables I want as well. From there I think I can figure out how to just get those random tables.
I'm trying to edit an MS Access database using some Java code (running NetBeans 7.2.1). I set up the data source and linked it to my database ProjectDatabase using the ODBC tool and named the data source DB, then i run the following code:
import java.sql.*;
public class NewMain {
public static void main(String[] args) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:DB");
Statement st=con.createStatement();
String name="roseindia";
String address="delhi";
int i=st.executeUpdate("insert into user(name,address) values('"+name+"','"+address+"')");
System.out.println("Row is added");
}
catch(Exception e){
System.out.println(e);
}
}
}
The code runs without and error and returns the "Row is added" message. The problem is that when I go back to view the database the changes have not taken effect. I have tried this with a code for deleting the data, also to no effect. Has anybody had this problem and knows how to solve it?
I'm running Windows 7 64-bit, Microsoft Office 64-bit with all the 64-bit drivers and I have been unable to find any mention of this problem through web searches.
Thanks in advance for any help =)
First of all you are not closing the connection, so that is one problem. Also change your code to:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:DB");
Statement st=con.createStatement();
con.setAutoCommit(false); //Notice change here
String name="roseindia";
String address="delhi";
int i=st.executeUpdate("insert into user(name,address) values('"+name+"','"+address+"')");
con.commit(); //Notice change here
System.out.println("Row is added");
con.close(); //Notice change here
This will commit the changes to access database, so now you should be able to see data in MS Access.
Read here to know more about best practices for Closing and Releasing JDBC resources