java 添加記錄到資料庫

來源:互聯網
上載者:User

java 添加記錄到資料庫教程
這應該是一款基本的java入門教程哦,他是先串連資料庫,如果資料庫連接成功後,再嘗試把使用者提交的資料添加到資料表哦。

import java.sql.*;

public class register
{
    string logname,realname,password,email,phone,address;
    string message;
    connection con;
    statement sql;
    resultset rs;
    public static void main(string args[])
    {
        register my=new register();
        my.logname="b";
        my.realname="b";
        my.password="b";
        my.email="www.111cn.net";
        my.phone="b";
        my.address="b";
        my.additem();
        system.out.println(my.message);
    }
    public register()
    {
        //載入橋接器:
        try
        {
            class.forname("sun.jdbc.odbc.jdbcodbcdriver");
        }
        catch(classnotfoundexception e){}
       
       
    }
    //設定屬性值、擷取屬性值的方法:
    public void setlogname(string name)
    {
        logname=name;
    }
    public string getlogname()
    {
        return logname;
    }
    public void setrealname(string name)
    {
        realname=name;
    }
    public string getrealname()
    {
        return realname;
    }
    public void setpassword(string pw)
    {
        password=pw;
    }
    public string getpassword()
    {
        return password;
    }
    public void setemail(string em)
    {
        email=em;
    }
    public string getemail()
    {
        return email;
    }
    public void setphone(string ph)
    {
        phone=ph;
    }
    public string getphone()
    {
        return phone;
    }
    public void setaddress(string ad)
    {
        address=ad;
    }
    public string getaddress()
    {
        return address;
    }
    public string getmessage()
    {
        return message;
    }
    //添加記錄到資料庫的user表:
    public void additem()
    {

        try
        {
            con=drivermanager.getconnection("jdbc:odbc:shop","","");
            sql=con.createstatement();
            string s="'"+logname+"'"+","+"'"+realname+"'"+","+"'"+password+"'"+","+"'"+
                                email+"'"+","+"'"+phone+"'"+","+"'"+address+"'";
            string condition="insert into user values"+"("+s+")";
            sql.executeupdate(condition);
            message="註冊成功了";
            con.close();
        }
        catch(sqlexception e)
        {
            message=e.getmessage();//+"http://down.111cn.net提示你:你還沒有註冊,或該使用者已經存在,請你更換一個名字";
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.