how to implements jdbc connections using awt?

how to implements jdbc connections using awt?

My name is Aditya Shinde.I want to implement small project on awt.so please give me your valuable information about how to implements jdbc connections in awt.please give me sample example awt with jdbc.

View Answers

December 1, 2011 at 12:24 PM

We are proving you a simple application of Login and Registration using java swing.

1)LoginDemo.java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class LoginDemo extends JFrame{
JButton SUBMIT,ADD;
JPanel panel;
JLabel label1,label2;
final JTextField text1;
final JPasswordField text2;
LoginDemo(){
label1 = new JLabel();
label1.setText("UserName:");
text1 = new JTextField(15);
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("Login");
ADD=new JButton("Create Account");
panel=new JPanel(new GridLayout(4,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
panel.add(ADD);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
String user1="";
String pass1="";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM login where username='"+value1+"' && password='"+value2+"'");
while (res.next()) {
user1 = res.getString("username");
pass1 = res.getString("password");
}
if (value1.equals(user1) && value2.equals(pass1)) {
NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
page.getContentPane().add(label);
}
else{
JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
});
ADD.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
CreateAccount account=new CreateAccount();
account.setVisible(true);
}
});
}
public static void main(String arg[]) {
LoginDemo frame=new LoginDemo();
frame.pack();
frame.setVisible(true);
}
}

December 1, 2011 at 12:24 PM

continue..

2)CreateAccount.java:

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
class CreateAccount extends JFrame{
JTextField text1,text2,text3,text4,text5;
JPasswordField pass1;
JLabel label1,label2,label3,label4,label5,label6;
JPanel panel;
JButton button;
CreateAccount() {
text1=new JTextField(15);
text2=new JTextField(15);
text3=new JTextField(15);
pass1=new JPasswordField(15);
text4=new JTextField(15);
text5=new JTextField(15);
label1=new JLabel("First Name");
label2=new JLabel("Last Name");
label3=new JLabel("User Name");
label4=new JLabel("Password");
label5=new JLabel("Address");
label6=new JLabel("Phone No");
button=new JButton("Save");
panel=new JPanel(new GridLayout(7,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(pass1);
panel.add(label5);
panel.add(text4);
panel.add(label6);
panel.add(text5);
panel.add(button);

button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String value3=text3.getText();
String value4=pass1.getText();
String value5=text4.getText();
String value6=text5.getText();

Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
String user1="";
String pass1="";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
Statement st = con.createStatement();
ResultSet rs=st.executeQuery("Select * from login where username='"+value3+"'");
String usern="";
if(rs.next()){
usern=rs.getString("username");
JOptionPane.showMessageDialog(null,"Invalid UserName");
}
else {

int k=st.executeUpdate("insert into login(firstname,lastname,username,password,address,contactno) values('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"','"+value6+"')");
JOptionPane.showMessageDialog(null,"Data is successfully saved");
}

}
catch(Exception e){}
}
});
add(panel);
setSize(300,400);
setVisible(true);
}
}









Related Tutorials/Questions & Answers:
how to implements jdbc connections using awt
how to implements jdbc connections using awt?
Advertisements
jdbc connections
Connections with MicroSoft SQL - JDBC
how to insert data into databasse by using jdbc
how to insert data into databasse by using jdbc
how to insert data into databasse by using jdbc
how to insert data into databasse by using jdbc
how to create a databaseconnectvity using SQLSERVER2005 in J2ME - JDBC
how to connection jsp to oracle database connections in netbeans ide
how to connection jsp to oracle database connections in netbeans ide
using Blob in jdbc - JDBC
JDBC using Spring framework - JDBC
database entry using jdbc
multiple JComboBoxes using with JDBC
ModuleNotFoundError: No module named 'connections'
How sql Queries possible in DAO without creating Database connections - Java Beginners
how to make a radiobutton selected when retrieving data from database using struts framework and spring jdbc
Connecting to remote mysql server using jdbc.
How to Retrieve Excel data into mysql using type 2 JDBC-ODBC driver
not able to connect to mysql using.. jdbc
using pb datawindow activex with jdbc for mysql - JDBC
how to connect mysql with JDBC - JDBC
Which JDBC Type Using - Java Beginners
excel file using JDBC java.?
JDBC Versions
ModuleNotFoundError: No module named 'implements'
how to store image file and videofile in ms access database using java? - JDBC
How to create a data base connectivity using sqlserver or Mydql through J2ME - JDBC
ModuleNotFoundError: No module named 'django-connections'
ModuleNotFoundError: No module named 'mypy-boto3-codestar-connections'
ModuleNotFoundError: No module named 'nw-connections'
httpd.conf max connections
unable to retrive the data from mysql using servlet using jdbc driver
Connect to MS Acces wothout using ODBC but JDBC - JDBC
closing connections in finally block
courier management service (using servlets,jsp,jdbc)
spring mvc configuration with mysql using jdbc
ModuleNotFoundError: No module named 'django-user-connections'
ModuleNotFoundError: No module named 'django-user-connections'
Interact with connection pools to obtain and release connections
Jdbc Login Page Validation using Combobox
DOubt in Mysql connections
Expport data into CSV File using JDBC in Servlet
Connecting to the Database Using JDBC and Pure Java driver
how to resolve this JDBC Error?
maximum limit of database connections have been reached.
maximum limit of database connections have been reached.
Jdbc Login Page Validation using Combobox
Jdbc Login Page Validation using Combobox

Ads