📄 sqlclient.java
字号:
import java.rmi.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class SQLClient extends JPanel implements ActionListener
,Runnable
{
static JFrame myFrame;
JButton bOpenScript,bScript,bSelect,bExit,bNew;
JLabel lblScript,lblSelect;
JTextField tfScript;
JTextArea taSelect,taError;
JTable tab;
JScrollPane pTab;
String nameFile="";
SQLInterface sql;
String strAuthor="";
String author[]={"T","e","s","t"," S","Q","L","-","C","o","p","y",
"r","i","g","h","t"," b","y"," N","h","u"," D","i","n","h",
" T","h","u","a","n"};
int findNext=0,ver=500,hor=600,index=0;
boolean booleanSave=true,booleanOpen=false,booleanExit=false;
Thread t;
public SQLClient(String address)
{
setLayout(null);
setBackground(new Color(64,118,145));
lblScript=new JLabel("SQL script file:");
lblScript.setForeground(new Color(213,255,122));
lblScript.setBounds(250,20,150,20);add(lblScript);
tfScript=new JTextField(38);
tfScript.setBounds(260,40,470,30);
tfScript.setBackground(new Color(229,231,237));
tfScript.setForeground(new Color(24,0,110));
add(tfScript);
bOpenScript=new JButton("Open");
bOpenScript.setBounds(30,40,100,30);
bOpenScript.setForeground(new Color(145,0,135));
bOpenScript.setBackground(Color.white);
bOpenScript.addActionListener(this);add(bOpenScript);
bScript=new JButton("Exe Script");
bScript.setBounds(150,40,100,30);
bScript.setForeground(new Color(145,0,135));
bScript.setBackground(Color.white);
bScript.addActionListener(this);add(bScript);
lblSelect=new JLabel("Enter your SQL (Select or Insert) statement here:");
lblSelect.setForeground(new Color(213,255,122));
lblSelect.setBounds(90,90,300,20);add(lblSelect);
taSelect=new JTextArea();
taSelect.setFont(new Font("Times New Roman",Font.PLAIN,15));
taSelect.setSelectedTextColor(Color.green);
taSelect.setSelectionColor(Color.darkGray);
taSelect.setBackground(new Color(229,231,237));
taSelect.setForeground(new Color(24,0,110));
JScrollPane pSelect=new JScrollPane();
pSelect.setViewportView(taSelect);
pSelect.setBounds(250,120,450,130);add(pSelect);
bSelect=new JButton("Execute");
bSelect.setMnemonic('X');
bSelect.setBounds(140,140,100,30);
bSelect.setForeground(new Color(145,0,135));
bSelect.setBackground(Color.white);
bSelect.addActionListener(this);add(bSelect);
bNew=new JButton("New");
bNew.setMnemonic('N');
bNew.setBounds(20,140,100,30);
bNew.setForeground(new Color(145,0,135));
bNew.setBackground(Color.white);
bNew.addActionListener(this);add(bNew);
bExit=new JButton("Exit");
bExit.setMnemonic('E');
bExit.setBounds(80,200,100,30);
bExit.setForeground(new Color(145,0,135));
bExit.setBackground(Color.white);
bExit.addActionListener(this);add(bExit);
pTab=JTable.createScrollPaneForTable(tab);
pTab.setBackground(new Color(106,116,104));
pTab.setBounds(10,280,700,200);add(pTab);
taError=new JTextArea();
taError.setForeground(Color.red);
taError.setEditable(false);
taError.setLineWrap(true);
t=new Thread(this);
t.start();
try
{
//InetAddress serverAddress=InetAdress
sql=(SQLInterface)Naming.lookup("//"+address+"/SQLInterface");
}
catch(Exception e){System.out.println("ket noi server"+e);}
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==bOpenScript){openScript();}
if(evt.getSource()==bScript){readFileScript();}
if(evt.getSource()==bNew){taSelect.setText("");}
if(evt.getSource()==bSelect){exe(taSelect.getText());}
if(evt.getSource()==bExit){System.exit(0);}
}
public void openScript()
{
JFileChooser jfc;
if(nameFile.equals("")){jfc=new JFileChooser("My Documents");}
else {jfc=new JFileChooser(nameFile);}
jfc.showOpenDialog(myFrame);
File fn = jfc.getSelectedFile();
try
{nameFile= fn.getPath();}
catch(Exception e){System.out.println("Khong co ten file"+e);
nameFile="";}
tfScript.setText(nameFile);
}
public void readFileScript()
{
String strReadLine="",strRead="";
try
{
if(nameFile!=null)
{
RandomAccessFile f=new RandomAccessFile(nameFile,"r");
strReadLine= f.readLine();
while(strReadLine!=null)
{
strRead+=strReadLine+"\n";
strReadLine= f.readLine();
}
f.close();
}
}
catch(Exception e){System.out.println("loi tai doc file"+e);}
try
{sql.exeScript(strRead);}
catch(Exception e){System.out.println("loi tai day"+e);}
}
public String blockString(String strBlock)
{
strBlock=strBlock.trim();
if(strBlock.startsWith("/*"))
{
if(strBlock.indexOf("*/")>-1)
{
strBlock=strBlock.substring(strBlock.indexOf("*/")+2);
strBlock=strBlock.trim();
}
else
{
strBlock=strBlock+"*/"+"\n"+"/*non script*/";
strBlock=strBlock.trim();
}
}
if(strBlock.startsWith("--")){ strBlock=lineString(strBlock);}
if(strBlock.startsWith("/*")){strBlock=blockString(strBlock);}
return strBlock;
}
public String lineString(String strLine)
{
strLine=strLine.trim();
if(strLine.startsWith("--"))
{
if(strLine.indexOf('\n')>-1)
{
strLine=strLine.substring(strLine.indexOf('\n'));
strLine=strLine.trim();
}
else{strLine=strLine+"\n"+"/*non script*/";}
}
if(strLine.startsWith("--"))
{
strLine=lineString(strLine);
}
if(strLine.startsWith("/*"))
{
strLine=blockString(strLine);
}
return strLine;
}
public void exe(String statement)
{
statement=blockString(statement);
statement=statement.trim();
if((statement.toLowerCase()).startsWith("select"))
{
try
{
sql.exeSelect(statement);
if(sql.getError().equals(""))
{setData(sql.getRowData(),sql.getNameColumn());}
else {setError(sql.getError());}
}
catch(Exception e){System.out.println("Select wrong"+e);}
}
else
{
try
{
sql.exeUpdate(statement);
if(sql.getError().equals(""))
{setError(sql.getResultUpdate());}
else {setError(sql.getError());}
}
catch(Exception e){System.out.println("Update wrong"+e);}
}
}
public void setData(String rowData[][],String columnName[])
{
tab=new JTable(rowData,columnName);
tab.setForeground(new Color(0,36,92));
tab.setBackground(new Color(227,255,243));
tab.setSelectionBackground(Color.darkGray);
tab.setSelectionForeground(Color.green);
pTab.setViewportView(tab);
}
public void setError(String error)
{
taError.setText(error);
taError.setBackground(new Color(181,130,181));
taError.setForeground(new Color(241,255,0));
pTab.setViewportView(taError);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.green);
g.fill3DRect(8,278,704,204,true);
g.setColor(new Color(106,197,255));
g.setFont(new Font("Dialog",Font.PLAIN,20));
g.drawString(author[index],hor,ver+10);
g.drawString(strAuthor,150,510);
}
public void run()
{
boolean len=true;
int dodai=strAuthor.length()+160;
try
{
while(true)
{
if(len)
{
if(index<30)
{
if(hor>dodai){hor--; }
if(hor==dodai)
{
strAuthor+=author[index];
dodai+=12;hor=600;index++;
}
}
if(index==30)
{
if(hor>dodai){ hor--;}
else {len=false;}
}
}
if(len==false)
{
if(index>0)
{
if(hor<600){hor++; }
if(hor>=600)
{
int endIndex=strAuthor.length()-author[index].length();
strAuthor=strAuthor.substring(0,endIndex);
dodai-=12;hor=dodai;index--;
}
}
if(index==0)
{
if(hor<600){ hor++;}
else
{
len=true;}
}
}
repaint();
t.sleep(10);
}
}
catch(Exception e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -