connect to msql db using name rather the ip address using tablet - java

I am trying to connect to a msql db using tablet
my connection string is such like
connString ="jdbc:jtds:sqlserver://10.0.0.28:1433/foodserv;user=align;password=alignminds;";
it work fine .
Now i want to connect to the db using name of the server Like ip 10.0.0.28 has server name suresh-pc
Then how the string will change
my point is to replace ip with name and i am connection using tablet

Server name is nothing but an alias for the IP of a host. If you use the server's name insted of IP the DNS server will resolve its ip for you. Check the link for ho DNS works http://www.howstuffworks.com/dns.htm. So simply replace the IP with server's name.

Related

Unable to connect to Tomcat server with URL

I can connect to my tomcat7 server using IP address but when I replace IP Address with my URL, I'm unable to connect to the host.
Error Message displayed:
This web page is not available
ERR_CONNECTION_REFUSED
The website may be down or your network may not be properly configured.
Try add pair host_ip host_name to host's file in your compruter.

Connection to <hostname>:5432 refused

I'm not a pro in communication networks, so I have no idea what should be my "hostname" and where can I find it?
I created a postgreSQL server on ubuntu 14.04, I can connect to it from the same computer without problem by setting hostname to 127.0.0.1(localhost), or even 192.168.1.42 (my private ip). But I can not connect to my server from any other computer, even if they are on local network or not. I always get this message:
"Connection to <hostname>:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections."
where is either 127.0.1.1, 127.0.0.1, 192.168.1.42, 98.765.432.123(public ip) or anything else, it never works.
What is the right hostname?
I already set listen_addresses to '*', already edited pg_hba.conf to accept 0.0.0.0/0 IP-s, and disabled ubuntu firewall.
This could be caused by many things.
The server has an IP address. That is the address that the JDBC connection string should use for <hostname>, unless you have some local DNS solution. To rule out problems with the latter, use the IP address of the Ubuntu server. On Ubuntu type ifconfig to see it; it probably is 192.168.1.42 like you mentioned. So from the client with the Java app, you use the IP address of the server.
If you have a standard PostgreSQL installation, it should use port 5432, you can check the setting in postgresql.conf. In the JDBC connection string you should also indicate the port: "jdbc:postgresql://192.168.1.42:5432/...", just to be on the safe side.
You need to have a database that you can connect to. Note that by default only the owner of the database (probably the user postgres in your case) can connect to it. See further down.
In pg_hba.conf you need to create an entry for your new database so it can be connected to (typical settings, check your network setup). The IP address (range) you specify here is that of the clients connecting to the database!
host my_db all 192.168.0.0/16 md5
You must restart your PostgreSQL server after modifying pg_hba.conf. (Typically on Ubuntu, do sudo ./etc/init.d/postgresql restart)
To create database my_db (or whatever name you prefer), go to your Ubuntu box and enter psql as the postgres user:
vekszor#ubuntu:~$ sudo -u postgres psql
[sudo] password for user:
psql (9.3.5)
Type "help" for help
postgres=#
In order to connect to the database, you should have a database:
postgres=# CREATE DATABASE my_db;
CREATE DATABASE
You also want a user (role) that is less powerful than the postgres superuser, but still able to manipulate the database. So create this user role and assign ownership of the database:
postgres=# CREATE ROLE vekszor LOGIN PASSWORD 'secret123' CREATEROLE;
CREATE ROLE
postgres=# ALTER DATABASE my_db OWNER TO vekszor;
ALTER
Now you can go back to your client computer with the Java app and finish the JDBC connection string with the name of the database, the user and password.
Note that if you want to access your database from the internet, you should set up a NAT rule in your router to point traffic on port 5432 to the IP of your Ubuntu server.So long as the internal address of the router is in the same address range of the client computers that you indicated in pg_hba.conf this should be easy to set up, otherwise add a new entry in pg_hba.conf.
Reading your comments to the correct answer:
This is how you add a rule to iptables:
iptables -A INPUT -s 0/0 -p tcp --dport 5432 -j ACCEPT
0/0: If you want anybody to access it. You can change it to a specific ip address or range of ip addresses.
If you are connected to the server using UI you can run the below query to get the IP Address of server using below query to verify if you are using the correct IP.
Select inet_server_addr();

Client System Host Name is not captureing using request.getRemoteHost()

We are using request.getRemoteHost() in the jsp page to get the hostname of the client but it ll return only IP address of the client system
Is any other way to get the client system host name from java-script,jsp,...
If you server has enableLookups ( for example for Tomcat ) on false this getRemoteHost() will return only ip . Simplest solution would be to just change property and force resolving of ip to hostname.

Connect to remote MySQL server using Java

