JDBC connection from eclipse not working, but working in Oracle SQL developer - sql

I'm trying to connect into DB via JDBC
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:#host_name:port:service_name"; //Changed the host_name, port and service_name for confidentiality
String username = "user_name"; // Changed the user_name
String password = "my_password"; //Changed the password
Connection conn = DriverManager.getConnection(url, username, password);
Got the below exception
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
But at the same time, I could connect via Oracle SQL developer.
Note- My service_name consists of "." and "_" and My host_name contains "."
Please help here!

//Step: 1
Load Driver Class
ex- Class.forName("oracle.jdbc.driver.OracleDriver");
//Step: 2
String url="jdbc:oracle:thin:#ipaddress:portNo of oracle :database name";
ex- String url="jdbc:oracle:thin:#localhost:1521:XE";
//Step: 3
User Name
String username = "user_name";
ex- String username = "system";
//Step: 3
Password
String password = "oracle_db_Password";
ex- String password = "sys123";
//Step:4
Connection con=DriverManager.getConnection(url,username,password);

Below is the right way to create data source object from oracle documentation:
//specify the datasource object
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:#//myhost:1521/orcl");
ods.setUser("scott");
ods.setPassword("tiger");
Connection conn = ods.getConnection();
I hope you are using it right way

Related

Send files from one remote server using JSch to another server using JSch

Please refer to an existing question regarding the same
Send files from one remote server using JSch to another server using JSch too
public boolean uploadFile() throws JSchException, SftpException {
ChannelSftp channelSftpA = createChannelSftp();
ChannelSftp channelSftpB = createChannelSftp();
channelSftpA.connect();
channelSftpB.connect();
localFilePath = "/data/upload/readme.txt";
remoteFilePath = "/bingo/pdf/";
channelSftpA.cd(localFilePath);
channelSftpA.put(localFilePath + "readme.txt", remoteFilePath + "readme.txt");
But it doesn't work. Should I put channelB.put into my first channelA.put?
Solution given for above Question is "for transferring file you should get file from server A, and that put on server B. By the way users under which you are going to download and upload files should have access to specified folders!"
private boolean transferFile() throws JSchException, SftpException {
ChannelSftp channelSftpA = createChannelSftp();
ChannelSftp channelSftpB = createChannelSftp();
channelSftpA.connect();
channelSftpB.connect();
String fileName = "readme.txt";
String remoteFilePathFrom = "/folderFrom/";
String remoteFilePathTo = "/folderTo/";
InputStream srcInputStream = channelSftpA.get(remoteFilePathFrom + fileName);
channelSftpB.put(srcInputStream, remoteFilePathTo + fileName);
System.out.println("Transfer has been completed");
channelSftpA.exit();
channelSftpB.exit();
return true;
}
But My query is
what if I want to directly transfer files from server1 to server2 without getting it on my local. This I can do using command "scp -rv /sourcefolder/text1 username#server2Hostname /destinationfolder/" but through Program it will work ONLY when public key is set up between server1 and server2 SO no Requirement to pass the password. But I need a solution when public key is Not set up and I have to use the Password, Am not able to figure out how to pass the password for server2 in this command OR if we can create parallel session/channel to server1 and server2 NOT Sure how can we do that. ANy Idea ?

Error while connecting to SQL Server. The server was not found or was not accessible

I am trying to connect to my SQL Server database. I am using C#. Here is my code:
string connectionString = #"Server = tcp:<myLocalIP>, 1433; Initial catalog = <DatabaseName>;
User Id = <User>; Password = <Passwd>;",
queryString = "SELECT * FROM [...]";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand SelectCommand = new SqlCommand(queryString, connection);
SqlDataReader myreader;
connection.Open();
myreader = SelectCommand.ExecuteReader();
List<String> lst = new List<String>();
while (myreader.Read())
{
lst .Add(myreader[0].ToString());
}
connection.Close();
But connection.Open() throws an error:
SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0).
My server is started, it allows remote connections and TCP/IP is enabled. Any ideas?
DBMSSOCN=TCP/IP is how to use TCP/IP instead of Named Pipes.
Try to change :
string connectionString = #"Server = tcp:<myLocalIP>, 1433;Network Library=DBMSSOCN; Initial catalog = <DatabaseName>;
User Id = <User>; Password = <Passwd>;"
If it won't work, try to hard core IP as in below example :
string connectionString = "Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword";
For more informations: Connection Strings
Hope that will solve problem, cheers
I found a solution. I created a server(Console Application) that connects to MySQL server. Then I connected my Xamarin/ASP.NET application to that server where I process queries.

Non idempotent SQL command with OrientDB JDBC Driver

