Import Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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