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";
}
}
}