1

I made a Java application to connect to a MySQL database. The connection was made ​​in this way:

public class Connection {
 public static Connection getConexao() throws SQLException {
 try {
 Class.forName("com.mysql.jdbc.Driver");
 //System.out.println("Conectado");
 return DriverManager.getConnection("jdbc:mysql://localhost/world","root", "rootadmin");
 } catch (ClassNotFoundException e) {
 throw new SQLException(e.getMessage());
 }
 }
}

Now I needed to change the connection from MySQL to Microsoft SQL Server 2012.

Can anyone help me change the connection to the database?

Thank you all very much.

asked May 14, 2014 at 14:55
5
  • Are you sure you want to access the DB from an applet? An applet runs in the browser. I doubt that the browser of your users are running on the same machine as your db server. Commented May 14, 2014 at 15:06
  • Thanks for reply.Yes, i'm sure. My application will be run on a server. But i dont no how do the conection :S Commented May 14, 2014 at 15:14
  • @user3320956 like jdiver said, applets don't run on the server, they run on the (client) user's computer Commented May 14, 2014 at 17:42
  • Do you have a java applet or an application? This are completely different things. Commented May 15, 2014 at 7:11
  • I have an application :S Commented May 15, 2014 at 7:46

3 Answers 3

2

This answer is presented for next visitors on this kind of question. Configuring the java driver connection for SQL Server can be quite confusing for new users. I'll guide you here through SQL Server Management Studio (SSMS):

There're 2 kinds of authentification accepted on SQL Server. They are Windows & SQL Server authentification. In this answer I'll active "sa" (syst. administrator) account for quick setup demonstration over the connection.

To enable the "sa" account (you can skip this if it had already been there):

  • Login as usual using default window authentification mode
  • Right click on the server name (i.e MYCOMPUTER223\SQLEXPRESS) > Security > go enable the SQL Server & Window authentification mode > ok
  • On the left tree menu, click Security > Logins > right click that "sa" > Properties > set up your "password" for this "sa" account
  • and then on the left menu there is the "Status"> enable the "Login:"
  • restart the SQL Server service
  • now login as "sa" through "SQL Server authentification mode" on the SMSS . Using the password we've just set up.

Enable the TCP/IP for the conn. instance (this is by default is disabled particularly on sql express editions):

  • Open "Sql Server Configuration Manager". This is installed along the installation of SQL Server engine.
  • "SQL Server Network Configuration" > "Protocol for SQLExpress" > enable the "TCP/IP"
  • right click that "TCP/IP" > "IP Address" > scroll down till you find "IPAll" and then just fill the "port" field with 1433

You can now use this credential for the SMSS:

username : sa
 password : ...the password you've just set up above..

Or you can now use this credential on your external java based clients/data or BI tools/sql management tools such as Pentaho, Heidi SQL, DB Weaver or any particular java framework conn. manager descriptor, etc. :

hostname : localhost (or any custome host domains)
database name : your database name..
instance name : i.e SQLEXPRESS (this can be found through the SMSS, right click the server name > view connection properties)
port : 1433
username : sa
password : ...the password you've just set up above..

or via url/uri for the Java connection manager/factory:

 String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=Yourdatabasename;user=sa;password=yourSApassword";
public Connection createConnection() throws NoSuchAlgorithmException { 
 System.out.println("Creating SQL Server DataBase Connection");
 Connection connection = null; 
 try { 
 // Provide the java database driver
 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
 // Provide URL, database and credentials according to your database
 // .getConnection ("url/namadatabase, user, password")
 String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=DummyDatabase;user=sa;password=YourSAaccountpassword";
 connection = DriverManager.getConnection(Connectionurl);
 } catch (Exception e) {
 e.printStackTrace();
 return null;
 }
 if (connection != null) {
 System.out.println("Connection created successfully..");
 }
 return connection;
 }
Tony Wu
1,13719 silver badges29 bronze badges
answered Sep 22, 2020 at 19:34
Sign up to request clarification or add additional context in comments.

Comments

1

First of all you will need JDBC drivers for MS SQL Server. Either from Microsoft or there are other options like jTDS.

Then you should use a connection string like jdbc:sqlserver://ServerName:Port;databaseName=;user=username;password=password;

Of course your SQL Server should be in mixed mode so you can connect with username and password created on server.

Applets run on users' computer, therefore you should open your SQL Server ports to all visitors which is a BAD idea.

answered May 14, 2014 at 15:03

Comments

0
Make database URL like :
jdbc:mysql://IP address:DatabasePort/DatabaseName,username, password 
public class Connection {
public static Connection getConexao()throws SQLException{
 try{
 Class.forName("com.mysql.jdbc.Driver");
 return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/world","root", "rootadmin");
 }catch(ClassNotFoundException e) {
 throw new SQLException(e.getMessage());
 }
}
}
answered May 14, 2014 at 17:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.