java如何实现替换指定位置的指定字符串的功能
功能类似:Stringsql="select*fromteacherwhereid=?andname=?";写一个方法publicStringReplaceString(...
功能类似:
String sql = "select * from teacher where id = ? and name = ?";
写一个方法public String ReplaceString (String str,String rstr,int a);
如:ReplaceString(sql,"name",1);这里想要的结果是将第二个?号替换为"name"
ReplaceString(sql,"101",0);这里想要的结果是将第一个?号替换为"101"?
请问有没有人实现过这种功能 ?? 展开
String sql = "select * from teacher where id = ? and name = ?";
写一个方法public String ReplaceString (String str,String rstr,int a);
如:ReplaceString(sql,"name",1);这里想要的结果是将第二个?号替换为"name"
ReplaceString(sql,"101",0);这里想要的结果是将第一个?号替换为"101"?
请问有没有人实现过这种功能 ?? 展开
8个回答
展开全部
自己写 conn
String sql = "select * from teacher where id = :x1 and name = :x2 ";
Connection conn = ...
PreparedStatement st = null;
try {
st = conn.prepareStatement(sql);
st.setString(1, "101");
st.setString(2,"name");
ResultSet rs =st.executeQuery();
}
finally {
try {
if (null != st) {
st.close();
}
if (null != conn) {
conn.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
String sql = "select * from teacher where id = :x1 and name = :x2 ";
Connection conn = ...
PreparedStatement st = null;
try {
st = conn.prepareStatement(sql);
st.setString(1, "101");
st.setString(2,"name");
ResultSet rs =st.executeQuery();
}
finally {
try {
if (null != st) {
st.close();
}
if (null != conn) {
conn.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一进入这个方法就做个判断, int a =1 就替换第二个? ,a=0就替换第一个?,但参数你怎么也少了一个...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询