I am new to Oracle connection manager. Can some help me with a Java Client code example to talk to a oracle database through Oracle connection manager.
Quote from oracle docs.
The Web server on which the Connection Manager is running is on host webHost and is listening on port 1610. The database to which you want to connect is running on host oraHost, listening on port 1521, and SID ORCL. You write the URL in TNS keyword-value format:
String myURL =
"jdbc:oracle:thin:#(description=(address_list=
(address=(protocol=tcp)(port=1610)(host=webHost))
(address=(protocol=tcp)(port=1521)(host=oraHost)))
(connect_data=(INSTANCE_NAME=orcl))
(source_route=yes))";
OracleDataSource ods = new OracleDataSource();
ods.setURL(myURL);
ods.setUser("scott");
ods.setPassword("tiger");
Connection conn = ods.getConnection();
The first element in the address_list entry represents the connection to the Connection Manager. The second element represents the database to which you want to connect. The order in which you list the addresses is important.
When your applet uses a URL such as the one above, it will behave exactly as if it were connected directly to the database on the host oraHost.
Related
I've got an Azure SQL Server database that I'm connecting to via JDBC, but want to connect instead to my SQL Server "localhost". In SSMS, I connect to localhost without needing a password. So, do I still need to enter a password in Java?
I have a code like this :
String connectionUrl =
"jdbc:sqlserver://etcetc.database.windows.net:1433;"
+ "database=med;"
+ "user=windersan#salemimed;"
+ "password=********;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
// + "hostNameInCertificate=*.database.windows.net;"
+ "loginTimeout=30;";
How do I change this to connect instead to localhost?
Just replace the etcetc.database.windows.net by localhost and replace the port number 1433 by the number that you are using.
I have used SQLServerDataSource class to make the work easier. You can also create a string URL and set it in the DriverManger.getConnection().
Try with this code :
SQLServerDataSource dataSource = new SQLServerDataSource();
dataSource.setUser("windersan#salemimed");
dataSource.setPassword("********");
dataSource.setServerName("localhost");
// set the port number of your system below.
dataSource.setPortNumber(1433);
dataSource.setDatabaseName("med");
dataSource.setEncrypt(true);
dataSource.setHostNameInCertificate("*.database.windows.net");
dataSource.setTrustServerCertificate(false);
Connection connection = dataSource.getConnection();
Please refer to this links down below for more info.
Microsoft Docs - ISQLServerDataSource Interface - This contains the list of methods that you can use to set the various properties in the datasource.
Microsoft Docs - How to work with the connection - This contains examples of the possible ways to connect to a SQL Server database.
the first line of your concatenated string contains the url etcetc.database.windows.net:1433 this is the location of the database server, and the bit you should change.
Also, it might be worth doing a google search on connecting to SqlServer with JDBC to see if there are any examples out there.
This is the code that I use to connect with the Oracle database that is on my pc:
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection co = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:etecsa", "system", "asd");
It works but if I change localhost for an ip it can not connect to the database. I already deactivated the firewall but nothing.
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection co = DriverManager.getConnection("jdbc:oracle:thin:10.8.6.50:1521:etecsa", "system", "asd");
Where can I configure the oracle database to accept the connection from a specific ip and not only from localhost?
Check the documentation for DB URL
jdbc:oracle:driver_type:[username/password]#database_specifier
so in your case (if etecsa is SID) the url will be #host:port:SID
jdbc:oracle:thin:#10.8.6.50:1521:etecsa
if etecsa is a service name then use #//host:port/service_name
jdbc:oracle:thin:#//10.8.6.50:1521/etecsa
I’m trying to connect to a MySQL database on my website from java.
Currently I’m getting a exception that says
Must specify port number after:”
I Google stack overflow and found the default MySQL port is 3306.
But I can't find any information about how I add it to my url, which now looks like
jdbc:mysql://http://www.findmeontheweb.biz/database name"+
“user=findmeon_bitcoin&password=password
code:
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
Connection connect = DriverManager.getConnection("jdbc:mysql://http: //www.findmeontheweb.biz//findmeon_bitcoin//"+ "user=findmeon_bitcoin&password=oreo8157");
} catch (Exception e) {
System.out.println("Exception...." );
}
1) Hope the space in your code was only a copy/paste error here
2) You need to remove the http in the mysql uri
Connection connect = DriverManager.getConnection("jdbc:mysql://http: //www.findmeontheweb.biz//findmeon_bitcoin//"+ "user=findmeon_bitcoin&password=oreo8157");
will be
Connection connect = DriverManager.getConnection("jdbc:mysql://findmeontheweb.biz/findmeon_bitcoin/"+ "user=findmeon_bitcoin&password=oreo8157");
And just in case you are running it on a custom port, you can specify port by
Connection connect = DriverManager.getConnection("jdbc:mysql://findmeontheweb.biz:3306/findmeon_bitcoin/"+ "user=findmeon_bitcoin&password=oreo8157");
replace 3306 with your custom port.
Also I hope that is not your real username and password!
Last time I used SQL with Java/Eclipse I had a SQL script linked to the Project.
This time, I connected to a different server using jdbc format (not localhost) and it connects correctly.
Now I need to get into this specific database named WInfo but I don't know how to do that.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
c = DriverManager.getConnection("jdbc:sqlserver://serverName:1433/;user=UserName;password=******;");
Use below connection string:
c = DriverManager.getConnection("jdbc:sqlserver://SERVERNAME:PORTNO;databaseName=DATABASENAME;
user=MyUserName;password=*****");
OR
c = DriverManager.getConnection("jdbc:sqlserver://SERVERNAME:PORTNO;databaseName=DATABASENAME",
MyUserName, MyPassword);
I am trying to connect to a MySQL Server using JDBC tool in java (using eclipse). I was just wondering how to enter 2 user/password combinations. The first one is the one I use to connect to the server(for example when I ssh into the server) and the second one I enter into phpmyadmin. As of now, I am putting in the phpmyadmin password only into the jdbc connection properties and it's not connecting. This is my current statement:
conn = DriverManager.getConnection("jdbc:mysql://[IP of server]:3306/[Database Table name]", "[UserName (same as phpmyadmin)]","[Password (same as phpmyadmin)]");
I am getting a
java.sql.SQLException: null, message from server: "Host '[My computer's full host name]' is not allowed to connect to this MySQL server"
I was just wondering if I needed to enter my server login/password (the one I use for ssh) as well in addition to the phpmyadmin username/pwd. I am new to JDBC and MySQL server, so I would appreciate any tips.
Just for background, I am able to connect successfully through ssh and I can login to the server through phpmyadmin.
Here's how you can create an account that can access your server from another client machine:
CREATE USER 'bobby'#'localhost' IDENTIFIED BY 'some_password';
That creates the user, and says he can connect from localhost.
If he is on the machine 192.168.0.5, you'd do something like this:
CREATE USER 'bobby'#'192.168.0.5' IDENTIFIED BY 'some_password';
Then of course, you have to grant privileges appropriately:
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'192.168.0.5' WITH GRANT OPTION;
That's been my experience anyway.
You're probably better off reading this section on how to specify MySQL accounts.
When you log in from PHPMyAdmin, the web server is located on the same server that hosts the Mysql database (in your case). Mysql, by default, does not allow connections from remote hosts; only the local machine may access the database.
I'll take a shot in the dark and say that the computer you're running the java code on is not the same machine that is hosting the mysql server. You must configure Mysql to allow connections from remote hosts via the config file and then change the Host row of the mysql.users table for the specified user to allow connection from your IP address (or, if security isn't your concern, any IP address.)
To configure mysql to allow connections from remote hosts you must remove the "bind-address=" line from the configuration file.
To allow any host to log on to a specific mysql user, you must set the mysql.users Host` column to "%".
Both of these must be done.
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}