Code: Save Button Code
11:09 AM
Kalpa
,
0 Comments
This code is little bit difficult to understand than other code. This code want your basic SQL Language knowledge. I'll put SQL notes presently. Now you can learn it from using this link.
Basic SQL
In this problem first you have to create a database and then create a table. I create Database called SWOWNER and create table called NAME.
And i add another field called ID(Jtextfield3) in our form. We can doing this without that field. but i think it is easy for you if i add that field.
in my table(Name) ID is the primary key.
first you have to open the sql console or there have GUI based sql ide such as MySQL Control Center, PHP My admin(this is web based sql gui ide).
Code for create database.
USE SWOWNER;
Then you have to create a table in the SWOWNER database called as NAME.(there's no rule that the Database name and table names are writing Block letters. Its for you for understand easy). In the table there have three field called ID(int), Fname(varchar) and Lname(varchar).
Code for create table.
before coding to save button first you have to create db class.
right click on the your package that consist in source package >> New >> Java class >> db >> Finish
now it create db class. first you have to import sql drivers(i dont know what is the real name for that). To that we can use following command
import java.sql.*;
if we put * all of drivers are import. When we use netbeance ide it is automatically import this drivers. then write follwing commands in the db class.
DriverManager.getConnection("jdbc:mysql://localhost:3306/Database_Name", "SQL_username", "SQL_password");
if we didn't use this db class not problem. the problem is we have to write above command all place where we connected java and sql. so i think crate db class is suitable and its very clear full.
before code the save button you have to add mysql-connector-java-5.0.7-bin.jar to libraries. To know about how to download and installed mysql java connecter click here.
to add java sql connector file to the netbeance right click on the libraries>> Add JAR/Folder >> select the file>>open. If it add it shows like following picture.
OKKkk. now we can write code for save button.
Save Button Command
try {
Connection con = db.getMyConnection();
Statement stmt = con.createStatement();
String sql=" insert into NAME() values('"+ jTextField1.getText() +"','"+ jTextField2.getText() +"','"+jTextField3.getText()+"')";
stmt.executeUpdate(sql);
}
catch (Exception e)
{
e.printStackTrace();
}
OR
try
{ String fn=jTextField1.getText();
String ln=jTextField2.getText();
String id=jTextField3.getText();
Connection con = db.getMyConnection();
Statement stmt = con.createStatement();
String sql="insert into NAME() values('"+ fn +"','"+ ln +"','"+id+"')";
stmt.executeUpdate(sql);
}
catch (Exception e) {
e.printStackTrace();
}
String sql=" insert into Table_Name() values( ' "+ jTextField1.getText() + " ' ,'"+ jTextField2.getText() +"','"+jTextField3.getText()+"')";
jTextField1.getText() :>>this code means that get all data that in the jtextField1. this code write inside in ' " + + " ' (don't forget)
insert into Table_Name() :>> this code is sql code. we can put out column name in the bracket. If we didn't use all fields(columns) to save data we should write the coloumn names that we use to save data
i think that second code is good if you use. because it is very clear if we use large number of feilds for table and we can check each textfield.(soon on).
Ohh its over. I'm so tired. Get a brake.>>>>
Basic SQL
In this problem first you have to create a database and then create a table. I create Database called SWOWNER and create table called NAME.
And i add another field called ID(Jtextfield3) in our form. We can doing this without that field. but i think it is easy for you if i add that field.
in my table(Name) ID is the primary key.
first you have to open the sql console or there have GUI based sql ide such as MySQL Control Center, PHP My admin(this is web based sql gui ide).
Code for create database.
SyntaxThen go in to the database using following commands
CREATE DATABASE database_name
CREATE DATABASE SWOWNER;
Syntax
USE DATABASE_NAME;USE SWOWNER;
Then you have to create a table in the SWOWNER database called as NAME.(there's no rule that the Database name and table names are writing Block letters. Its for you for understand easy). In the table there have three field called ID(int), Fname(varchar) and Lname(varchar).
Code for create table.
Syntax
CREATE TABLE table_name(colomn_name1 data_type,colomn_name2 data_type,...)okkkkk this are all about sql. not about java. now i write code for save button. so double click on the save(JButton1) button.
CREATE TABLE NAME(
ID int NOT NULL
Fname varchar(255) NOT NULL
Lname varchar(255)
PRIMARY KEY(ID)
)
in this table ID and Fname colomn aren't be null. it should be have value and Lname can be null.
The primary key is ID.
before coding to save button first you have to create db class.
right click on the your package that consist in source package >> New >> Java class >> db >> Finish
now it create db class. first you have to import sql drivers(i dont know what is the real name for that). To that we can use following command
import java.sql.*;
if we put * all of drivers are import. When we use netbeance ide it is automatically import this drivers. then write follwing commands in the db class.
private static Connection con;
public static Connection getMyConnection() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SWOWNER", "root", "");
return con;
}
public static Connection getMyConnection() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SWOWNER", "root", "");
return con;
}
Syntax
DriverManager.getConnection("jdbc:mysql://localhost:3306/Database_Name", "SQL_username", "SQL_password");
if we didn't use this db class not problem. the problem is we have to write above command all place where we connected java and sql. so i think crate db class is suitable and its very clear full.
before code the save button you have to add mysql-connector-java-5.0.7-bin.jar to libraries. To know about how to download and installed mysql java connecter click here.
to add java sql connector file to the netbeance right click on the libraries>> Add JAR/Folder >> select the file>>open. If it add it shows like following picture.
OKKkk. now we can write code for save button.
Save Button Command
try {
Connection con = db.getMyConnection();
Statement stmt = con.createStatement();
String sql=" insert into NAME() values('"+ jTextField1.getText() +"','"+ jTextField2.getText() +"','"+jTextField3.getText()+"')";
stmt.executeUpdate(sql);
}
catch (Exception e)
{
e.printStackTrace();
}
OR
try
{ String fn=jTextField1.getText();
String ln=jTextField2.getText();
String id=jTextField3.getText();
Connection con = db.getMyConnection();
Statement stmt = con.createStatement();
String sql="insert into NAME() values('"+ fn +"','"+ ln +"','"+id+"')";
stmt.executeUpdate(sql);
}
catch (Exception e) {
e.printStackTrace();
}
Syntax
String sql=" insert into Table_Name() values( ' "+ jTextField1.getText() + " ' ,'"+ jTextField2.getText() +"','"+jTextField3.getText()+"')";
jTextField1.getText() :>>this code means that get all data that in the jtextField1. this code write inside in ' " + + " ' (don't forget)
insert into Table_Name() :>> this code is sql code. we can put out column name in the bracket. If we didn't use all fields(columns) to save data we should write the coloumn names that we use to save data
i think that second code is good if you use. because it is very clear if we use large number of feilds for table and we can check each textfield.(soon on).
Ohh its over. I'm so tired. Get a brake.>>>>
You can leave a response, or trackback from your own site.
0 Response to "Code: Save Button Code"
Post a Comment