public int input(String sql){
int n=0;
if(stmt!=null){
try {
n=stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
else{
return 0;
}
return n;
}怎么调用这个(上面的代码是数据库连接中的一个类)来把这个注册的功能写到MySQL数据库中用户名:  
密 码:  
密 码确认:  
生日:  
手机号码:  
年龄:  
  

写入的数据库

解决方案 »

  1.   

    我理解的意思 你想把注册信息插入或者更新到数据库?  那把用户名:
    密 码:
    密 码确认:
    生日:
    手机号码:
    年龄:
    这些参数都写到SQL里吧- - 不过一般都不这么写。。
      

  2.   

    if(stmt!=null)从哪里传来的值,是判断你输入的东西是否为空吗,建议你把代码贴出来看看
      

  3.   

    package dao;import java.sql.*;
    import java.util.*;public class DbManager {
        public DbManager() {
            try {
                jbInit();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }    private Connection conn = null;
        private Statement stmt = null;
        private ResultSet rs = null;
        private void getStmt(String db) {
            try {
              //加载驱动
                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                //创建连接
                String url = "jdbc:jtds:sqlserver://localhost:1433/" +
                             db;
                conn = DriverManager.getConnection(url,"sa","baolian");
                //创建语句对象
                stmt = conn.createStatement();
            } catch (Exception ex) {
                System.out.println("创建语句对象失败");
            }
        }    //******************************************
      //增删改方法
         public int exeSql(String db, String sql) {
             int i = 0;
             try {
                 System.out.println(sql);
                 this.getStmt(db);
                 i = stmt.executeUpdate(sql);
                 stmt.close();
                 conn.close();
             } catch (Exception ex) {
                 System.out.println("执行SQL失败");
             }
             return i;
         }    //******************************************
         //查询方法(返回值为结果集)
         public ResultSet getRs(String db, String sql) {
             try {
                 this.getStmt(db);
                 rs = stmt.executeQuery(sql);
             } catch (Exception ex) {
                 System.out.println("查询失败");
             }
             return rs;
         }    //******************************************
         //关闭方法
         public void close() {
             try {
                 if (rs != null) {
                     rs.close();
                 }
                 if (stmt != null) {
                     stmt.close();
                 }
                 if (conn != null) {
                     conn.close();
                 }
             } catch (Exception ex) {
                 System.out.println("关闭失败");
             }
         }    //******************************************
         //查询方法(返回值为Vector)
         public Vector getRows(String db, String sql) {
             Vector rows = new Vector();
             try {
                 this.getStmt(db);
                 rs = stmt.executeQuery(sql);
                 while (rs.next()) {
                     Vector row = new Vector();
                     for (int i = 1; i < rs.getMetaData().getColumnCount(); i++) {
                         row.add(rs.getString(i));
                     }
                     rows.add(row);
                 }
                 rs.close();
                 stmt.close();
                 conn.close();
             } catch (Exception ex) {
                 System.out.println("查询失败");
             }
             return rows;
         }    private void jbInit() throws Exception {
        }
    }
      

  4.   

    这个是mysql连接的代码
    package action;import java.sql.*;public class DataBase{
    private Connection conn = null;
    private Statement stmt = null;
    private ResultSet rs = null; public DataBase() throws Exception {
    if(conn==null){
    try {
    Class.forName("com.mysql.jdbc.Driver");
    conn=DriverManager.getConnection("jdbc:mysql://localhost/haut","root","1111");
    stmt = conn.createStatement();
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }
    }
    public ResultSet read(String sql) throws Exception{
    try{
    if(stmt!=null){
    rs=stmt.executeQuery(sql);
    }
    else{
    return null;
    }
    }
    catch(Exception e){
    throw e;
    }
    return rs;
    }


    public int input(String sql){
    int n=0;
    if(stmt!=null){
    try {
    n=stmt.executeUpdate(sql);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    else{
    return 0;
    }
    return n;
    }

    }
    ------------------------------------------------------------------
    这个是注册
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package action;import java.sql.ResultSet;
    import java.util.Date;import com.opensymphony.xwork2.ActionSupport;/**
     * 
     * @author frankwu
     * 
     */
    public class RegisterAction extends ActionSupport {
    // Action类公用私有变量,用来做页面导航标志
    private static String FORWARD = null; // 用户名属性
    private String username; // 密码属性
    private String password; // 确认密码属性
    private String repassword; // 生日属性
    private Date birthday; // 手机号码属性
    private String mobile; // 年龄属性
    private int age; // 取得用户名值
    public String getUsername() {
    return username;
    } // 设置用户名值
    public void setUsername(String username) {
    this.username = username;
    } // 取得密码值
    public String getPassword() {
    return password;
    } // 设置密码值
    public void setPassword(String password) {
    this.password = password;
    } // 取得确认密码值
    public String getRepassword() {
    return repassword;
    } // 设置确认密码值
    public void setRepassword(String repassword) {
    this.repassword = repassword;
    } // 取得生日值
    public Date getBirthday() {
    return birthday;
    } // 设置生日值
    public void setBirthday(Date birthday) {
    this.birthday = birthday;
    } // 取得手机号码值
    public String getMobile() {
    return mobile;
    } // 设置手机号码值
    public void setMobile(String mobile) {
    this.mobile = mobile;
    } // 取得年龄值
    public int getAge() {
    return age;
    } // 设置年龄值
    public void setAge(int age) {
    this.age = age;
    }


    // public String execute() {
    // DataBase db = null;
    // try {
    // db = new DataBase();
    // } catch (Exception e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // String sql="insert into user values('username','password','repassword','birthday','tel','age')";
    // db.input(sql);
    // return sql;
    // }

    // 执行注册方法    select * from user where username='"+username+"' and password='"+password+"'
    public String execute() throws Exception {
    FORWARD = "success"; return FORWARD;
    }
    }
    我就是想把内容插入到数据库中