Java connection string for sqlserver for localdb - java

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.

Related

How do I use ucanaccess to access a remote server for a java interface / how do I find my remote SQL Server file path?

I know that the code below works for a Microsoft Access Database but I need to switch it to my remote SQL server.
try {
String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
Class.forName(driver);
conn = DriverManager.getConnection("jdbc:ucanaccess://???SQLSERVERPATH???");
} catch (Exception e) {
System.out.println(e);
}
So where do I figure out what the server path is ?
The UCanAccess JDBC driver is only designed to work with data stored in Microsoft Access datbase files (.accdb, .mdb). It cannot be used to work with data in a SQL Server database. You will need to use a SQL Server JDBC driver for that (e.g., this one).

Connecting Java application to a database on a remote server

I am developing a Java application that needs a database. I know how to connect my application to a database that is stored in my local hard disk, but I need help to connect to a remote database.
String host="jdbc:derby://localhost:1527/Test";
Connection con=null;
String uname="admins";
String pass="admins";
try {
con=DriverManager.getConnection(host, uname, pass);
} catch (SQLException ex) {
}
The above code allows me to connect to a local database. What changes should I apply to connect to a database that is stored in a remote server?
Syntax for Derby JDBC database connection URL for the network client driver:
jdbc:derby://server[:port]/databaseName[;attribute=value]*
By defaultthe port is 1527 if omitted. suppose the user abc with password xyz wants to connect to the remote database remotedb on the server dbserver, use the following URL:
jdbc:derby://dbserver/remotedb;user=abc;password=xyz
One more thing as Russell Uhl said in his comment first make sure the remote server accepts connection

NetBeans with Connector/J

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/

Can connect to remote database through MySQL Workbench, but not Java JBDC

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.

Cannot connect to Mysql database using Java

I am running the following code on a Windows Server box using Java on Eclipse.
Connection conn = null; // connection object
Statement stmt = null; // statement object
ResultSet rs = null; // result set object
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/errorcodes", "myusername", "mypassword");
System.out.println ("Database connection established");
}catch (Exception e){
System.err.println ("Cannot connect to database server");
}
And i keep seeing the "Cannot connect to database server error". Any ideas what i might be doing wrong?
I have tried netstat -an and i see :
TCP 127.0.0.1:4464 127.0.0.1:3306 Established
My guess? You haven't got the mysql jdbc connector jar in your classpath. It should be called something like mysql-connector-java-5.1.16-bin.jar, depending on your version of mysql
If you don't have that jar, visit here
Do you sure that it is mysql running on port 3306 and that it's version is supported by your connector/j?
i think you did not start the MySql server in your PC.before running your application
Try the followings:
Clean and rebuild the project in eclipse.
Try to access the mysql database using the username and password in command prompt to ensure the username and password are correct.
If you want to ensure the username and password , you have to query the user table in mysql table again to need another mysql admin account to query.
It is simple .. you need .jar file called mysql-connector-java-5.1.16-bin.jar ,,, download it and add it to your libs ...
good luck !!!

Categories