How can I connect to a database on my filesystem using Java - java

I need to connect to a database on my file system but I have noticed that JDBC is referring to a database in memory, any time I want to check the tables need to write a code to retrieve their data, currently I have a SQLite database.
I used the following code but even when I enter a correct address it does not connect to my database.
String sDriver = "org.sqlite.JDBC";
String Database = "users/documents/DB.sqlite";
String sJdbc = "jdbc:sqlite";

Move your database (DB.sqlite) in your working directory. Then use this url to connect to it :
jdbc:sqlite:DB.sqlite
Using JDBC, it will be something like this :
String driver = "org.sqlite.JDBC";
Class.forName(driver);
String dbUrl = "jdbc:sqlite:DB.sqlite";
Connection connection = DriverManager.getConnection(dbUrl);

Related

How to convert Azure DB URL to localhost DB URL?

I've got an Azure SQL Server database that I'm connecting to via JDBC, but want to connect instead to my SQL Server "localhost". In SSMS, I connect to localhost without needing a password. So, do I still need to enter a password in Java?
I have a code like this :
String connectionUrl =
"jdbc:sqlserver://etcetc.database.windows.net:1433;"
+ "database=med;"
+ "user=windersan#salemimed;"
+ "password=********;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
// + "hostNameInCertificate=*.database.windows.net;"
+ "loginTimeout=30;";
How do I change this to connect instead to localhost?
Just replace the etcetc.database.windows.net by localhost and replace the port number 1433 by the number that you are using.
I have used SQLServerDataSource class to make the work easier. You can also create a string URL and set it in the DriverManger.getConnection().
Try with this code :
SQLServerDataSource dataSource = new SQLServerDataSource();
dataSource.setUser("windersan#salemimed");
dataSource.setPassword("********");
dataSource.setServerName("localhost");
// set the port number of your system below.
dataSource.setPortNumber(1433);
dataSource.setDatabaseName("med");
dataSource.setEncrypt(true);
dataSource.setHostNameInCertificate("*.database.windows.net");
dataSource.setTrustServerCertificate(false);
Connection connection = dataSource.getConnection();
Please refer to this links down below for more info.
Microsoft Docs - ISQLServerDataSource Interface - This contains the list of methods that you can use to set the various properties in the datasource.
Microsoft Docs - How to work with the connection - This contains examples of the possible ways to connect to a SQL Server database.
the first line of your concatenated string contains the url etcetc.database.windows.net:1433 this is the location of the database server, and the bit you should change.
Also, it might be worth doing a google search on connecting to SqlServer with JDBC to see if there are any examples out there.

Access SQL Server 2008 R2 database from netbeans 8

I am completely new to java, and I want to make a connection to a remote SQL server 2008 R2 database (like 192.168.17.11) and load data from it.
Please suggest alternative ways if you know any.
First you must have JDBC driver for MS SQL, you have two jars (jtds-1.2.5.jar or sqljdbc4-2.0.jar), which added to your classpath
Second you need to create your connection as below:
String password="pass";
String driver= "net.sourceforge.jtds.jdbc.Driver"; // For sqljdbc4, use: com.microsoft.sqlserver.jdbc.SQLServerDriver
String username="user";
String URL="jdbc:jtds:sqlserver://serverIP:port/dbname"; // For sqljdbc4, use: jdbc:sqlserver://serverIP:port;databaseName=dbname
Class.forName(driver);
Connection conn = DriverManager.getConnection(URL, username, password);
// Use your connection here
// Don't forget to close the connection

msSQL jdbc .. I connected to the server but how to connect to use a specific databaseName

