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

📄 Ȧ

📁 java入门的几个代码,很实用,初学者不可错过
💻
字号:
//源程序清单11-4
package database;
import java.awt.*;
import java.awt.event.*;
import com.borland.jbcl.layout.*;
import com.borland.jbcl.control.*;
import java.sql.*;
import java.util.*;
import com.borland.jbcl.model.*;
public class MainFrame extends DecoratedFrame
{
  Connection connection=null;
  PreparedStatement statement=null;
  int row=0;
  int column=0;
  int i;
  Vector date=new Vector();
  XYLayout xYLayout1 = new XYLayout();
 Button button1 = new Button();
  Button button2 = new Button();
  Button button3 = new Button();
  Button button4 = new Button();
  GridControl gridControl1 = new GridControl();
public MainFrame()
{
try {
jbInit();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection("jdbc:odbc:people","yzl","yzl");
SetGridControl();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.setTitle("数据库实例");
this.setLayout(xYLayout1);
gridControl1.setColumnCaptions(new String[]{"name","sex", "age", "phone", "other"});
button1.setLabel("增加");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
}) ;
button2.setLabel("修改");
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
  button2_actionPerformed(e);
}
});
 button3.setLabel("删除");
 button3.addActionListener(new java.awt.event.ActionListener() {
   public void actionPerformed(ActionEvent e) {
    button3_actionPerformed(e);
      }
    });
 button4.setLabel("退出");
 button4.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    button4_actionPerformed(e);
   }
    });
    xYLayout1.setHeight(328);
    xYLayout1.setWidth(471);
    this.add(gridControl1, new XYConstraints(2, 7, 459, 147));
    this.add(button1, new XYConstraints(46, 186, 93, 29));
    this.add(button3, new XYConstraints(46, 236, 95, 31));
    this.add(button2, new XYConstraints(263, 185, 97, 34));
  this.add(button4, new XYConstraints(266, 236, 98, 30));
}
public static void main(String[] args)
{
MainFrame frame=new MainFrame();
frame.setLocation(150,150);
frame.setSize(550,400);
frame.show();
}
void SetGridControl()
{
try
{
date.removeAllElements();
String sq1="select * from yzl1";
statement=connection.prepareStatement(sq1);
ResultSet result=statement.executeQuery();
while(result.next())
{
String name=result.getString("name");
String sex=result.getString("sex");
int age=result.getInt("age");
String phone=result.getString("phone");
String other=result.getString("other");
date.addElement(name);
date.addElement(sex);
date.addElement(Integer.toString(age));
date.addElement(phone);
date.addElement(other);
}
result.close();
statement.close();
int len=date.size()/5;
String[][] items=new String[len][5] ;
for(int i=0;i<len;i++)
{
items[i][0]=(String)date.elementAt(i*5);
items[i][1]=(String)date.elementAt(i*5+1);
items[i][2]=(String)date.elementAt(i*5+2);
items[i][3]=(String)date.elementAt(i*5+3);
items[i][4]=(String)date.elementAt(i*5+4);
}
gridControl1.setReadOnly(false);
gridControl1.setItems(items);
gridControl1.setReadOnly(true);
}catch(Exception e)
{
System.out.println(e.toString());
}
}
void button1_actionPerformed(ActionEvent e)
{
i+=1;
InputDialog idlg=new InputDialog(this,"添加新记录",true);
idlg.setLocation(200,200);
idlg.setSize(300,200);
idlg.show();

}
void button2_actionPerformed(ActionEvent e)
{
i=i-1;
InputDialog idlg=new InputDialog(this,"修改记录",true);
idlg.groupBox1.setLabel("请输入修改用户信息:");
idlg.setLocation(200,200);
idlg.setSize(300,200);
idlg.show();
}
void button3_actionPerformed(ActionEvent e)
{
try
{
MessageDialog mdlg=new MessageDialog(this,"删除确认","你确定要删除该记录吗?",6);
mdlg.show();
if(mdlg.getResult()==MessageDialog.NO)
return;
String name=(String)date.elementAt(row*5);
String sq1="delete from yzl1 where name='"+name+"'";
statement=connection.prepareStatement(sq1);
statement.executeUpdate();
statement.close();
SetGridControl();
}catch(Exception ee)
{
System.out.println(ee.toString());
}
}
void button4_actionPerformed(ActionEvent e)
{
System.exit(0);
}
void gridContro11_subfocusChaned(MatrixSubfocusEvent e)
{
try
{
MatrixLocation location=e.getLocation();
if(location==null)return;
row=location.row;
column=location.column;
}catch(Exception ee) {}
}
}

⌨️ 快捷键说明

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