Well I'm working on a GNU/Linux machine and I'm still learning how to connect to my database
I've got the Connector/J File downloaded and edited my CLASSPATH, here's the reusult of echoing the path
/home/user/Connector-J/mysql-connector-java-5.1.30/mysql-connector-java-ver-bin.jar:
I also have my SQL File.. let's say: sqlfile.sql
Threw the documentation and while searching I found how I should connect to the Connector.. My question is: where should I put my SQL file?
Also, Here's a piece of code I found that is used to connect, is it right?
String userName = "root";
String password = "password";
String url = "jdbc:mysql://localhost/somefile";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
To build an application that connects to a database, you will need:
A database engine (e.g. MySQL, SQL Server, Oracle, etc)
A JDBC driver library (e.g. Connector/J for MySQL, jTDS for SQL Server, ojdbc for Oracle, etc)
Java application itself.
Your code is only creating connection. Even that, the url looks incorrect. The "somefile" should be replaced with the name of database schema you want to connect to.
To interact with database, creating connection is still far from done.
To read, insert, update, delete, etc records from/to database, you need to create statement object by passing your SQL query.
The SQL queries should not be put in a file, but written in your code instead.
Complete JDBC tutorial is here: http://docs.oracle.com/javase/tutorial/jdbc/
Related
I have done Java application using Netbeans and Sqlserver 2012.
So for developing the application i ran Sqlserver in particular port and used the below string for connecting.
public static Connection connectDB(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost\\SQLEXPRESS:1433;databaseName=DB1;user=zubair;password=zubair1234");
//JOptionPane.showMessageDialog(null,"Connection established");
return conn;
}catch (Exception ex){
JOptionPane.showMessageDialog(null,ex);
return null;
}
}
Now for delivering the java application , I need to make the sqlserver db to be run locally within the application. Iam not an expert in java so little help i require to change the connection string.
For access db i know we can use "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+accessFileName+".accdb;";" this string and give the filename of the Db.
Is there any similar string for sqlserver db. I have copied the DB to my project folder.?
Connect to the database on the local computer by using a username and password:
jdbc:sqlserver://localhost;databaseName=DbName;user=MyUserName;password=*****;
Please read Building the Connection URL and follow the guidelines.
I don't have any running example with me.
But will surely post after trying it myself.
Secondly, I strongly recommend using properties files to store connection details.
Prefer XML over prop files.
I have a remote MySQL database that I can connect to with MySQL Workbench (screen shot below), but I ultimately need to connect to it via JBDC and everytime I try to connect, it throws an exception. I'm new to this, so could anyone provide me some insight on what could be wrong?
String host = "testdb.db.10682960.hostedresource.com";
String datab = "testdb";
String url = "jdbc:mysql://" + host + ":3306/" + datab;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, datab, "password");
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
Edit: Not sure if this is relevant or not, but I'm running this on Android.
It looks like you're missing a user name. Try adding "?user=testdb" to the end of your url.
Edit: I didn't realize this was an Android app. Technically, it should be possible, but not advisable. Your network connection would be much less reliable, and you would be pushing server credentials out to your client. Use of a REST API to communicate from your Android to a web server in the same data center as the database is a much safer option.
I don't have experience trying this from Android... so I don't know what exception would be thrown if, for example, you can't reach port 3306 from the Android's connection to the network. Even if you get it working, though, do look into using a web service instead.
According to the jTDS FAQ (http://jtds.sourceforge.net/faq.html) the format of a JDBC URL is:
jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
If I were you, I would replace the '?' with a ';' to adhere to the above format. The "query string" is not like that of a "normal" URL.
This, of course, assumes that you are using the open source JDBC connector found via the link above.
i'm trying to connect to *.dbf (dBase III) file on my Java application, running on a Windows Server 2003 system.
I'm encountering this error and I cannot really understand the meaning (sources for OdbcJdbc.java seems to be unavailable):
[Microsoft][ODBC dBase driver] '(unknown)' is not a valid path error
This is the code I run on my application:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:DRIVER={Microsoft dBase Driver(*.dbf)};DBQ=D:\\dbNeri\\CARISTAT;";
Connection conn = DriverManager.getConnection(database);
Statement s = conn.createStatement();
String selTable = "SELECT * FROM CARISTAT";
Does it exists a JDBC driver able to connect to dBase files or do I have to import external libraries to do the magic?
Thanks in advance for your help!
Ok guys, I finally found the answer to all my problems.
Without any need to configure a ODBC data source, the mistake in that code was I've targeted directly in my connection string the file name I would like to access.
(In the code up here, I removed "\CARISTAT").
Thus the application run easily and with no more JDBC driver error!!
Thanks anyway for your support!
Are you able to set up a DSN for that database (using ODBC Administrator)? Maybe you just don't have the dBase ODBC drivers installed on that server?
Dbase III is a 16-bit product and Windows Server is a 64-bit environment, which is what causes the compatibility issue.
You will have to use dBDOS to use your DOS based dBase to run our dBase applications on 64-bit platforms.
These sites have more information:
http://pmcgee#dbasellc.com
http://www.dbase.com
I am using the jTDS driver in order to connect to an SQL Server database from my Android application, which uses the Windows Authentication. As advised in the FAQs, I read the READMESSO file and as told, I placed the native SPPI library (ntlmauth.dll) in the system path (defined by the PATH system variable)
However, when I try to connect to the database using the following code:
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
String connString = "jdbc:jtds:sqlserver://192.168.56.1/MyMovies;";
Conncection conn = DriverManager.getConnection(connString);
I get the following exception:
java.sql.SQLException: Single-Sign-On is only supported on Windows. Please specify a user name.
Since you are connecting from an android device, you would not be able to get the SSO credentials required by the driver to connect to SQL server. The setting you referred to works only if the java program trying to connect to the DB is on a windows machine, which is clearly mentioned by the error message.
Unless your application has authorization based on the SSO user connecting to the DB, you should have an SQL Server user-based authentication mechanism to connect to the server and all authorization procedures should be tied to this user.
You might have to give the username also.
"jdbc:jtds:sqlserver://192.168.56.1/MyMovies;instance=SQLEXPRESS;user=foo"
I have a derby database that is deployed along with my webapp to WEB-INF/classes/myDb
What should my jdbc.connection url be to connect so that I can write to the database?
I am trying
jdbc:derby:myDb;
and it can not find the database. I need to be able to modify the database. If i put classpath:myDb, it finds it, but it is unfortunately read only per the derby docs.
i solved it by setting my jdbc connection url at runtime and using:
StringBuilder derbyUrl = new StringBuilder("jdbc:derby:");
derbyUrl.append(servletContext.getRealPath("/"));
derbyUrl.append("/WEB-INF/classes/myDb;");
dataSource.setUrl(derbyUrl.toString());