I'm writing a program to push stuff into a remote database. My database is stored on a redhat server, while my program is written on a windows machine.
I dont want to give out my server addresses but lets say my Linux server is xx.xx.xx.xx8
MySQL Workbench in windows says that my mysql server host is 127.0.0.1:3306
I know there are a million similar questions but each one is pretty unique to the situation.
I've been using http://www.vogella.com/tutorials/MySQLJava/article.html#javaconnection as a guide but this looks like a local connection.
I have also been referencing this but it confuses me.
Here's a mock up of some code i think may work:
Connection connection = null;
String dburl = "jdbc:mysql://127.0.0.1:3306/Db_Name";
String userName = "user";
String passWord = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(dburl, userName, passWord);
Statement st = connection.createStatement();
String query = "INSERT INTO Example (`TestColumn`) VALUES('hello')";
int rsI = st.executeUpdate(query);
System.out.println("Hi");
}catch (Exception e) {
System.out.println(e);
} finally {
if (connection != null) {
try {
connection.close();
System.out.println("Database connection terminated");
} catch (Exception e) { /* ignore close errors */ }
}
}
that is based on the link i provided. All i changed was the address. I have no idea where the server address xx.xx.xx.xx8 should go.
In order to connect to the remote database you need to know the external ( or called public ) ip address of the server.
String dburl = "jdbc:mysql://85.65.85.222:3306/Db_Name"
Even if your database on server says it listens on localhost:3306, you can still connect to it using public ip with correct port. (providing you have sufficient access rights to the server)
Sometimes there may be situation that even if you know the remote server IP address you still won't be able to connect to remote database directly perhaps due to blocked port or other ACL issues ( and this is ofc applicable if you have no control over this). You could however create ssh tunnel to that server and create a port forward.
JSch jsch = new JSch();
Session session = jsch.getSession(user, host);
session.setPassword(password);
session.connect(timeout);
session.setPortForwardingL(listenPort, destHost, destPort);
example ssh connnection could look something like this:
ssh user#85.65.85.222 -Llocalhost:5050:127.0.0.1:3306 <- this will create a ssh tunnel with port forwarding, meaning on your local machine where you have your code and whole environment you will still be able to connect to remote database using
String dburl = "jdbc:mysql://localhost:5050/dbname"
Just to clarify for anyone looking at this later...
localhost is always the machine you are sitting at - always. There is no way you will ever connect to a remote box using localhost. Also, the IP address 127.0.0.1 is equivalent to localhost, so don't use that either.
You may see that the database binds to localhost or 127.0.0.1 on the database server. That is ok. Use the IP address of the server running the database in your database connection URL
mysql:jdbc://<IPOfServer>:<PortOfServer>/<DbName>).
You can find the IP address of the server by running the following command on the database server...
Windows: ipconfig
Linux: ifconfig
Good luck!
You can see what documentation says:
The method DriverManager.getConnection establishes a database
connection. This method requires a database URL, which varies
depending on your DBMS. The following are some examples of database
URLs:
MySQL: jdbc:mysql://localhost:3306/, where localhost is the name of
the server hosting your database, and 3306 is the port number
So, for MySQL Connector/J Database URL
The following is the database connection URL syntax for MySQL Connector/J:
jdbc:mysql://[host][,failoverhost...]
[:port]/[database]
[?propertyName1][=propertyValue1]
[&propertyName2][=propertyValue2]...
Where:
host:port is the host name and port number of the computer hosting your database. If not specified, the default values of host and port are 127.0.0.1 and 3306, respectively. So, every operation you do will be applied on this post
database is the name of the database to connect to. If not specified, a connection is made with no default database.
failover is the name of a standby database (MySQL Connector/J supports failover).
propertyName=propertyValue represents an optional, ampersand-separated list of properties. These attributes enable you to instruct MySQL Connector/J to perform various tasks.
Update: if you have a remote databases (your remote server) that you want to access to, you should have for example:
String dburl = "jdbc:mysql://<REMOTE_HOST>:3306/Db_Name"; //replace with your remote host
... // here all the code related to your remote host
Note: if you remote server has in its configuration "localhost" you have to figure out its public ip address by executing ifconfig. Then use its ip address on your string. As important note your remote server must be configured to allow remote connections.
if you connect to a server (ie. with the ip xx.xx.xx.xx8) you specify through the port (which is usually 3306) that you want to connect to the databaseserver on that machine. so simply use:
String dburl = "jdbc:mysql://xx.xx.xx.xx8:3306/Db_Name";
as your connectionstring.
If you dont know the port, mysql is running, have a look in your /etc/mysql/my.cnf file. There should be the port specified.
Also have a look at your firewallrules if you can't connect and make sure you enabled remoteconnections (here is a good tutorial to enable it: http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html).

connection string for phpMyAdmin database

If I have a mysql database on my own pc, I would use the following cnxn string to connect it to my java program :
String url = "jdbc:mysql://localhost:port/dbname";
But if my database is made over phpMyAdmin, what would be the connection string ?
one other note, the phpmyadmin database is not on my local pc now, it is on another one but both pc are in the same network.
Thanks for any help ..
phpMyadmin is administration web application developed on PHP platform. phpMyadmin connects to MySQL database that might be on other machine.
you need to check config.inc.php file of phpMyadmin to check the host and port of mysql database.
Once you get the IP and port of the mysql database you can connect using connect string like
jdbc:mysql://<ipaddress>:<port>/<database_created_through_phpMyAdmin>
Replace localhost with the IP of the machine that hosts your database:
String url = "jdbc:mysql://<ip_goes_here>:port/dbname";
As Rutesh mentions, this may or may not be the same machine on which phpMyAdmin runs.

Categories

Resources