0% found this document useful (0 votes)
40 views4 pages

"Demosql": // Phan Tên CSDL

This Java program creates a graphical user interface (GUI) to interact with a SQL database. It allows the user to select a database, enter a SQL query, and displays the query results in a table. The program imports Java GUI and SQL libraries. It defines classes to create the window frames, text fields, buttons for interaction and methods to connect to the database, execute queries, and display results.

Uploaded by

luthimaithanh
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

"Demosql": // Phan Tên CSDL

This Java program creates a graphical user interface (GUI) to interact with a SQL database. It allows the user to select a database, enter a SQL query, and displays the query results in a table. The program imports Java GUI and SQL libraries. It defines classes to create the window frames, text fields, buttons for interaction and methods to connect to the database, execute queries, and display results.

Uploaded by

luthimaithanh
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

package 

CSDL;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class DemoSQL extends JFrame implements ActionListener 
{
    
    private JTextField nameData,select;
    private JButton ok,clear,query;
    private JPanel display,control;
    private Connection con ;
    
    public DemoSQL(){
        super("DemoSQL");
        setSize(600, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        
        setLayout(new BorderLayout());
        addZoom();
        control.setVisible(false);
        display.setVisible(false);
    }
    
    private void addZoom() {
        
        // phan tên CSDL
        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout(FlowLayout.CENTER));
        p1.add(new JLabel("Data name "));
        nameData = new JTextField(10);
        p1.add(nameData);
        ok = new JButton("OK");
        ok.addActionListener(this);
        p1.add(ok);
        add(p1,BorderLayout.NORTH);
        
        // phan hien thi
        display = new JPanel();
        display.setBorder(BorderFactory.createTitledBorder(" R
esult :"));
        add(display,BorderLayout.CENTER);
        
        // phan dieu khien
        clear = new JButton("Clear");
        clear.addActionListener(this);
        query = new JButton("Query");
        query.addActionListener(this);
        select = new JTextField(30);
        control = new JPanel();
        control.setLayout(new FlowLayout(FlowLayout.CENTER));
        control.add(new JLabel("SQL"));
        control.add(select);
        control.add(query);
        control.add(clear);
        add(control,BorderLayout.SOUTH);
    }

    public static void main(String&#&#[&#&#] args) {
        new DemoSQL();
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==ok){
            if(Connect(nameData.getText())){
                control.setVisible(true);
                display.setVisible(true);
            }

        }
        else if(e.getSource()==clear){
            select.setText("");
            display.removeAll();
            display.setVisible(false);
            display.setVisible(true);
        }
        else if(e.getSource()==query){
            selectData(select.getText());
        }
        
    }

    private void selectData(String sql) {
        
        Statement stmt;
        ResultSet rs;
        try {
             stmt = con.createStatement();
             rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            display.removeAll();
            display.setVisible(false);
            display.setVisible(true);
            return;
        }    
        creatTable(rs);    
    }

    private void creatTable(ResultSet rs) {
        display.removeAll();
        Vector vtData=new Vector() ;
        Vector vtColumn = null ;
        
        ResultSetMetaData rsm = null;
        
        try {
            rsm = rs.getMetaData();
            int col = rsm.getColumnCount();
            vtColumn = new Vector(col,col);
            for (int i = 1; i <= col; i++){
                 vtColumn.add(rsm.getColumnName(i));
            }
                 
            vtData = new Vector(10,10);
            Vector temp;
            while(rs.next()){
                temp = new Vector(col);
                for(int i = 1; i<=col;i++){
                    temp.add(rs.getString(i));
                }
                vtData.add(temp);
            }
             
            JTable table = new JTable(vtData,vtColumn);
            display.add(new JScrollPane(table));
            
        } catch (SQLException e) {}
        
        display.setVisible(false);
        display.setVisible(true);
    }

    private boolean Connect(String name) {
        
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:"+nam
e);
        } catch (Exception e) {
            
            return false;
        }
            
        return true;
    }

}  

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