i am trying to implement java RMI client server connection, i made my server in NetBeans IDE and client class made in oracle database as java store procedure and use this via oracle function in my PLSQL block, everything is working fine but I stuck in little issue below:
I made this java store procedure in SYSTEM user and give dbms_java.grant_permission('SYSTEM','java.net.SocketPermission','192.168.43.25:*','connect,resolve') to SYSTEM user via SYSDBA, "*" is used for all available ports. I get perfect response from my server but till specific session expiry.
Whenever I logout from SYSTEM user and login back again, its again throws me an exception the Permission ("java.net.SocketPermission" "192.168.43.25:56792" "connect,resolve") has not been granted please guide me how can I get rid from this repeated task? because in development environment I'll manage it but how can deploy it on production with this issue. My Environment is Oracle DB 18c XE, Thank You!
Following is my java store procedure:
create or replace and compile java source named rmi as
package rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.Naming;
import java.security.Policy;
public class RmiClient {
public RmiClient(){
}
public static String getRequestFromClient(){
Policy.setPolicy(new MyPolicy());
String result = "";
try {
IServer server = getServer();
result = server.getRequestFromClient();
} catch (Exception ex) {
result = ex.getMessage().toString();
}
return result;
}
private static IServer getServer() throws Exception {
Parameter configurationParameters = new Parameter();
IServer server = (IServer) Naming.lookup( configurationParameters.objectName);
return server;
}
}
Granting permissions with DBMS_JAVA is a little different than a typical Oracle grant. After granting a permission using DBMS_JAVA.GRANT_PERMISSION, you will need to COMMIT at some point after the grant as been executed for it to take affect permanently. Notice how there is a COMMIT after the DBMS_JAVA call in the Oracle documentation.
Related
A few classmates and I are creating a Java project which requires a database. I have created a connection in MySQL and connected it to my Java project successfully using the following Connect class:
package com.example.javaworkoutgame.Model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect {
static Connection con;
public Connect() {
connect();
}
// attempt to connect to MySQL database
public static void connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded Successfully");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lab3", "root",
"**********"); // not the actual password
System.out.println("Successful Connection");
} catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
}
This code runs properly on my machine.
I committed and pushed the code to Bitbucket so my partners could access it. However, when they run the code on their computers, they get the following error message:
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
Is there something I need to change in MySQL workbench in order for other people to be able to access the database? I could not find any information on this.
The only thing I was able to try was found at this thread:
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
I opened a new .sql file and tried running the command:
GRANT ALL PRIVILEGES ON . TO 'root'#'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;
(I replaced '%password%' with the actual password)
When I tried that I got the following error message:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY '*********' WITH GRANT OPTION'
No, and you need to stop this line of thought and do some research first.
Your current configuration says that the mysql server is on the very same physical machine that the code is running on. You installed mysql on your dev machine, your friends need to install it on theirs, and each has their own unique database (nothing is shared).
You could, instead, take your mysql server, open it up to the world (which, for virtually all ways internet is made available in residential connections, requires messing with your router to 'open a port').
But then you have an open mysql server, and the username and password are on a public bitbucket site.
It also requires either a permanent IP (which few residential internet providers offer) or a dyndns service. More generally, hosting open MySQL servers that see lots of traffic gets your internet shut down, for good reason. You'd end up hosting a whole bunch of hackers. All hardware in your network will be p0wned and turned into bot nets. Hence, very very bad idea.
Good ways to solve this problem:
Everybody installs their own MySQL server. This is sensible; you're writing code and bound to make mistakes, it'd be real bad if all code you write is first-run and tested on live data. You don't want one of your friends to wipe your database. If you need some initial data to test with, set it up properly, and read up on how to make an SQL dump. With such a dump file you can reset any mysql server to that exact state - and that'd be how you and your friends develop: Set up the DB to be in that known state, write some code, and if you ruin the db by doing so, no problem. Just reset it again.
Set up a VPN between your friends. NOW you can share the IP your system has within the VPN (it'll be 10., 172.16., 192.168.* - if it's 127.0.0.1, it's localhost, i.e. everybody needs to install mysql on their own and nothing is shared, and if it's anything else, you're opening it to the world, which you don't want to do). Do not put the VPN username/password info anywhere in that bitbucket. And you need to trust your friends.
You should have a properties type file so that each person who is going to interact with the code has their local data without the need to replicate yours, in the same way you can have different values in the properties for test or production environments.
example of a property file:
system.properties
#BD
db.driver=com.mysql.cj.jdbc.Driver
db.user=user
db.pass=password
db.server=server_IP
db.port= port_IP
db.db = DB
Then you should have a procedure to read from java the properties inside the file
Utils.java
package com.example.javaworkoutgame.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public final class Utils {
public static Properties getProperties() {
String path = String.format("PATH to your properties FILE/system.properties",
System.getProperty("user.dir"));
Properties properties = new Properties();
try (InputStream is = new FileInputStream(new File(path))) {
properties.load(is);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return properties;
}
}
And finally you make a call to the function that gets the properties from your connection class
Connect.java
package com.example.javaworkoutgame.Model;
import com.example.javaworkoutgame.util.Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect {
Properties properties = Utils.getProperties();
static Connection con;
public Connect() {
connect();
}
// attempt to connect to MySQL database
public static void connect() {
try {
String driver = properties.getProperty("db.driver");
String ip = properties.getProperty("db.ip");
String port = properties.getProperty("db.port");
String db = properties.getProperty("db.db");
String user = properties.getProperty("db.user");
String pass = properties.getProperty("db.pass"):
Class.forName(driver);
System.out.println("Driver Loaded Successfully");
con = DriverManager.getConnection("jdbc:mysql://"+ip+":"+port+"/"+db, user,
pass);
System.out.println("Successful Connection");
} catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
}
About the MYSQL error, if your partners do not have a local mysql environment with the same values as you, they will experience the error you describe, since your configuration is a local configuration, if you need your partners to connect to your pc, you must open the ports of mysql and give them your public IP (not recommended)
I hope this answer helps you!
I got a google cloud platform - compute engine instance, which I installed MySQL server on.
And now I can't get any signal of life our of the VM the sql installed on,
for exsample:
package com.company;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void connection(){
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("in conncection");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void connectToMySQL(){
connection();
String host = "jdbc:mysql://hotsIP:3306/DBname";
String user = "user";
String pass = "password";
try {
Connection connection = DriverManager.getConnection(host,user,pass);
System.out.println("???");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
connectToMySQL();
}
}
It's take a few second like he trying to connect and the EXEPTION
in conncection
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
What I done to make it work:
in the my.conf:bind address = 0.0.0.0, skip-external-locking comment out
restart the server
looked if the server is active
looked if the server listening to the port
looked if its TCP
I don't know what to do anymore.
You have to make the following change to your my.cnf file
my.cnf
bind-address = www.000webhost.com (OR)
bind-address = xx.xx.xx.xx (IP Address)
You need to restart your MySQL service, once this setting is changed.
Also worth noting is the point that MAMP/ MAMP Pro sets MAMP_skip-networking_MAMP by default. You've to disable this line in your my.cnf
And if you don't have any user login issues, you should be able to connect to the MySQL Database from your Java code.
In my case the root cause was: Firewall. I was trying to run the application at work.
What was interesting is that the App Engine Standard running locally actually generated a non-error log in Google Cloud Platform Logs, making me discard the firewall hypotheses.
Solution: I found out bringing my notebook from home and connecting to company's network, did not work. When I connected to the shared connection in my mobile, worked perfectly.
I am trying to use Sqoop 2 to import data from a MySQL database to HDFS, basically following the instructions here. However, the Sqoop server is unable to make a connection to the MySQL database due to appropriate drivers not found.
Setup:
Here is some background on my setup:
Hadoop cluster: I have a three machine Hadoop cluster running CDH 4.4.0. Sqoop 2 was configured through the Cloudera Manager, and is running on the same machine as the Namenode.
I am developing on a Windows machine which is also where my MySQL database lives. The Hadoop cluster is a set of three Ubuntu Server machines.
MySQL database: I have a MySQL database running on my Windows machine, and I have checked that the MySQL database can be accessed from each of the machines in my Hadoop cluster.
Client application: My client application is an Eclipse project on my Windows machine which basically opens up a Sqoop client corresponding to a Sqoop server (I have verified that the Sqoop server and client are running on my Namenode).
Here is the main class of my client application.
package com.fc.SqoopImport;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.sqoop.client.*;
import org.apache.sqoop.*;
import org.apache.sqoop.common.*;
import org.apache.sqoop.model.*;
import org.apache.sqoop.validation.Status;
import com.mysql.jdbc.*;
public class SqoopImport {
// utlity function to cycle through the connector and framework forms for errors
private static void printMessage(List<MForm> formList) {
for(MForm form : formList) {
List<MInput<?>> inputlist = form.getInputs();
if (form.getValidationMessage() != null) {
System.out.println("Form message: " + form.getValidationMessage());
}
for (MInput minput : inputlist) {
if (minput.getValidationStatus() == Status.ACCEPTABLE) {
System.out.println("Warning:" + minput.getValidationMessage());
} else if (minput.getValidationStatus() == Status.UNACCEPTABLE) {
System.out.println("Error:" + minput.getValidationMessage());
}
}
}
}
public static void main(String[] args) throws Exception {
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
// location of the server running Sqoop 2 server
String urlSqoop2Server = "http://fc-01.fc.com:12000/sqoop/";
SqoopClient clientSqoop2 = new SqoopClient(urlSqoop2Server);
// dummy connection object
MConnection sqoopConnSAP = clientSqoop2.newConnection(1);
MConnectionForms sqoopConnSAPFrameworkForm = sqoopConnSAP.getFrameworkPart();
MConnectionForms sqoopConnSAPConnForm = sqoopConnSAP.getConnectorPart();
sqoopConnSAP.setName("SqoopConnSAP");
// Set the values for the connection form
sqoopConnSAPConnForm.getStringInput("connection.connectionString").setValue("jdbc:mysql://192.168.31.172:3306/dbsap");
sqoopConnSAPConnForm.getStringInput("connection.jdbcDriver").setValue("com.mysql.jdbc.Driver");
sqoopConnSAPConnForm.getStringInput("connection.username").setValue("root");
sqoopConnSAPConnForm.getStringInput("connection.password").setValue("1234");
sqoopConnSAPFrameworkForm.getIntegerInput("security.maxConnections").setValue(10);
Status statusConnSAP = clientSqoop2.createConnection(sqoopConnSAP);
if(statusConnSAP.canProceed()) {
System.out.println("Created. New connection ID: " + sqoopConnSAP.getPersistenceId());
} else {
System.out.println("Check for status and forms errors.");
printMessage(sqoopConnSAP.getConnectorPart().getForms());
printMessage(sqoopConnSAP.getFrameworkPart().getForms());
}
}
}
Error:
Running this project gives the following error:
Check for status and forms errors.
Form message: Can't connect to the database with given credentials: No suitable driver found for jdbc:mysql:192.168.31.172:3306/dbsap
Error:Can't load specified driver
Diagnosis:
The appropriate JDBC drivers (mysql-connector-java-5.1.26-bin.jar) is part of my Eclipse project, and for good measure, I have added this to the sqoop2 lib folder
/opt/cloudera/parcels/CDH-4.4.0-1.cdh4.4.0.p0.39/lib/sqoop2/client-lib
as well. However, this is the part I am not sure of, since the CDH4 documentation says]1 that in case Sqoop was installed using Cloudera Manager, the location of the appropriate JDBC driver should be added to HADOOP_CLASSPATH. So, I did
export HADOOP_CLASSPATH=/usr/lib/jdbcJars:HADOOP_CLASSPATH;
on my Hadoop Namenode, so that an echo $HADOOP_CLASSPATH gives /usr/lib/jdbcJars. Again, I am not entirely sure of the utility of this since my client application is not being developed on the Hadoop cluster.
The last thing that I have not tried yet is creating a new /usr/lib/sqoop/lib directory and adding the JDBC driver there.
Any help figuring this out would be appreciated.
Never ever alter content of parcel directory (/opt/cloudera/parcels/*). There are always different ways how to configure components. For example based on the official documentation, you need to copy the MySQL JDBC driver into /var/lib/sqoop2 directory on the node where you are running Sqoop2 server.
put the mysql-jdbc-driver into the dir:
/usr/lib/sqoop2/webapps/sqoop/WEB-INF/lib/mysql-connector-java-5.1.25.jar
and restart the sqoop2 server
I am trying to connected with my provider by the help of these lines of code:
import javax.telephony.*;
import javax.telephony.Phone.*;
import javax.comm.*;
import Phone.ProviderService;
public class terminals
{
private Address origaddr;
private Provider myprovider;
public void getTerm()
{
try
{
JtapiPeer peer = JtapiPeerFactory.getJtapiPeer(null);
myprovider = peer.getProvider(null);
System.out.println("Provider is " + myprovider);
} catch (Exception e)
{
System.out.println("Can't get Provider: " + e.toString());
System.exit(0);
}
}
}
On executing this code, system is not able to get Provider. When I am using gjtapi-1.8.jar in the library then it is showing "net.sourceforge.gjtapi.GenericProvider#53c015" as the provider. I have added log4j-1.2.12.jar, jtapi-1.3.1.jar, gjtapi-tapi3-1.9-rc1.jar, Gjtapi-1.8.jar, log4j.properties file in library to make it. Its running but i want to connect it with my mobile service provider. I am using MTNL (through Serial prot) mobile service provider in Delhi (India) and MTNL braodband connection (through LAN). Please suggest me how to proceed.
"javax.telephony.JtapiPeerUnavailableException: JtapiPeer: DefaultJtapiPeer could not be instantiated. at javax.telephony.JtapiPeerFactory.getJtapiPeer(JtapiPeerFactory.java:135) at Phone.Outcall.main(Outcall.java:24)" is the stack trace thrown when I am removing gjtapi-1.8.jar file from the library. And when i am adding this file in the library, it is showing provider as "net.sourceforge.gjtapi.GenericProvider#53c015", and the call is not connecting to any mobile no. There is some more classes on which I am working, taken from "jtapi-1_4-fr3-spec>javax>Telephony>package.html" file.
Those files are OutCall.java & MyOutCallObserver.java on which I am working.
In other implementations of JTAPI (Cisco for example), you have to pass a provider connection string into the getProvider method:
provider = peer.getProvider("host;login=username;passwd=password;appinfo=MyApp");
So people are probably going to tell me this is a bad idea, but I'd like to at least give it a go.
EDIT The intention of this app is that it can only work when the device is part of the same network the oracle db is on or is connected to the network via VPN. The information in the database is not going to be globally accessible, which is why I will need direct connection to the oracle db.
Now according to this thread
Connecting the oracle in android application
He was successful in querying the oracle db.
So I have a fairly basic class that when initialised will try to get a connection to my database.
package com.producermobile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.util.Log;
public class ConnectOra {
private Connection conn;
private Statement stmt;
public ConnectOra() throws ClassNotFoundException {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:#x.x.x.x:1521:PR10";
this.conn = DriverManager.getConnection(url,"xxx","xxx");
this.conn.setAutoCommit(false);
this.stmt = this.conn.createStatement();
} catch(SQLException e) {
Log.d("tag", e.getMessage());
}
}
public ResultSet getResult() throws SQLException {
ResultSet rset = stmt.executeQuery("select customer from customers");
stmt.close();
return rset;
}
}
And in my main activity onCreate method I have this
#Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
ConnectOra db = new ConnectOra();
ResultSet rs = db.getResult();
ArrayList<String> list = new ArrayList<String>();
while(rs.next()) {
list.add(rs.getString(1));
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
} catch(Exception e) {
}
}
In Eclipse I add the external ojdbc14.jar file to the build path.
However when I run
this.conn = DriverManager.getConnection(url,"xxx","xxx");
I receive the following exception
"Io exception: The Network Adapter
could not establish the connection"
If however I create an instance of this class inside a standard java app with a main method, the connection works. Any ideas?
Oracle has a product that provides both data sync with Oracle DB as well as offline capabilities; it's called Mobile Server
They way it works is, you just store your data in SQLite, and the Oracle client will handle sync. Of course you have to install and configure mobile server, and each new device will need to be provisioned, but once that is done, it "just works!"
There is a download tab in the link above, you can download and try it out of you like.
I hope that helps. Good luck with your problem.
Regards
Eric, Oracle PM
:) yes, I'm one that'll tell u it is a "bad" idea. IMHO, given that Android apps are intended to run on mobiles where connectivity might be an issue or be lost temporarily, I claim each good app should have some degree of offline capabilities. So you'd implement some very basic sync mechanism - as for instance demonstrated with the SampleSyncAdapter - which synchronizes with the apps local SQLite db.
I think this is the best way to go (also for the user experience).
Have you added in the AndroidManifest.xml the
uses-permission android:name="android.permission.INTERNET"
I've to create an app with the same constraint of connectivity and database. I've done this, and I've a lot of Exception (ArrayIndexOutOfBound during the connexion to the database.)
I use ojdbc14.jar for that. So, I "just have to" fix the Exception I have, and it would be OK...
For more information, see this topic
Sorry to revive an old thread but I had this problem for a long time and finally fixed it. Just run eclipse or whatever you're developing in "as administrator" and the error will go away.
Another and much easier approach is to use a Virtual JDBC Driver that relies on a secure three-tier architecture: your JDBC code is sent through HTTP to a remote Servlet that filters the JDBC code (configuration & security) before passing it to the Oracle JDBC Driver. The result is sent you back through HTTP.
There are some free software that use this technique.
Just Google "Android JDBC Driver over HTTP".