我需要使用 PreparedStatement 將用戶 IP 地址插入到 MySql 表中。我該怎么做?我嘗試在 Servlet 中使用以下代碼。InetAddress ipaddress;try { ipaddress = InetAddress.getLocalHost();} catch (UnknownHostException e) { e.printStackTrace();}我想知道有沒有比上面更好的方法來獲取 IP 地址??或者我可以繼續上面的代碼?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
1 回答

慕碼人2483693
TA貢獻1860條經驗 獲得超9個贊
如果您想獲取 IP,則不要使用:
preparedStatement.setString(1, ipaddress);
使用 :
// this will return the IP Host Adresse for example 192.168.1.2 preparedStatement.setString(1, ipaddress.getHostAddress());
添加回答
舉報
0/150
提交
取消