0% found this document useful (0 votes)
49 views

Import Java

This document contains code for connecting to a database and performing basic CRUD operations in Java. It defines a database_connect class with methods to establish a connection, insert data into a table, and retrieve data from a table. The main method demonstrates inserting a record and retrieving all records from a table. A second code sample creates a simple GUI with a label, text field, button, and checkbox to write input text to a file when the button is clicked.

Uploaded by

Jamal Sharif
Copyright
© Attribution Non-Commercial (BY-NC)
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)
49 views

Import Java

This document contains code for connecting to a database and performing basic CRUD operations in Java. It defines a database_connect class with methods to establish a connection, insert data into a table, and retrieve data from a table. The main method demonstrates inserting a record and retrieving all records from a table. A second code sample creates a simple GUI with a label, text field, button, and checkbox to write input text to a file when the button is clicked.

Uploaded by

Jamal Sharif
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 3

import java.sql.

*;

public class database_connect {


private ResultSet rs;
private Statement st;
private Connection con;

private void mycon(){


try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:student");
}
catch(ClassNotFoundException e){
System.out.println("Class Not Found::");
}
catch(SQLException e1){
System.out.println("SQL Error1");
}
}

public void idata(String table, int rno, String name){


try {
mycon();
String query;
int result = 0;
query = "Inserted into" + table + "values(Roll Number, Student Name)" + rno + name;
st = con.createStatement();
result = st.executeUpdate(query);
if (result>0)
System.out.println("Value inserted");
else
System.out.println("Values not inserted");
}
catch(SQLException e){
System.out.println("Data not inserted..");
}
}

public ResultSet showall(String table){


try{
mycon();
st = con.createStatement();
rs = st.executeQuery("select from " + table);
}
catch(SQLException e){
System.out.println("SQL Error2");
}
return rs;
}

public static void main(String[] args){


ResultSet rs;
try {
database_connect db1 = new database_connect();
rs=db1.showall("std");
db1.idata("std",345,"bilal");
while (rs.next()){
System.out.print(rs.getString(1) +"\t");
System.out.println(rs.getString(2));
}
}
catch(SQLException e2){
System.out.println("SQL Error3");
}
}
}

GUI
//window.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class window {
static JFrame window = new JFrame();
static JButton b1;
static JCheckBox check;
static JLabel lb;
static Container content = window.getContentPane();
static JTextField txt;

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


// main windows
window.setTitle("Welcome to my Screen");
content.setBackground(Color.LIGHT_GRAY);
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
window.setBounds(center.x/4, center.y/4, 1024, 768);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setResizable(false);
FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
content.setLayout(layout);
//label
lb = new JLabel("Name:");
content.add(lb);
//text field
txt = new JTextField(20);
content.add(txt);

// add button
b1= new JButton("Ok");
content.add(b1);
b1.setBounds(100, 50, 200, 200);
// check box
check = new JCheckBox("BSCS",false);
content.add(check);
// action of button
b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {


try {
String text = txt.getText();
File write = new File("bscs.txt");
PrintWriter writer = new PrintWriter(write);
writer.println(text);
}
catch(FileNotFoundException e1){
e1.getMessage();
}

content.setBackground(Color.blue);
}
});
//checkbox event
check.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
b1.setEnabled(false);
}
});

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy