0% found this document useful (0 votes)
33 views8 pages

JDBC and Others

The document contains Java code for a GUI application that facilitates user login and registration, connects to a MySQL database, and handles user input validation. It also includes a simple chat server and client implementation using sockets, allowing for communication between a server and a client. Additionally, the document features a file handling example that demonstrates how to create, write to, and read from a file.

Uploaded by

Tesfaye Desalegn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views8 pages

JDBC and Others

The document contains Java code for a GUI application that facilitates user login and registration, connects to a MySQL database, and handles user input validation. It also includes a simple chat server and client implementation using sockets, allowing for communication between a server and a client. Additionally, the document features a file handling example that demonstrates how to create, write to, and read from a file.

Uploaded by

Tesfaye Desalegn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

public class LOGINToRegistration extends javax.swing.

JFrame {

final static String JDBC_DRIVER = "com.mysql.jdbc.Driver";

final static String DB_URL = "jdbc:mysql://localhost:3306/thirdyear";

final static String USER_NAME = "root";

final static String PASSWORD = "";

public LOGINToRegistration() {

initComponents();

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void LOGINToRegistrationActionPerformed(java.awt.event.ActionEvent evt) {

if(jTextField1.getText().length()== 0 && jPasswordField1.getPassword().length == 0 ){ // Checking for


empty field

jLabel1.setText("Empty fields detected ! Please fill up both");}

else if( jTextField1.getText().length()== 0 ) // Checking for empty field

jLabel1.setText("Empty fields detected ! Please fill up username");

else if( jPasswordField1.getPassword().length == 0 ) // Checking for empty field


jLabel1.setText("Empty fields detected ! Please fill up password");

else

{ String u = jTextField1.getText(); // Collecting the input

String p = jPasswordField1.getText();

try{

Class.forName(JDBC_DRIVER);

Connection con = DriverManager.getConnection(DB_URL ,USER_NAME,PASSWORD);

Statement stmt = con.createStatement();

String logqry = "select * from login";

ResultSet rs = stmt.executeQuery(logqry);

while(rs.next())

{ if( u.equals(rs.getString("Username"))&& p.equals(rs.getString("Password")))

this.hide();

new field().setVisible(true);

else{

jLabel1.setText("Wrong Username");

}catch (SQLException ex)

{ Logger.getLogger(DBTest_JFrame.class.getName()).log(Level.SEVERE, null, ex); } catch


(ClassNotFoundException ex) {

Logger.getLogger(LOGINToRegistration.class.getName()).log(Level.SEVERE, null, ex);

jTextField1.setText("");

jPasswordField1.setText("");

}
public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new LOGINToRegistration().setVisible(true);

});

private void RegisterActionPerformed(java.awt.event.ActionEvent evt) {

try {

Class.forName(JDBC_DRIVER); // load driver

con = DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);

insqry = "insert into student values(?,?,?,?)";

pstmt = con.prepareStatement(insqry );

pstmt.setString( 1 , jTextField1.getText() );

pstmt.setString( 2, jTextField2.getText() );

pstmt.setString( 3, jComboBox1.getItemAt(0|1) );
pstmt.setString( 4, jTextField3.getText() );

pstmt.executeUpdate();

jTextArea1.setText(" 1 record is inserted" );

pstmt.close(); con.close();

} catch (SQLException ex) {

Logger.getLogger(field.class.getName()).log(Level.SEVERE, null, ex);

ex.printStackTrace();

} catch (ClassNotFoundException ex) {

Logger.getLogger(field.class.getName()).log(Level.SEVERE, null, ex);

private void ViewActionPerformed(java.awt.event.ActionEvent evt) {

try{

Class.forName(JDBC_DRIVER); // load driver

con = DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);

stmt = con.createStatement();

selqry = "SELECT * FROM student";

rst = stmt.executeQuery(selqry);

while(rst.next() )

{ jTextArea1.setText("\nSTUDID\tFName\tSex\tAge"+"\n\n"

+rst.getString(1) + "\t" + rst.getString(2) +

"\t" + rst.getString(3) + "\t" + rst.getInt(4));

rst.close(); stmt.close(); con.close();

}catch(Exception ex){

ex.printStackTrace();

}
///////////////////////////////////////////////////////////////////////////////////////

import java.io.*;

import java.net.*;

public class chatserver

public static void main(String [] args)throws IOException

ServerSocket sockserv = new ServerSocket(1025 );

System.out.println("Server is ready to chat....");

Socket cl = sockserv.accept();

OutputStream out = cl.getOutputStream();

PrintWriter pw = new PrintWriter(out,true);

BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));

InputStream in = cl.getInputStream();

BufferedReader br2 = new BufferedReader(new InputStreamReader(in));

String rxc, syc;

while(true)

{ if((rxc = br2.readLine()) != null)

System.out.println("recieve from client: "+rxc);

syc = br1.readLine();

pw.println(syc);

System.out.println("sending to client: ");


}

//////////////////////////////////////////////////////////////////////////////////////

import java.io.*;

import java.net.*;

public class chatclient

public static void main(String [] args)throws IOException

Socket cl = new Socket("localhost",1025);

OutputStream out = cl.getOutputStream();

PrintWriter pw = new PrintWriter(out,true);

BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));

InputStream in = cl.getInputStream();

BufferedReader br2 = new BufferedReader(new InputStreamReader(in));

System.out.println("start chatting");

String rxc, syc;

while(true)

syc = br1.readLine();

pw.println(syc);
System.out.println("sending to server: ");

if((rxc = br2.readLine()) != null)

System.out.println("recieve from server: "+rxc);

////////////////////////////////////////////////////////////////////////////////////////////

package chaptertwo;

import java.io.*;

import java.util.Scanner;

public class FileCreateAndWriteAndRead // MyFileWriter

{ public static void main(String[] args) throws IOException

{ FileOutputStream fout = new FileOutputStream("C:\\Users\\user pc\\Desktop\\year3.txt");

Scanner scan = new Scanner(System.in);

System.out.println("Please, Enter your String: ");

String str = scan.next();

//String str = "this is file writing Example";

byte xx[] = str.getBytes();

for (int i = 0; i < xx.length; i++)

fout.write(xx[i]);
}

fout.close();

System.out.println("File created");

/* To Read created file */

FileInputStream fr = new FileInputStream("filewrt.txt" );

byte b[] = new byte[fr.available()]; /*size of data with fr.available method */

fr.read(b); //reading the data from Input Stream

String ss = new String(b); //The byte type array is converted into String

System.out.println("The created is: " + ss); //Then String is printed on screen

fr.close();

You might also like