⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yoursqlgui.java

📁 原创的DBMS 数据库! 功能很简单
💻 JAVA
字号:
package code;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.Color;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.SystemColor;
import javax.swing.Action;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.lang.Object;
import java.lang.String;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionEvent;
import javax.swing.JScrollBar;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;

public class yourSQLGUI extends JFrame {
	public Connection con=null;
	public Statement stmt=null;
	public String tablelist="";
	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JTextField jTitle = null;

	private JTextField jTextField1 = null;

	private JLabel jLabel = null;

	private JTextArea jTable = null;

	private JRadioButton jRadioButton1 = null;

	private JRadioButton jRadioButton2 = null;

	private JRadioButton jRadioButton3 = null;

	private JRadioButton jRadioButton4 = null;

	private JRadioButton jRadioButton5 = null;

	private JTextField jQuery = null;

	private JButton jButton = null;

	private JTextArea jResult = null;
	private JScrollPane jScrollPane = null;
	private JScrollPane jScrollPane1 = null;
	private JLabel jLabel1 = null;
	/**
	 * This is the default constructor
	 */
	public yourSQLGUI() {
		super();
		initialize();
		try
	      {
	         Class.forName("code.yourSQLDriver");
	      } catch (ClassNotFoundException e) {
	         System.err.println(
	              "JDBC Driver could not be registered!!\n");
	      }
	    String  fName = ".";
	     
	       try {
			con = DriverManager.getConnection("jdbc:yourSQL:.", "", "");
			stmt = con.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String   dir="E:/";  
		  File   f=new   File(dir);   
		  String[]   file=f.list();   
		  for(int   i=0;i<file.length;i++)   
		  {   
		  if(file[i].endsWith("dbf"))//此处可换成你想要的扩展名   
		  {   
			  tablelist+=file[i];//此处可写成你想要的操作   
			  tablelist+=" \n";
		  }   
		  }  
		  jTable.setText(tablelist);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(646, 412);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(171, 182, 100, 27));
			jLabel1.setText("Result:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(14, 96, 140, 36));
			jLabel.setBackground(Color.lightGray);
			jLabel.setFont(new Font("Dialog", Font.BOLD, 13));
			jLabel.setText("  Tables in the DBMS");
			jContentPane = new JPanel();
			ButtonGroup bp= new ButtonGroup();
			jContentPane.setLayout(null);
			jContentPane.add(getJTitle(), null);
			jContentPane.add(getJTextField1(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJRadioButton1(), null);
			jContentPane.add(getJRadioButton2(), null);
			jContentPane.add(getJRadioButton3(), null);
			jContentPane.add(getJRadioButton4(), null);
			jContentPane.add(getJRadioButton5(), null);
			bp.add(jRadioButton1);
			bp.add(jRadioButton2);
			bp.add(jRadioButton3);
			bp.add(jRadioButton4);
			bp.add(jRadioButton5);
			jContentPane.add(getJQuery(), null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getJScrollPane1(), null);
			jContentPane.add(jLabel1, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTitle	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTitle() {
		if (jTitle == null) {
			jTitle = new JTextField();
			jTitle.setBounds(new Rectangle(170, 10, 226, 84));
			jTitle.setFont(new Font("Dialog", Font.PLAIN, 22));
			jTitle.setBackground(Color.lightGray);
			jTitle.setForeground(Color.magenta);
			jTitle.setEditable(false);
			jTitle.setText(" yourSQL  数据库系统");
		}
		return jTitle;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(400, 69, 210, 22));
			jTextField1.setFont(new Font("Dialog", Font.ITALIC, 14));
			jTextField1.setForeground(Color.yellow);
			jTextField1.setEditable(false);
			jTextField1.setText("Copyright 2006-2008 423 room");
		}
		return jTextField1;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTable() {
		if (jTable == null) {
			jTable = new JTextArea();
			jTable.setEditable(false);
		}
		return jTable;
	}

	/**
	 * This method initializes jRadioButton1	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton1() {
		if (jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setBounds(new Rectangle(166, 104, 67, 21));
			jRadioButton1.setText("create");
		}
		return jRadioButton1;
	}

	/**
	 * This method initializes jRadioButton2	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton2() {
		if (jRadioButton2 == null) {
			jRadioButton2 = new JRadioButton();
			jRadioButton2.setBounds(new Rectangle(241, 105, 68, 21));
			jRadioButton2.setText("insert");
		}
		return jRadioButton2;
	}

	/**
	 * This method initializes jRadioButton3	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton3() {
		if (jRadioButton3 == null) {
			jRadioButton3 = new JRadioButton();
			jRadioButton3.setBounds(new Rectangle(316, 105, 68, 21));
			jRadioButton3.setText("select");
		}
		return jRadioButton3;
	}

	/**
	 * This method initializes jRadioButton4	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton4() {
		if (jRadioButton4 == null) {
			jRadioButton4 = new JRadioButton();
			jRadioButton4.setBounds(new Rectangle(391, 105, 74, 21));
			jRadioButton4.setText("update");
		}
		return jRadioButton4;
	}

	/**
	 * This method initializes jRadioButton5	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton5() {
		if (jRadioButton5 == null) {
			jRadioButton5 = new JRadioButton();
			jRadioButton5.setBounds(new Rectangle(466, 105, 81, 21));
			jRadioButton5.setText("delete");
		}
		return jRadioButton5;
	}

	/**
	 * This method initializes jQuery	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJQuery() {
		if (jQuery == null) {
			jQuery = new JTextField();
			jQuery.setBounds(new Rectangle(170, 136, 355, 41));
		}
		return jQuery;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setAction(new Action(){

	public void addPropertyChangeListener(PropertyChangeListener arg0) {
	}

	public Object getValue(String arg0) {
		// TODO Auto-generated method stub
		return null;
		
	}

	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
		
	}

	public void putValue(String arg0, Object arg1) {
	}

	public void removePropertyChangeListener(PropertyChangeListener arg0) {
	}

	public void setEnabled(boolean arg0) {
	}

	public void actionPerformed(ActionEvent arg0) {
		try {
			stmt = con.createStatement();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(jRadioButton1.isSelected()||jRadioButton2.isSelected()||jRadioButton4.isSelected()||jRadioButton5.isSelected())
		{
			try {
				stmt.executeUpdate(jQuery.getText());
				tablelist="";
				String   dir="E:/";  
				  File   f=new   File(dir);   
				  String[]   file=f.list();   
				  for(int   i=0;i<file.length;i++)   
				  {   
				  if(file[i].endsWith("dbf"))//此处可换成你想要的扩展名   
				  {   
					  tablelist+=file[i];//此处可写成你想要的操作   
					  tablelist+=" \n";
				  }   
				  }  
				  jTable.setText(tablelist);
				  stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else
		{
			boolean xing=false;
			String sql=jQuery.getText();
			int snum=0,fnum=0;
			int fromflag=sql.indexOf("from");
			String [] colname=new String[10];
			String strtemp="";
			int colnamenum=0;
			snum=7;
			fnum=sql.indexOf(" ",snum);
			strtemp=sql.substring(snum,fnum);
			if(!strtemp.equals("*"))
			{
				xing=false;
				while(true)
				{
				colname[colnamenum]=strtemp;
				colnamenum++;
				snum=fnum+1;
				fnum=sql.indexOf(" ",snum);
				strtemp=sql.substring(snum,fnum);
				if(fnum>=fromflag)
					break;
				}
//				for(int p=0;p<colnamenum;p++)
//					System.out.println(" "+colname[p]);
			}
			else
				xing=true;
//			for(int p=0;p<colnamenum;p++)
//				System.out.println(" "+colname[p]);
			snum=fromflag+5;
			fnum=sql.indexOf(" ",snum);
			String tablename=sql.substring(snum,fnum);//获取表的名称
			File file=new File("E:/"+tablename+".dbf");
			RandomAccessFile randomAccessFile;
			String []name=new String[10];
			String []type=new String[10];
			int []indextemp=new int[10];
			int ptemp=0;
			for(int yy=0;yy<10;yy++)
				indextemp[yy]=0;
				try {
					randomAccessFile = new RandomAccessFile(file, "r");
					int headlongth=randomAccessFile.readInt();
					int headnum=randomAccessFile.readInt();
					int filelongth=randomAccessFile.readInt();
					int filenum=randomAccessFile.readInt();
					for(int k=0;k<headnum;k++)
					{
						 char[] namece = new char[100]; 
						 char[] typece=new char[100];
					        for(int i = 0; i < namece.length; i++) 
					             namece[i] = randomAccessFile.readChar(); 
					        for(int i = 0; i < typece.length; i++) 
					        	typece[i] = randomAccessFile.readChar(); 
					        
					        name[k]=new String(namece).replace('\0', ' ');
					        type[k]=new String(typece).replace('\0', ' ');
					        
					        if(type[k].startsWith("int"))
					        	ptemp+=4;
					        if(type[k].startsWith("string"))
					        	ptemp+=200;
					        if(type[k].startsWith("double"))
					        	ptemp+=8;
					        
					        indextemp[k+1]=ptemp;
					 }
				
				ResultSet rs=stmt.executeQuery(jQuery.getText());
				String out="";
				if(!xing)
				{
					System.out.println("Column name num :"+colnamenum);
					for(int i=0;i<colnamenum;i++)
					{
						out+=colname[i];
						out+="        ";
					}
					out+="\n";
				}else
				{
					for(int i=0;i<headnum;i++)
					{
						String sstemp=name[i];
						out+=sstemp.substring(0,sstemp.indexOf(" "));
						out+="       ";
					}
					out+="\n";
				}
				while(rs.next())
				{
					if(!xing)
					{
						for(int i=0;i<colnamenum;i++)
						{
							for(int j=0;j<headnum;j++)
							{
								if(name[j].startsWith(colname[i]))
								{
									if(type[j].startsWith("int"))
										out+=rs.getInt(colname[i]);
									if(type[j].startsWith("double"))
										out+=rs.getDouble(colname[i]);
									if(type[j].startsWith("string"))
									{
										String sstemp=rs.getString(colname[i]);
										out+=sstemp.substring(0,sstemp.indexOf(" "));
									}
									out+="     ";
								}
							}
						}
						out+="\n";
					}else
					{
						
						for(int i=0;i<headnum;i++)
						{
							if(type[i].startsWith("int"))
								out+=rs.getInt(name[i]);
							if(type[i].startsWith("double"))
								out+=rs.getDouble(name[i]);
							if(type[i].startsWith("string"))
							{
								String sstemp=rs.getString(name[i]);
								out+=sstemp.substring(0,sstemp.indexOf(" "));
							}
							out+="     ";
						}
						out+="\n";
					}
				}
				jResult.setText(out);
				randomAccessFile.close();
				rs.close();
				stmt.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}});
			jButton.setBounds(new Rectangle(548, 137, 73, 31));
			jButton.setText("Excute");
		}
		return jButton;
	}

	/**
	 * This method initializes jResult	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJResult() {
		if (jResult == null) {
			jResult = new JTextArea();
			jResult.setEditable(false);
			jResult.setWrapStyleWord(true);
			jResult.setLineWrap(true);
			jResult.setBorder(null);
			jResult.setBackground(Color.white);
		}
		return jResult;
	}
	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(168, 211, 445, 161));
			jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			jScrollPane.setViewportView(getJResult());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(new Rectangle(14, 139, 140, 232));
			jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			jScrollPane1.setViewportView(getJTable());
		}
		return jScrollPane1;
	}

	public static void main(String [] args)
	{
		yourSQLGUI yGUI=new yourSQLGUI();
		yGUI.setVisible(true);
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -