|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
但是我同意你的观点,对于大型项目来说,应该是采用框架的一部分,根据功能的不同而改进,欢迎你能再提出些宝贵意见,我会多多学习的。说到jbuilder,我可能是个人感觉,用的时候确实没有vs爽,我最喜欢的IDE是netbeans,谢谢。语句<%@pagecontentType="text/html;charset=gb2312"%>
<%@pageimport="java.sql.*,
java.util.*
"%>
<%!
publicStringchomp(Stringstr,Stringseparator){
if(str==null||str.length()==0||separator==null){
returnstr;
}
if(str.endsWith(separator)){
returnstr.substring(0,str.length()-separator.length());
}
returnstr;
}
%>
<%!
publicStringcapitalize(Stringstr){
intstrLen;
if(str==null||(strLen=str.length())==0){
returnstr;
}
returnnewStringBuffer(strLen)
.append(Character.toUpperCase(str.charAt(0)))
.append(str.substring(1))
.toString();
}
%>
<%
StringtableName=request.getParameter("t");
if(tableName==null||"".equals(tableName))
{
out.println("参数t");
return;
}
%>
<%
Connectionconn;
StringDBUser="sa";
StringDBPassword="sa";
StringDBServer="127.0.0.1";//Cantuselocalhost,youmustuseIPorCNAME
StringDBNAME="BcinetDB";//changetoyourdbname
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//connecttothedatabase
conn=java.sql.DriverManager.getConnection("jdbc:microsoft:sqlserver://"+DBServer+":1433;DatabaseName="+DBNAME,DBUser,DBPassword);
Stringsql="select*from"+tableName;//changetoyourtablename
Statementstmt=conn.createStatement();
ResultSetrs=stmt.executeQuery(sql);
ResultSetMetaDatameta=rs.getMetaData();
//tableName=meta.getTableName(1);
intcount=meta.getColumnCount();
ArrayListAutoIncrementFields=newArrayList();
String[]ColumnNames=newString[count];
StringColumnClassNames[]=newString[count];
for(inti=0;i<count;i++)
{
if(meta.isAutoIncrement(i+1)){
AutoIncrementFields.add(newInteger(i));
}
ColumnNames[i]=meta.getColumnName(i+1);
ColumnClassNames[i]=meta.getColumnClassName(i+1);
}
rs.close();
stmt.close();
conn.close();//改成本人的数据库毗连开释
HashMapm=newHashMap();
m.put("java.lang.Boolean","Boolean");
//m.put("java.lang.Integer","Integer");
m.put("java.lang.Integer","Int");
m.put("java.lang.Long","Long");
m.put("java.math.BigDecimal","BigDecimal");
m.put("jjava.lang.Float","Float");
m.put("java.lang.Double","Double");
m.put("java.lang.String","String");
m.put("java.sql.Date","Date");
m.put("java.sql.Time","Time");
m.put("java.sql.Timestamp","Timestamp");
m.put("java.lang.Object","Object");
StringBufferselect=newStringBuffer("select");
StringBufferinsert1=newStringBuffer("insertinto");
insert1.append(tableName);
insert1.append("(");
StringBufferinsert2=newStringBuffer(")values(");
StringBufferupdate=newStringBuffer("update");
update.append(tableName);
update.append("set");
StringBufferinsertp1=newStringBuffer(insert1.toString());
StringBufferinsertp2=newStringBuffer(insert2.toString());
StringBufferupdatep=newStringBuffer(update.toString());
for(inti=0;i<count;i++)
{
select.append(ColumnNames[i]);
select.append(",");
if(!AutoIncrementFields.contains(newInteger(i))){
insert1.append(ColumnNames[i]);
insert1.append(",");
insertp1.append(ColumnNames[i]);
insertp1.append(",");
insert2.append(""+");
insert2.append(ColumnNames[i]);
insert2.append("+"");
insert2.append(",");
insertp2.append("?");
insertp2.append(",");
update.append(ColumnNames[i]);
update.append("="+");
update.append(ColumnNames[i]);
update.append("+",");
updatep.append(ColumnNames[i]);
updatep.append("=");
updatep.append("?");
updatep.append(",");
}
}
select=newStringBuffer(chomp(select.toString(),","));
select.append("from");
select.append(tableName);
select.append("");
insert1=newStringBuffer(chomp(insert1.toString(),","));
insert2=newStringBuffer(chomp(insert2.toString(),","));
insert2.append(")");
insert1.append(insert2);
update=newStringBuffer(chomp(update.toString(),","));
update.append("where");
if(AutoIncrementFields.size()>0)
{
update.append(ColumnNames[((Integer)AutoIncrementFields.get(0)).intValue()]);
update.append("="+");
update.append(ColumnNames[((Integer)AutoIncrementFields.get(0)).intValue()]);
update.append("+"");
}
insertp1=newStringBuffer(chomp(insertp1.toString(),","));
insertp2=newStringBuffer(chomp(insertp2.toString(),","));
insertp2.append(")");
insertp1.append(insertp2);
updatep=newStringBuffer(chomp(updatep.toString(),","));
updatep.append("where");
if(AutoIncrementFields.size()>0)
{
updatep.append(ColumnNames[((Integer)AutoIncrementFields.get(0)).intValue()]);
updatep.append("=");
updatep.append("?");
}
%>
<html>
<head>
<title>sql语句天生啦</title>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
</head>
<body>
<p>表<%=tableName%>共
<%
out.print(count);
out.print("个字段");
Iteratoriterator=AutoIncrementFields.iterator();
intj=AutoIncrementFields.size();
while(iterator.hasNext())
{j--;
if(j==AutoIncrementFields.size()-1){
out.print("个中");
}
inti=((Integer)iterator.next()).intValue();
out.print(ColumnNames[i]);
if(j!=0){
out.print(",");
}
else
{
out.print("是AutoIncrement范例不呈现在sql语句中");
}
}
%>
<%
out.print("<br>");
out.println(select);
out.print("<br>");
out.print("<br>");
out.println(insert1);
out.print("<br>");
out.print("<br>");
out.println(update);
out.print("<br>");
out.print("<br>");
out.println(insertp1);
out.print("<br>");
out.print("<br>");
out.println(updatep);
%>
</p>
<hr>
<p>
<%
for(inti=0;i<count;i++)
{
out.print("private");
out.print(ColumnClassNames[i]);
out.print("");
out.print(ColumnNames[i]);
out.println(";<br>");
}
%>
<%
out.println("<br>");
for(inti=0;i<count;i++)
{
out.print("publicvoidset");
out.print(capitalize(ColumnNames[i]));
out.print("(");
out.print(ColumnClassNames[i]);
out.print("");
out.print(ColumnNames[i]);
out.print("){");
out.println("<br>");
out.print("this.");
out.print(ColumnNames[i]);
out.print("=");
out.print(ColumnNames[i]);
out.print(";}");
out.println("<br>");
out.print("public");
out.print(ColumnClassNames[i]);
out.print("get");
out.print(capitalize(ColumnNames[i]));
out.print("(){");
out.println("<br>");
out.print("return");
out.print(ColumnNames[i]);
out.println(";}<br>");
}
%>
</p>
<hr>
<p>
<%
for(inti=0;i<count;i++)
{
out.print(ColumnNames[i]);
out.print("=");
out.print(tableName);
out.print(".get");
out.print(capitalize(ColumnNames[i]));
out.println("();<br>");
}
%>
</p>
<hr>
<p>
ParameterParserparser=newParameterParser(request);
<br/>
<%
for(inti=0;i<count;i++)
{
out.print("String");
out.print(ColumnNames[i]);
out.print("=");
out.print("parser.getStringParameter("");
out.print(ColumnNames[i]);
out.print("","");");
out.println("<br>");
}
%>
</p>
<hr>
<p>
<%
for(inti=0;i<count;i++)
{
out.print("String");
out.print(ColumnNames[i]);
out.print("=");
out.print("request.getParameter("");
out.print(ColumnNames[i]);
out.print("");");
out.println("<br>");
}
%>
</p>
<hr>
<pre>
Connectionconn=ConnectionManager.getConnection();
PreparedStatementstmt=null;
StringBuffersql=newStringBuffer();
sql.append("<%out.print(insertp1);%>");
try{
stmt=conn.prepareStatement(sql.toString());
</pre>
<%
intjj=0;
for(inti=0;i<count;i++)
{
if(!AutoIncrementFields.contains(newInteger(i))){
jj++;
out.print("stmt.set");
out.print(m.get(ColumnClassNames[i]));
out.print("(");
out.print(jj);
out.print(",");
out.print(ColumnNames[i]);
out.print(");<br>");
}}
%>
<pre>
introws=stmt.executeUpdate();
}
}
catch(SQLExceptione){
throwe;
}
finally{
if(stmt!=null){
stmt.close();
}
if(conn!=null){
ConnectionManager.colse(conn);
}
}
</pre>
<hr>
<pre>
Connectionconn=ConnectionManager.getConnection();
PreparedStatementstmt=null;
StringBuffersql=newStringBuffer();
sql.append("<%out.print(updatep);%>");
try{
stmt=conn.prepareStatement(sql.toString());
</pre>
<%
intjjj=0;
for(inti=0;i<count;i++)
{
if(!AutoIncrementFields.contains(newInteger(i))){
jjj++;
out.print("stmt.set");
out.print(m.get(ColumnClassNames[i]));
out.print("(");
out.print(jjj);
out.print(",");
out.print(ColumnNames[i]);
out.print(");<br>");
}}
if(AutoIncrementFields.size()>0)
{
jjj++;
out.print("stmt.set");
out.print(m.get(ColumnClassNames[((Integer)AutoIncrementFields.get(0)).intValue()]));
out.print("(");
out.print(jjj);
out.print(",");
out.print(ColumnNames[((Integer)AutoIncrementFields.get(0)).intValue()]);
out.print(");<br>");
}
%>
<pre>
introws=stmt.executeUpdate();
}
}
catch(SQLExceptione){
throwe;
}
finally{
if(stmt!=null){
stmt.close();
}
if(conn!=null){
ConnectionManager.colse(conn);
}
}
</pre>
<hr>
<pre>
<tablewidth="90%"border="0"align="center"cellpadding="1"cellspacing="1">
<formname="form1"method="post"action="">
</pre>
<%
for(inti=0;i<count;i++)
{
if(!AutoIncrementFields.contains(newInteger(i))){
out.print("<tr>");
out.print("<td>");
out.print(ColumnNames[i]);
out.print("</td>");
out.print("<td>");
out.print("<inputtype="text"name="");
out.print(ColumnNames[i]);
out.print("">");
out.print("</td>");
out.print("</tr>");
out.println("<br>");
}
}
/*if(AutoIncrementFields.size()>0)
{
out.print("<inputtype="hidden"name="");
out.print(ColumnNames[((Integer)AutoIncrementFields.get(0)).intValue()]);
out.print("">");
}*/
%>
<pre>
</form>
</table>
</pre>
<hr>
<pre>
<tablewidth="90%"border="0"align="center"cellpadding="1"cellspacing="1">
<formname="form1"method="post"action="">
</pre>
<%
for(inti=0;i<count;i++)
{
if(!AutoIncrementFields.contains(newInteger(i))){
out.print("<tr>");
out.print("<td>");
out.print(ColumnNames[i]);
out.print("</td>");
out.print("<td>");
out.print("<inputtype="text"name="");
out.print(ColumnNames[i]);
out.print(""value="<%=");
out.print(ColumnNames[i]);
out.print("%>">");
out.print("</td>");
out.print("</tr>");
out.println("<br>");
}
}
if(AutoIncrementFields.size()>0)
{
out.print("<inputtype="hidden"name="");
out.print(ColumnNames[((Integer)AutoIncrementFields.get(0)).intValue()]);
out.print(""value="<%=");
out.print(ColumnNames[((Integer)AutoIncrementFields.get(0)).intValue()]);
out.print("%>">");
}
%>
<pre>
</form>
</table>
</pre>
</body>
</html>
其实你不用Struts,spring这些工具,直接用jsp,servlet能够很方便地写出来,而且,可以根据个人的水平、爱好,有很多方案。而struts,spring这些工具的出来。 |
|