I need to insert user IP Address into MySql table using PreparedStatement. How can I do it?
I tried with following code in Servlet.
InetAddress ipaddress;
try {
ipaddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
I want to know is there any better method than above to get IP Address ?? or I can continue with above code?
String insertquery = "insert into tablename (IPAdd) values (?)";
preparedStatement = conn.prepareStatement(insertquery);
preparedStatement.setString(1, ipaddress);// It's not a string so How can I set values here ?// Getting error here
If you want to get the IP then Instead of use :
preparedStatement.setString(1, ipaddress);
use :
// this will return the IP Host Adresse for example 192.168.1.2
preparedStatement.setString(1, ipaddress.getHostAddress());