Java Add records to database

Source: Internet
Author: User

Java add log to database tutorial
This should be a basic Java primer Oh, he is the first to connect the database, if the database connection is successful, then try to add the user submitted data to the datasheet OH.

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 ()
{
To load a bridging device:
Try
{
Class.forName ("Sun.jdbc.odbc.jdbcodbcdriver");
}
catch (ClassNotFoundException e) {}


}
To set the property value, get the method of the property value:
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;
}
To add a user table that is logged to the database:
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= "registered successfully";
Con.close ();
}
catch (SqlException E)
{
Message=e.getmessage ()//+ "http://down.111cn.net prompts you: You have not registered, or the user already exists, please change a name";
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.