|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
从一个编程语言的普及程度来将,一个好的IDE是至关中要的,而现在的java的IDE虽然已经很好了,但是和.net比起来还是稍微差一些的,这是个客观事实。java要想普及的更好。DE是必须加以改进的。js|mysqlJsp,JavaBean,Mysql的毗连办法
使用JSP完成Web与数据库的毗连:
(1).完成情况设置,导进java.sql包,命令以下:
importjava.sql.*
(2).载进驱动器
(3).毗连到数据库
(4).语句接口
(5).取得了局集
1.测试情况:
windows2003
j2sdk1.4.2_06
Tomcat5.0
mysql-4.0.23-win
mm.mysql-2.0.4-bin.jar
2.创建数据库及表
在Mysql中建个publish数据库,并建个book表。在book中增加id,title,price
3.JavaBean的代码:dbconn.java
packageBorder;
importjava.sql.*;//完成情况设置,导进java.sql包
publicclassdbconn{
publicdbconn()
{
}
//declarevariable
privateConnectionconn=null;
privateResultSetrs=null
privateStringserver="127.0.0.1";//Cantuselocalhost,youmustuseIPorCNAME
privateStringport="3306";//changetoyourport
privateStringdb="publish";//changetoyourdbname
privateStringuser="root";//changetoyourusername
privateStringpass="root";//changetoyourpassword
privateStringdrivername="org.gjt.mm.mysql.Driver";//mysqldriver
privateStringURL="jdbc:mysql://"+server+":"+port+"/"+db+"?useUnicode=true&characterEncoding=GBK&user="+user+"&password="+pass;
publicConnectiongetConn(){//getdatabaseconnection
try{
Class.forName(drivername).newInstance();//载进驱动器
conn=DriverManager.getConnection(URL);//毗连到数据库
}
catch(Exceptione){
e.printStackTrace();
}
returnconn;
}
publicResultSetexecuteSQL(Stringstr){
try{
Statementstmt=conn.createStatement();//语句接口
rs=stmt.executeQuery(str);//取得了局集
}
catch(Exceptione){
e.printStackTrace();
}
returnrs;
}
}
编译javacdbconn.java,将编译后的文件dbconn.class放到目次"你的项目"WEB-INFclassesBorder下
4.挪用JavaBean的JSP文件test.jsp
<%@pagecontentType="text/html;charset=GBK"import="java.sql.*"%>
<jsp:useBeanid="Border"scope="page"class="Border.dbconn"/>
<%
ResultSetrs=null;
Connectionconn=null;
conn=Border.getConn();
rs=Border.executeSQL("select*frombook");
%>
<html>
<body>
<br>
<h2align="center">MyfirstJspJavaBeanMysql</h2>
<br>
<tableborder="1"align="center">
<tr>
<th>
id
</th>
<th>
title
</th>
<th>
price
</th>
</tr>
<%
while(rs.next()){
%>
<tr>
<th>
<%=rs.getString("id")%>
</th>
<th>
<%=rs.getString("title")%>
</th>
<th>
<%=rs.getString("price")%>
</th>
</tr>
<%}%>
<%
rs.close();
conn.close();
%>
</table>
<formname="form1"method="post"action="update.jsp">
<tablewidth="210"border="1"align="center"cellpadding="0"cellspacing="0">
<tr>
<tdwidth="77">title:</td>
<tdwidth="127"><label>
<inputname="Title"type="text"id="Title">
</label></td>
</tr>
<tr>
<td>price:</td>
<td><label>
<inputname="Price"type="text"id="Price">
</label></td>
</tr>
<tr>
<td><divalign="right">
<label>
<inputtype="submit"name="Submit"value="提交">
</label>
</div></td>
<td><label>
<inputtype="reset"name="Submit2"value="重置">
</label></td>
</tr>
</table>
</form>
</body>
</html>
5.update.jsp写进数据库
<%@pagecontentType="text/html;charset=GBK"import="java.sql.*"%>
<%request.setCharacterEncoding("GBK");%>
<jsp:useBeanid="Border"scope="page"class="Border.text"/>
<%
Connectionconn=null;
conn=Border.getConn();
Stringtit=request.getParameter("Title");
Stringprc=request.getParameter("Price");
Statementstmt=conn.createStatement();
stmt.executeUpdate("INSERTINTObook(title,price)VALUES("+tit+","+prc+")");
%>
<jsp:forwardpage="text.jsp"/>
6.供应几个函数:
(1).getConnection
publicstaticConnectiongetConnection(Stringurl,
Stringuser,
Stringpassword)
throwsSQLException
AttemptstoestablishaconnectiontothegivendatabaseURL.TheDriverManagerattemptstoselectanappropriatedriverfromthesetofregisteredJDBCdrivers.
Parameters:
url-adatabaseurloftheformjdbc:subprotocol:subname
user-thedatabaseuseronwhosebehalftheconnectionisbeingmade
password-theuserspassword
Returns:
aconnectiontotheURL
Throws:
SQLException-ifadatabaseaccesserroroccurs
(2).createStatement
publicStatementcreateStatement()
throwsSQLException
CreatesaStatementobjectforsendingSQLstatementstothedatabase.SQLstatementswithoutparametersarenormallyexecutedusingStatementobjects.IfthesameSQLstatementisexecutedmanytimes,itmaybemoreefficienttouseaPreparedStatementobject.
ResultsetscreatedusingthereturnedStatementobjectwillbydefaultbetypeTYPE_FORWARD_ONLYandhaveaconcurrencylevelofCONCUR_READ_ONLY.
Returns:
anewdefaultStatementobject
Throws:
SQLException-ifadatabaseaccesserroroccurs
(3).executeQuery
publicResultSetexecuteQuery(Stringsql)
throwsSQLException
ExecutesthegivenSQLstatement,whichreturnsasingleResultSetobject.
Parameters:
sql-anSQLstatementtobesenttothedatabase,typicallyastaticSQLSELECTstatement
Returns:
aResultSetobjectthatcontainsthedataproducedbythegivenquery;nevernull
Throws:
SQLException-ifadatabaseaccesserroroccursorthegivenSQLstatementproducesanythingotherthanasingleResultSetobject
(4).getString
publicStringgetString(StringcolumnName)
throwsSQLException
RetrievesthevalueofthedesignatedcolumninthecurrentrowofthisResultSetobjectasaStringintheJavaprogramminglanguage.
Parameters:
columnName-theSQLnameofthecolumn
Returns:
thecolumnvalue;ifthevalueisSQLNULL,thevaluereturnedisnull
Throws:
SQLException-ifadatabaseaccesserroroccurs
(5).next
publicbooleannext()
throwsSQLException
Movesthecursordownonerowfromitscurrentposition.AResultSetcursorisinitiallypositionedbeforethefirstrow;thefirstcalltothemethodnextmakesthefirstrowthecurrentrow;thesecondcallmakesthesecondrowthecurrentrow,andsoon.
Ifaninputstreamisopenforthecurrentrow,acalltothemethodnextwillimplicitlycloseit.AResultSetobjectswarningchainisclearedwhenanewrowisread.
Returns:
trueifthenewcurrentrowisvalid;falseiftherearenomorerows
Throws:
SQLException-ifadatabaseaccesserroroccurs
7.ok!Nowgameisover~~
8.若您以为中意,想转载或保藏这篇文章,我十分感激,但请您说明作者:border(border@hacker.cn)
http://blog.csdn.net/border1985
最初接待人人会见--中国平安信息网--http://www.hacker.cn/
还是要自己一点一点写代码,然后编译,改错再编译好那。还有最重要的是.net的编译环境非常好,你甚是不需要了解太多工具,对于简单的系统,你可以之了解一些语法就哦了。 |
|