Hello,
I'm facing an issue while using the JDBC driver to connect to a Plocal Orient DB.
Here is my code:
Properties info = new Properties();
info.put("user", this.user);
info.put("password", this.pwd);
java.sql.DriverManager.registerDriver(new com.orientechnologies.orient.jdbc.OrientJdbcDriver());
Connection conn = (OrientJdbcConnection) DriverManager.getConnection(this.url, info);
String sql = "insert into personne (name) values(?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "test recuperation RID par jdbc");
ResultSet rs = stmt.executeQuery();
...
And here the exception stack;
Exception in thread "main" com.orientechnologies.orient.core.exception.OCommandExecutionException: Cannot execute non idempotent command
at com.orientechnologies.orient.core.storage.OStorageEmbedded.executeCommand(OStorageEmbedded.java:90)
at com.orientechnologies.orient.core.storage.OStorageEmbedded.command(OStorageEmbedded.java:85)
at com.orientechnologies.orient.core.sql.query.OSQLQuery.run(OSQLQuery.java:69)
at com.orientechnologies.orient.core.sql.query.OSQLSynchQuery.run(OSQLSynchQuery.java:82)
at com.orientechnologies.orient.core.query.OQueryAbstract.execute(OQueryAbstract.java:29)
at com.orientechnologies.orient.core.db.record.ODatabaseRecordAbstract.query(ODatabaseRecordAbstract.java:505)
at com.orientechnologies.orient.core.db.ODatabaseRecordWrapperAbstract.query(ODatabaseRecordWrapperAbstract.java:188)
at com.orientechnologies.orient.jdbc.OrientJdbcPreparedStatement.executeQuery(OrientJdbcPreparedStatement.java:73)
at org.octopus.dao.jeudonnees.impl.JdbcJeuDonneesDAO.create(JdbcJeuDonneesDAO.java:39)
at org.octopus.dao.jeudonnees.impl.JdbcJeuDonneesDAO.create(JdbcJeuDonneesDAO.java:1)
at org.octopus.tests.tests.main(tests.java:23)
It seems like it's not possible to run non idempotent commands using the Orient JDBC Driver.
Thanks for your help
The standard way to call an insert with JDBC is by using .executeUpdate():
http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/

Accessing UniData through .net

I'm having trouble with accessing UniData data from the u2.net toolkit. I'm able to connect ok - have tested connections with the "Test Connection Tool" and in code, both connections work fine. My problem is when I try and fill a dataset - using the sample code: I get this error:
[U2][UCINET][UNIDATA]:You have no privilege on file THENAME
Here is the code:
U2Connection con = new U2Connection();
try
{
U2ConnectionStringBuilder conn_str = new U2ConnectionStringBuilder();
conn_str.UserID = "id";
conn_str.Password = "pwd";
conn_str.Server = "srv2";
conn_str.Database = "DB.XXX";
conn_str.ServerType = "UNIDATA";
conn_str.RpcServiceType = "udserver";
con.ConnectionString = conn_str.ToString();
con.Open();
DataTable schema = con.GetSchema();
U2DataAdapter da = new U2DataAdapter("SELECT * FROM THENAME ", con);
DataSet ds = new DataSet();
da.Fill(ds);
}
catch (Exception ex)
{
string lStr = ex.Message;
}
finally
{
con.Close();
1 more note, I have an ODBC connection setup. Through ODBC I can use the same credentials inside a SQL Server Linked Server to access the same query successfully.
Any Ideas would be appreciated.
By default, UniData grants no privileges to access files via SQL.
You will need to run CONVERT.SQL from the database to grant privileges to the file.
You can find out more about the command by either running HELP CONVERT.SQL on the command line, or reading the manuals.
Could you please run TCL command ?
select * from privilege;
Do you see THENAME there? For example, see enclosed screen shot for VOC file.

access Mbeans on weblogic

From the documentation of oracle :
Domain Runtime MBean Server : This MBean server also acts as a single
point of access for MBeans that reside on Managed Servers.
what i want to do is to use this fact to access all my custom mBeans scattered in several managed servers.
for example assume that i have two nodes server-1 server-2 .
how can i access all of the custom mBeans on both server-1 server-2 by connecting to the administrator node ?
i dont want to remotly access each node to return the result i want a single entry point
i managed to get the names of the servers and the states and other information by doing this
JMXConnector connector;
ObjectName service;
MBeanServerConnection connection;
String protocol = "t3";
Integer portInteger = Integer.valueOf(<admin server port>);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.runtime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, "<serverName>", port,
jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
h.put(Context.SECURITY_CREDENTIALS, "weblogicpass");
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
h.put("jmx.remote.x.request.waiting.timeout", new Long(10000));
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection(); service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
ObjectName[] ons = (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
int length = (int) ons.length;
for (int i = 0; i < length; i++) {
String name = (String) connection.getAttribute(ons[i],
"Name");
String state = (String) connection.getAttribute(ons[i],
"State");
String internalPort = (String) connection.getAttribute(ons[i],"ListenPort");
System.out.println("Server name: " + name + ". Server state: "
+ state);
but i need to access the custom Mbeans created on each server and not only the information
maybe my question wasnt clear but i found an answer and i will share it here now :
Question summary : i need to access custom mBeans exists in a managed server by connecting to the administration server from a client application.
Answer :
to do that you need to deploy your application to the administrator server (i tried remote but it didn't work )
you need to connect to the DomainRuntimeServiceMBean because it provide a common access point for navigating to all runtime and configuration MBeans in the domain .
when searching for the Object name add Location=
here is the code:
Hashtable props = new Hashtable();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
props.put(Context.SECURITY_PRINCIPAL, "<userName>");
props.put(Context.SECURITY_CREDENTIALS, "<password>");
Context ctx = new InitialContext(props);
MBeanServer server = (MBeanServer)ctx.lookup("java:comp/env/jmx/domainRuntime");
ObjectName on =new ObjectName("com.<companyName>:Name=<Name>,Type=<Type>,Location=<managed_server_name>");
boolean boolresult=(Boolean)server.invoke(on, "<method_Name>",
new Object[]{"<ARG1>","<ARG2>","<ARG3>"}
,new String[]{"java.lang.String","java.lang.String","java.lang.String"});
out.print(boolresult);