Reputation: 1391
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.
Upvotes: 0
Views: 10077
Reputation: 3109
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):
Enable the TCP/IP for the conn. instance (this is by default is disabled particularly on sql express editions):
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;
}
Upvotes: 2
Reputation: 174
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());
}
}
}
Upvotes: 0
Reputation: 2393
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.
Upvotes: 1