asdfgmhgfdsadfgh
Machine Learning Attacker
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
Good day! This tutorial will focus on creating a user account information in java particularly in adding records to the database.
Now let's start this tutorial! :)
1. Create your database in ms access and named it sample.mdb with Login table and the following entities below.
2. Create your java program now named UserSettings.java.
3. Import the following libraries.
4. Initialize the following variables. 3 textfield for username, first name and family name, and 1 passwordfield for the password, 1 button named btnNew, and sql connection variables Connection, Statement, and PreparedStatement.
5. Create a constructor named UserSettings() same with your classname for creating the panels to put controls in the form as well as the connection in the database.
7. Create your ActionEvent for clicking the button. This will trigger to add records when clicking our btnNew button.
The sql syntax here "ps=cn.prepareStatement("INSERT INTO Login" + " (username,password,name1,name2) " + " VALUES(?,?,?,?)");" inserts the record in the database.
8. Create a method named clear to clear all your textfield.
9. Lastly create your Main. This will create the size and location of your form.
Output:
Here's the full code of this tutorial:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Download
Now let's start this tutorial! :)
1. Create your database in ms access and named it sample.mdb with Login table and the following entities below.


2. Create your java program now named UserSettings.java.
3. Import the following libraries.
- import
javax.swing.*
;
- import
java.awt.*
;
- import
java.awt.event.*
;
- import
java.sql.*
;
- import
java.lang.*
;
4. Initialize the following variables. 3 textfield for username, first name and family name, and 1 passwordfield for the password, 1 button named btnNew, and sql connection variables Connection, Statement, and PreparedStatement.
- JLabel
lblUser =
new
JLabel
(
"Username "
,JLabel
.RIGHT
)
;
- JLabel
lblPass =
new
JLabel
(
"Password "
,JLabel
.RIGHT
)
;
- JLabel
lblName1 =
new
JLabel
(
"First Name"
,JLabel
.RIGHT
)
;
- JLabel
lblName2 =
new
JLabel
(
"Family Name"
,JLabel
.RIGHT
)
;
- JTextField
txtUser =
new
JTextField
(
20
)
;
- JPasswordField
txtPass=
new
JPasswordField
(
20
)
;
- JTextField
txtName1=
new
JTextField
(
20
)
;
- JTextField
txtName2=
new
JTextField
(
20
)
;
- JButton
btnNew =
new
JButton
(
"Add"
)
;
- Connection
cn;
- Statement
st;
- PreparedStatement
ps;
5. Create a constructor named UserSettings() same with your classname for creating the panels to put controls in the form as well as the connection in the database.
- public
UserSettings(
)
{
- super
(
"User Account Settings"
)
;
- JPanel
pane =
new
JPanel
(
)
;
- pane.setLayout
(
null
)
;
- lblUser.setBounds
(
5
,50
,80
,25
)
;
- pane.add
(
lblUser)
;
- txtUser.setBounds
(
90
,50
,150
,25
)
;
- pane.add
(
txtUser)
;
- lblUser.setForeground
(
Color
.white
)
;
- lblPass.setBounds
(
5
,85
,80
,25
)
;
- pane.add
(
lblPass)
;
- txtPass.setBounds
(
90
,85
,150
,25
)
;
- txtPass.setEchoChar
(
'*'
)
;
- pane.add
(
txtPass)
;
- lblPass.setForeground
(
Color
.white
)
;
- lblName1.setBounds
(
5
,120
,80
,25
)
;
- pane.add
(
lblName1)
;
- txtName1.setBounds
(
90
,120
,150
,25
)
;
- pane.add
(
txtName1)
;
- lblName1.setForeground
(
Color
.white
)
;
- lblName2.setBounds
(
5
,155
,80
,25
)
;
- pane.add
(
lblName2)
;
- txtName2.setBounds
(
90
,155
,150
,25
)
;
- pane.add
(
txtName2)
;
- lblName2.setForeground
(
Color
.white
)
;
- btnNew.setBounds
(
5
,190
,75
,35
)
;
- pane.add
(
btnNew)
;
- btnNew.addActionListener
(
this
)
;
- pane.setBackground
(
Color
.black
)
;
- setContentPane(
pane)
;
- setDefaultCloseOperation(
JFrame
.DISPOSE_ON_CLOSE
)
;
- pane.setBorder
(
BorderFactory
.createTitledBorder
(
- BorderFactory
.createEtchedBorder
(
)
, "Creating User Account"
)
)
;
- try
{
- Class
.forName
(
"sun.jdbc.odbc.JdbcOdbcDriver"
)
;
- cn =
DriverManager
.getConnection
(
"jdbc:odbc:User"
)
;
- }
catch
(
ClassNotFoundException
e)
{
- System
.err
.println
(
"Failed to load driver"
)
;
- e.printStackTrace
(
)
;
- }
- catch
(
SQLException
e)
{
- System
.err
.println
(
"Unable to connect"
)
;
- e.printStackTrace
(
)
;
- }
- }
7. Create your ActionEvent for clicking the button. This will trigger to add records when clicking our btnNew button.
- public
void
actionPerformed(
ActionEvent
e)
{
- Object
source =
e.getSource
(
)
;
- if
(
source ==
btnNew)
{
- try
{
- String
uname=
txtUser.getText
(
)
;
- String
pass=
txtPass.getText
(
)
;
- String
name1=
txtName1.getText
(
)
;
- String
name2=
txtName2.getText
(
)
;
- if
(
!
uname.equals
(
""
)
&&
!
pass.equals
(
""
)
&&
!
name1.equals
(
""
)
&&
!
name2.equals
(
""
)
)
{
- st=
cn.createStatement
(
)
;
- ps=
cn.prepareStatement
(
"INSERT INTO Login"
+
" (username,password,name1,name2) "
+
" VALUES(?,?,?,?)"
)
;
- ps.setString
(
1
,txtUser.getText
(
)
)
;
- ps.setString
(
2
,txtPass.getText
(
)
)
;
- ps.setString
(
3
,txtName1.getText
(
)
)
;
- ps.setString
(
4
,txtName2.getText
(
)
)
;
- ps.executeUpdate
(
)
;
- JOptionPane
.showMessageDialog
(
null
,"New account has been successfully added."
,"Payroll System: User settings"
,JOptionPane
.INFORMATION_MESSAGE
)
;
- txtUser.requestFocus
(
true
)
;
- st.close
(
)
;
- clear(
)
;
- }
- else
{
- JOptionPane
.showMessageDialog
(
null
,"Please Fill Up The Empty Fields"
,"Warning"
,JOptionPane
.WARNING_MESSAGE
)
;
- }
- }
catch
(
SQLException
sqlEx)
{
- sqlEx.printStackTrace
(
)
;
- JOptionPane
.showMessageDialog
(
null
,"Unable to save!."
,"Payroll System: User settings"
,JOptionPane
.ERROR_MESSAGE
)
;
}
- }
- }
The sql syntax here "ps=cn.prepareStatement("INSERT INTO Login" + " (username,password,name1,name2) " + " VALUES(?,?,?,?)");" inserts the record in the database.
8. Create a method named clear to clear all your textfield.
- public
void
clear(
)
{
- txtUser.setText
(
""
)
;
- txtPass.setText
(
""
)
;
- txtName1.setText
(
""
)
;
- txtName2.setText
(
""
)
;
- }
9. Lastly create your Main. This will create the size and location of your form.
- public
static
void
main(
String
[
]
args)
{
- UserSettings panel =
new
UserSettings(
)
;
- panel.setSize
(
370
,350
)
;
- panel.setVisible
(
true
)
;
- panel.setLocation
(
350
,200
)
;
- panel.setResizable
(
false
)
;
- }
Output:


