小弟要做一个网站系统,可是写好了jsp文件和连接数据库的文件,可是不知道怎么样把信息插入到数据库中.我这里把代码贴出来,希望有大侠帮忙解决一下,在线等。
数据库连接:DataStore.java
/**
 * Copyright (c) 2006, Anndy
 * All copyrights reserved.
 *
 * Created on: 2006-12-4
 * Project_name: MyWebSite
 * Package_name: scu.Anndy.db
 * Filename: DataStore.java
 * Abstract: *******************
 */
package scu.Anndy.db;/**
 * @author Anndy
 * @version 1.00
 * 
 */
import java.sql.*;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DataStore { private static DataStore db = null; private static Connection conn = null; private Statement stmt = null;

private boolean hadErrors = false; private DataStore() throws Exception {
if (conn == null) {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/website", "root",
"root");
stmt = conn.createStatement();
}
}

public void beginTransaction() throws SQLException
    {
conn.setAutoCommit(false);
    }

public void commitTransaction() throws SQLException
    {
        if( !hadErrors ){
            conn.commit();
        }
        else{
         conn.rollback();
         hadErrors = false;
        }               hadErrors = false;
        conn.setAutoCommit(true);
    } public void ErrorOccur( ){
hadErrors = true;
}

public static DataStore getInstance() {
if (db == null) {
try {
db = new DataStore();
} catch (Exception e) {
conn = null;
Log log = LogFactory.getLog("mylog");
log.error("数据库连接出错-DataStore.java");
return null;
}
}
return db; } synchronized public void execute(String sql){
try{
if (stmt != null)
stmt.executeUpdate(sql);
else {
Log log = LogFactory.getLog("mylog");
log.error("数据库插入数据出错");
}
}catch(SQLException e){

}
} synchronized public ResultSet read(String sql){
ResultSet tmp = null;

try{
if (stmt != null) {
tmp = stmt.executeQuery(sql);
return tmp;
} else {
return null;
}
}catch(SQLException e){
return null;
}
}

synchronized public int readCount( String sql ){
int nCount = 0;

try {
if( stmt != null ) {
ResultSet tmp = null;
tmp = stmt.executeQuery(sql);
if( tmp!=null && tmp.next() ) {
nCount = tmp.getInt(1);
}else {
nCount = 0;
}
}else {
nCount = 0;
}
}catch(SQLException e) {
nCount = 0;
}

return nCount;
} synchronized public void stop() {
try {
if (conn != null) {
conn.close();
stmt.close();
}
} catch (Exception e) {
/* 记录到日志 */
Log log = LogFactory.getLog("mylog");
log.error("数据库关闭出错-DataStore.java");
} finally {
conn = null;
}
}
}

解决方案 »

  1.   

    用户注册的jsp文件:register.jsp
    </head>
    <body bgcolor="white" background="pic/bg_1.jpg">
    <table width="100%" height="781"  border="0">
         <tr>
           <td height="86" align="center" valign="top"><table width="100%" height="84"  border="0">
             <tr>
               <td width="18%" height="80"><img src="pic/head_1.gif" width="131" height="73"></td>
               <td width="35%" valign="bottom"><img src="pic/title_1.jpg"></td>
               <td width="47%" align="center" valign="bottom" class="timeText"><jeffy:MyDate /></td>
             </tr>
           </table></td>
         </tr>
         <tr>
           <td height="30" background="pic/headline_1.gif">&nbsp;</td>
         </tr>
         <tr>
           <td height="25" align="center" valign="top" class="downloadText"><html:errors/></td>
         </tr>
         <tr><td>如果使用MySQL数据库,最好不要使用中文,中文显示可能会不正常</td></tr>
         <tr>
           <td height="487" align="center" valign="top">
       <html:form action="/Register.do" focus="userZh">
       <table width="647" height="479" border="1" cellpadding="1" cellspacing="0" bordercolor="#0066FF">
             <tr align="center">
               <td colspan="2" class="BodyHeader_Middle"><bean:message key="reg.a"/></td>
             </tr>
             <tr>
               <td width="121" height="36" class="bookText"><bean:message key="my.account"/></td>
               <td width="464" class="bookText">
       <html:text property="userZh" size="15" maxlength="20"/>
       &nbsp;&nbsp;用户名只能由英文字母、数字和下划线组成</td>
             </tr>
             <tr>
               <td class="bookText"><bean:message key="my.password"/></td>
               <td class="bookText">
       <html:password property="userPwdf" size="13" maxlength="15"/>
       &nbsp;&nbsp;密码只能由英文字母、数字和下划线组成,密码位数不得小于6位</td>
             </tr>
     <tr>
               <td class="bookText"><bean:message key="my.password.again"/></td>
               <td class="bookText">
       <html:password property="userPwds" size="13" maxlength="15"/>
       </td>
             </tr>
             <tr>
               <td class="bookText"><bean:message key="my.name"/></td>
               <td class="bookText">
       <html:text property="userXm" size="17" maxlength="40"/>
       </td>
             </tr>
             <tr>
               <td class="bookText"><bean:message key="my.sex"/></td>
               <td class="bookText">
       <html:radio property="userSex" value="男" ><bean:message key="boy"/></html:radio>
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
               <html:radio property="userSex" value="女" ><bean:message key="girl"/></html:radio>
       </td>
             </tr>
             <tr>
               <td class="bookText"><bean:message key="my.mail"/></td>
               <td class="bookText">
       <html:text property="userMail" size="18" maxlength="50"/>
       </td>
             </tr>
             <tr>
               <td class="bookText"><bean:message key="my.phone"/></td>
               <td class="bookText">
       <html:text property="userPhone" size="13" maxlength="20"/>
       </td>
             </tr>
             <tr>
               <td class="bookText"><bean:message key="my.qq"/></td>
               <td class="bookText">
       <html:text property="userQq" size="13" maxlength="15"/>
       </td>
             </tr>
             <tr>
               <td class="bookText"><bean:message key="my.description"/></td>
               <td class="bookText">    
       <html:textarea property="userDescription" rows="5" cols="40"/>
       不得超过50个汉字
       </td>
             </tr>
             <tr>
               <td class="bookText"><bean:message key="my.ifpublic"/></td>
               <td class="bookText">
        <html:radio property="userPublic" value="公开" ><bean:message key="public.true"/></html:radio>
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <html:radio property="userPublic" value="不公开" ><bean:message key="public.false"/></html:radio>
       &nbsp;&nbsp
       </td>
             </tr>
             <tr align="center">
               <td height="48" colspan="2" class="bookText"><table width="434" border="0" cellpadding="0" cellspacing="0">
                 <tr>
                   <td height="25" align="center" class="buttonText">
       <a href="/PersonalPlatform/Log.jsp">返回上一页</a>
       </td>
                   <td align="center" class="buttonText">
       <html:submit property="submit" value="提交"/>
       </td>
                   <td align="center" class="buttonText">
       <html:reset><bean:message key="Reset"/></html:reset>
       </td>
                 </tr>
               </table></td>
             </tr>
           </table>
       </html:form>    </td>
         </tr>
         <tr>
           <td height="63"></td>
         </tr>
         <tr>
           <td height="21" align="center" class="normalText"><table width="21%"  border="0">
             <tr>
               <td width="25%" align="right" class="normalText"><img src="pic/et.gif"></td>
               <td width="75%" align="center" class="normalText"><bean:message key="tail.label"/></td>
             </tr>
           </table></td>
         </tr>
         <tr>
           <td height="31"></td>
         </tr>
    </table>
    </body>数据库中的这个表为:
    CREATE TABLE `client` (
      `ZH` varchar(20) NOT NULL,
      `UPASSWORD` varchar(15) NOT NULL,
      `XM` varchar(40) default NULL,
      `XB` varchar(4) default NULL,
      `MAIL` varchar(50) default NULL,
      `PHONE` varchar(20) default NULL,
      `QQ` varchar(15) default NULL,
      `DESCRIPTION` varchar(100) default NULL,
      `IFPUBLIC` int(11) NOT NULL,
      PRIMARY KEY  (`ZH`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;那要怎么样才能将用户注册的信息保存到数据库中呢???请大侠帮帮忙。给出完整的实现一定马上给分。
      

  2.   

    晕倒 你用了struts啊 那再加上hibernate不就行了
      

  3.   

    struts是对应form的呀,你把form中的数据取出来,想怎么做都可以呀
    直接连数据库写个DAO就可以了,不过要自己写SQL,用hibernate也可以呀