java_csdl
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.Font;
public class Connect {
final static JFrame F=new JFrame();
public static void main(String[] args) {
AddFrame();
}
public static void AddFrame()
{
final JTextField txt1,txt2,txt3,txt4,txt5,txt6;
final JButton bt1,bt2,bt3,bt4,bt5,bt6;
final JPanel p;
final ClassConnect Conn=new ClassConnect();
System.out.println(Conn.KetnoiCSDL());
Conn.setSql("Select * From KhachHg");
Conn.Hienthilenbang();
F.setLocation(250,100);
F.setSize(500,500);
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTable Tb= Conn.Bang;
F.setLayout(null);
JScrollPane Thanhcuon=new JScrollPane(Tb);
Thanhcuon.setBounds(15,20,450,200);
F.add(Thanhcuon);
p=new JPanel();
txt1=new JTextField();
txt2=new JTextField();
txt3=new JTextField();
txt4=new JTextField();
txt5=new JTextField();
txt6=new JTextField();
bt1=new JButton("Insert");
bt2=new JButton("Delete");
bt3=new JButton("Update");
bt4=new JButton("Nhap L?i");
bt5=new JButton("Thoa");
bt6=new JButton("Save As");
bt1.setFont(new Font("Arial",Font.BOLD,9));
bt2.setFont(new Font("Arial",Font.BOLD,9));
bt3.setFont(new Font("Arial",Font.BOLD,9));
bt4.setFont(new Font("Arial",Font.BOLD,9));
bt5.setFont(new Font("Arial",Font.BOLD,9));
bt6.setFont(new Font("Arial",Font.BOLD,9));
p.setLayout(new GridLayout(2,6,2,8));
p.add(txt1);
p.add(txt2);
p.add(txt3);
p.add(txt4);
p.add(txt5);
p.add(txt6);
p.add(bt1);
p.add(bt2);
p.add(bt3);
p.add(bt4);
p.add(bt5);
p.add(bt6);
p.setBounds(10,250,450,65);
F.add(p);
Tb.setSelectionMode(1);//addListSelectionListener
Tb.getSelectionModel().addListSelectionListener( new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
int Row=Tb.getSelectedRow();
if(e.getValueIsAdjusting())
{
return;
}
txt1.setText(Tb.getValueAt(Row,0).toString());
txt2.setText(Tb.getValueAt(Row,1).toString());
txt3.setText(Tb.getValueAt(Row,2).toString());
txt4.setText(Tb.getValueAt(Row,3).toString());
txt5.setText(Tb.getValueAt(Row,4).toString());
txt6.setText(Tb.getValueAt(Row,5).toString());
}
});
bt5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
F.setVisible(false);
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
);
bt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Conn.setSql("Insert Into KhachHg values('"+txt1.getText().toString()+"','"+txt2.getText().toString()+"','"+txt3.getText().toString()+"','"+txt4.getText().toString()+"','"+txt5.getText().toString()+"',"+Integer.parseInt(txt6.getText().toString())+")");
Conn.Update();
F.validate();
}
});
bt2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Conn.setSql("Delete * From KhachHg where MaKH='"+txt1.getText().toString()+"'");
Conn.Update();
F.validate();
}
});
F.setVisible(true);
}
}
import java.util.*;
import java.sql.*;
import javax.swing.JTable;
public class ClassConnect {
String Url;
String Sql;
boolean isConn;
Connection Conn;
ResultSet KQ;
public JTable Bang;
public ClassConnect()
{ //"jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb); DBQ=NhanVien.mdb;READONLY=false;"
this.Url="jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb); DBQ=NhanVien.mdb;READONLY=false;";
this.Sql="";
this.isConn=false;
}
public ClassConnect(String Duongdan,String Caulenh)
{
this.Url=Duongdan;
this.Sql=Caulenh;
}
public String getUrl()
{
return this.Url;
}
public String getSql()
{
return this.Sql;
}
public void setUrl(String Duongdan)
{
this.Url=Duongdan;
}
public void setSql(String Caulenh)
{
this.Sql=Caulenh;
}
public Boolean KetnoiCSDL()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
}
catch(Exception e){
System.out.print("Loi ket noi"+e.toString());
this.isConn=false;
return false;
}
try{
this.Conn=DriverManager.getConnection(this.Url);
this.isConn=true;
return true;
}
catch(SQLException ex)
{
System.out.println("Loi Co So Du Lieu SQL"+ex.toString());
this.isConn=false;
return false;
}
}
public void LayCSDL()
{
try{
Statement Caulenh=this.Conn.createStatement();
this.KQ=Caulenh.executeQuery(this.Sql);
}
catch(Exception ex)
{
System.out.println("Loi SQL"+ex.toString());
}
}
public void Hienthilenbang()
{
try{
this.LayCSDL();
Vector Cot=new Vector();
Vector Row=new Vector();
ResultSetMetaData Cmp=this.KQ.getMetaData();
for(int i=1;i<=Cmp.getColumnCount();i++)
{
Cot.addElement(Cmp.getColumnName(i));
}
for(int i=0;i<Cmp.getColumnCount();i++)
{
System.out.println(Cot.get(i).toString());
}
boolean More=this.KQ.next();
while(More){
Vector DongHt=new Vector();
for(int i=1;i<=Cmp.getColumnCount();i++)
{
DongHt.addElement(this.KQ.getString(i));
}
Row.addElement(DongHt);
More=this.KQ.next();
}
this.Bang=new JTable(Row,Cot);
}
catch(Exception ex)
{
System.out.println("Loi Hien Thi"+ex.toString());
}
}
public void Update()
{
try{
Statement Sta=Conn.createStatement();
int cmp=Sta.executeUpdate(this.Sql);
}
catch(SQLException ex)
{
System.out.print("Loi");
}
}}
Bạn đang đọc truyện trên: AzTruyen.Top