Here's the full code of this tutorial:
- import
javax.swing.*
;
- import
java.awt.*
;
- import
java.awt.event.*
;
- import
java.sql.*
;
- import
java.lang.*
;
- public
class
UserSettings extends
JFrame
implements
ActionListener
{
- JLabel
lblUser =
new
JLabel
(
"Username "
,JLabel
.RIGHT
)
;
- JLabel
lblPass =
new
JLabel
(
"Password "
,JLabel
.RIGHT
)
;
- JLabel
lblName1 =
new
JLabel
(
"First Name"
,JLabel
.RIGHT
)
;
- JLabel
lblName2 =
new
JLabel
(
"Family Name"
,JLabel
.RIGHT
)
;
- JTextField
txtUser =
new
JTextField
(
20
)
;
- JPasswordField
txtPass=
new
JPasswordField
(
20
)
;
- JTextField
txtName1=
new
JTextField
(
20
)
;
- JTextField
txtName2=
new
JTextField
(
20
)
;
- JButton
btnNew =
new
JButton
(
"Add"
)
;
- Connection
cn;
- Statement
st;
- PreparedStatement
ps;
- public
void
clear(
)
{
- txtUser.setText
(
""
)
;
- txtPass.setText
(
""
)
;
- txtName1.setText
(
""
)
;
- txtName2.setText
(
""
)
;
- }
- public
UserSettings(
)
{
- super
(
"User Account Settings"
)
;
- JPanel
pane =
new
JPanel
(
)
;
- pane.setLayout
(
null
)
;
- lblUser.setBounds
(
5
,50
,80
,25
)
;
- pane.add
(
lblUser)
;
- txtUser.setBounds
(
90
,50
,150
,25
)
;
- pane.add
(
txtUser)
;
- lblUser.setForeground
(
Color
.white
)
;
- lblPass.setBounds
(
5
,85
,80
,25
)
;
- pane.add
(
lblPass)
;
- txtPass.setBounds
(
90
,85
,150
,25
)
;
- txtPass.setEchoChar
(
'*'
)
;
- pane.add
(
txtPass)
;
- lblPass.setForeground
(
Color
.white
)
;
- lblName1.setBounds
(
5
,120
,80
,25
)
;
- pane.add
(
lblName1)
;
- txtName1.setBounds
(
90
,120
,150
,25
)
;
- pane.add
(
txtName1)
;
- lblName1.setForeground
(
Color
.white
)
;
- lblName2.setBounds
(
5
,155
,80
,25
)
;
- pane.add
(
lblName2)
;
- txtName2.setBounds
(
90
,155
,150
,25
)
;
- pane.add
(
txtName2)
;
- lblName2.setForeground
(
Color
.white
)
;
- btnNew.setBounds
(
5
,190
,75
,35
)
;
- pane.add
(
btnNew)
;
- btnNew.addActionListener
(
this
)
;
- pane.setBackground
(
Color
.black
)
;
- setContentPane(
pane)
;
- setDefaultCloseOperation(
JFrame
.DISPOSE_ON_CLOSE
)
;
- pane.setBorder
(
BorderFactory
.createTitledBorder
(
- BorderFactory
.createEtchedBorder
(
)
, "Creating User Account"
)
)
;
- try
{
- Class
.forName
(
"sun.jdbc.odbc.JdbcOdbcDriver"
)
;
- cn =
DriverManager
.getConnection
(
"jdbc:odbc:User"
)
;
- }
catch
(
ClassNotFoundException
e)
{
- System
.err
.println
(
"Failed to load driver"
)
;
- e.printStackTrace
(
)
;
- }
- catch
(
SQLException
e)
{
- System
.err
.println
(
"Unable to connect"
)
;
- e.printStackTrace
(
)
;
- }
- }
- public
void
actionPerformed(
ActionEvent
e)
{
- Object
source =
e.getSource
(
)
;
- if
(
source ==
btnNew)
{
- try
{
- String
uname=
txtUser.getText
(
)
;
- String
pass=
txtPass.getText
(
)
;
- String
name1=
txtName1.getText
(
)
;
- String
name2=
txtName2.getText
(
)
;
- if
(
!
uname.equals
(
""
)
&&
!
pass.equals
(
""
)
&&
!
name1.equals
(
""
)
&&
!
name2.equals
(
""
)
)
{
- st=
cn.createStatement
(
)
;
- ps=
cn.prepareStatement
(
"INSERT INTO Login"
+
" (username,password,name1,name2) "
+
" VALUES(?,?,?,?)"
)
;
- ps.setString
(
1
,txtUser.getText
(
)
)
;
- ps.setString
(
2
,txtPass.getText
(
)
)
;
- ps.setString
(
3
,txtName1.getText
(
)
)
;
- ps.setString
(
4
,txtName2.getText
(
)
)
;
- ps.executeUpdate
(
)
;
- JOptionPane
.showMessageDialog
(
null
,"New account has been successfully added."
,"Payroll System: User settings"
,JOptionPane
.INFORMATION_MESSAGE
)
;
- txtUser.requestFocus
(
true
)
;
- st.close
(
)
;
- clear(
)
;
- }
- else
{
- JOptionPane
.showMessageDialog
(
null
,"Please Fill Up The Empty Fields"
,"Warning"
,JOptionPane
.WARNING_MESSAGE
)
;
- }
- }
catch
(
SQLException
sqlEx)
{
- sqlEx.printStackTrace
(
)
;
- JOptionPane
.showMessageDialog
(
null
,"Unable to save!."
,"Payroll System: User settings"
,JOptionPane
.ERROR_MESSAGE
)
;
}
- }
- }
- // public void frameUser(){
- public
static
void
main(
String
[
]
args)
{
- UserSettings panel =
new
UserSettings(
)
;
- panel.setSize
(
370
,350
)
;
- panel.setVisible
(
true
)
;
- panel.setLocation
(
350
,200
)
;
- panel.setResizable
(
false
)
;
- }
- }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Download
You must upgrade your account or reply in the thread to view hidden text.