Last time I used SQL with Java/Eclipse I had a SQL script linked to the Project.
This time, I connected to a different server using jdbc format (not localhost) and it connects correctly.
Now I need to get into this specific database named WInfo but I don't know how to do that.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
c = DriverManager.getConnection("jdbc:sqlserver://serverName:1433/;user=UserName;password=******;");
Use below connection string:
c = DriverManager.getConnection("jdbc:sqlserver://SERVERNAME:PORTNO;databaseName=DATABASENAME;
user=MyUserName;password=*****");
OR
c = DriverManager.getConnection("jdbc:sqlserver://SERVERNAME:PORTNO;databaseName=DATABASENAME",
MyUserName, MyPassword);

Access (.mdb) Database manipulation with jdbc

Background: Working on an application to be run on an Apache server hosted by Network Solutions. Friend/Customer insisted on using an Access Database instead of SQL database.
Current Problem: Wrote a Java test program to make sure I can connect to the database before I dive head first into writing the whole backend. When I run this code on the JVM of the apache server the final product will be hosted:
import java.sql.*;
import java.io.*;
public class test {
public static void main(String[] args) {
try {
Class driverClass = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
DriverManager.registerDriver((Driver) driverClass.newInstance());
// set this to a MS Access DB you have on your machine
String filename = new File(".").getCanonicalPath() + "/ITEMS.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim(); // add on to the end
// now we can get the connection from the DriverManager
System.out.println(database);
Connection conn = DriverManager.getConnection( database );
} catch (Exception e) {
System.out.println("Error: " + e.getMessage() + " " + e.getLocalizedMessage());
e.printStackTrace();
}
}
}
I get a null pointer exception on the line Connection conn= DriverManager.getConnection( database)
Here is the stacktrace:
java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.initialize(JdbcOdbcDriver.java:436)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:153)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at test.main(test.java:20)
To write this test I used this as my primary source: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2691&lngWId=2
String strconnect="jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ="
Sring filename="some path";
StringBuilder a =new StringBuilder();
a.append(strconnect);
a.append(filename);
String db = a.toString();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c= DriverManager.getrConnection(db,"","")
Statement stmt = c.createStament();
String query = "some query";
stmt.executeQuery(query);
c.close();
Never get canonical Path, Either Specify the path of the access file or use JFile Explore and get path
also don't forget to close connection
Since you mention apache, I will presume that the server is running Linux or BSD.
In that case, please have a look at the following:
Connecting to access database from linux
Good Linux ODBC drivers for Access are expensive! (US$850 for a single machine!)
There is no default drivers for Access provided on linux, and the JDBC ODBC won't work unless you install one.
Unless there is a very special and overwhelming good reason for using an Access database as the backend to a website on a linux server, tell your friend that this is not technically or even economically coherent.
If you need a lightweight database on the server, use SQLite: it's free, there is good support for connecting to it from Java in linux, there is good tooling (even browser plugins), and you can always convert that database to Access if your friend want to get an Access backup once in a while.
Otherwise, go for PostgreSQL or MySQL like everyone does (generally for a good reason).

Java MSAccess DSN Less

So the problem is that I want to make a connection to a msaccess database that has password every time you open it.
The password works if I directly open the access file.
I can make the connection if I remove the password, which means my code works if there is no password involve
The password was set using Set database Password in the database tools
MS Access 2007(but i used the .mdb)
Here's the code
String dbFile = "db.mdb";
String connectionString = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
String driverID = ";DriverID=22;READONLY=true;pwd=qwer}";
if (CONNECTION == null || CONNECTION.isClosed()) {
dbURL = connectionString + dbFile.trim() + driverID;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
CONNECTION = DriverManager.getConnection(dbURL);
}
----------------------------------------------
Error Code : java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Not a valid password.
----------------------------------------------
I already tried adding parameters to the get connection but it did not work.
Please Help :)
There are 2 types of passwords for MS Access database files:
user passwords
database password
With a user password, you supply the user name and password in the connection string.
;User Id=admin;Password=;
For a database password, you need a different identifier in the connection string to distinguish the password as a database password rather than a user password.
;Database Password=MyDbPassword;
Personally I don't use a database password. That feature doesn't offer much in the way of security, so it seems like it's more trouble than it's worth.
Edit: I don't know if it's possible to supply a database password with an ODBC connection. All the connection examples I found used OLE DB when including a database password.
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\somepath\\mydb.mdb;" & _
"Jet OLEDB:Database Password=MyDbPassword;", "admin", ""
Perhaps it could work to switch from the current Access ODBC driver to the Microsoft OLE DB Provider for ODBC. Or maybe with the current ODBC driver if you switch
Database Password=MyDbPassword;
to
Jet OLEDB:Database Password=MyDbPassword;
I don't know. But seems to me the database password is just getting in your way here. You already know you can connect if you remove the password from the database.

Categories

Resources