1

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.?

4
  • You are making a huge assumption: that SQLServer and Microsoft Access are exactly the same. But they are nothing alike at all. Commented Dec 4, 2013 at 17:43
  • oh ...Now it is the mistake I have done?. For the end user whom i am going to deliver the java application should not require sqlserver to be installed Commented Dec 4, 2013 at 18:05
  • Then you need to provide an SQLServer instance somewhere that can be reached externally. Or else an SQLServer WILL need to be installed locally somewhere. Or not use SQLServer but some embeddable database solution such as H2. Commented Dec 5, 2013 at 10:13
  • Thanks Gimby. I shifted to HSQLDB for embeddedable database solution Commented Dec 13, 2013 at 13:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Iam not sure how this will take the localdb named DB1.mdf inside the project folder since it doesnt specify the name of the db file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.