class FileOp
{
RandomAccessFile info=null; // 文件类
FileOp() //判断记录文件是否存在
{
boolean isNewFile=false;//是否是一个新的文件
File infoFile=new File("info.txt");
if(infoFile.exists())
{
DBConString="数据库文件连接成功!";
}
else //如果不存在建立一个新文件
{
isNewFile=true;
DBConString="数据库文件连接失败!正在创建数据库...";
try
{
infoFile.createNewFile();
DBCreatOKString="创建成功!";
}
catch(Exception e){}
}
try //打开数据库
{
info=new RandomAccessFile("info.txt","rw");
if(isNewFile)
{
info.seek(0); //写入总记录的数量为0
info.writeInt(0);
}
}
catch(Exception e){}
}
void CloseRecFile() //关闭记录文件
{
try
{
info.close();
}
catch(Exception e){}
}
public void ReadRec() //读取记录
{
try //读取总记录个数
{
info.seek(0);
totalRecNum=info.readInt();
info.seek(4);
int count=0;
char t='1';
while(count!=8*recNum-8)
{
t=info.readChar();
if(t==' ') //以空格为隔断
{
count++;
}
}
}
catch(Exception e){}
for(int i=0;i<8;i++)
{
String tempStr="";
char t='1';
try
{
while(t!=' ') //以空格为隔断
{
t=info.readChar();
tempStr=tempStr+t;
}
}
catch(Exception e){}
switch(i)
{
case 0: name=tempStr.trim();break;
case 1: sex=tempStr.trim();break;
case 2: age=tempStr.trim();break;
case 3: jiGuan=tempStr.trim();break;
case 4: tel=tempStr.trim();break;
case 5: qqNum=tempStr.trim();break;
case 6: email=tempStr.trim();break;
case 7: aiHao=tempStr.trim();break;
}
}
}
public void WriteRec() //写入记录
{
try //写入总记录个数
{
info.seek(0);
info.writeInt(totalRecNum);
int count=0;
char t='1';
while(count!=8*recNum-8)
{
t=info.readChar();
if(t==' ') //以空格为隔断
{
count++;
}
}
}
catch(Exception e){}
for(int i=0;i<8;i++)
{
String tempStr=new String("");
switch(i)
{
case 0: tempStr=name;break;
case 1: tempStr=sex;break;
case 2: tempStr=age;break;
case 3: tempStr=jiGuan;break;
case 4: tempStr=tel;break;
case 5: tempStr=qqNum;break;
case 6: tempStr=email;break;
case 7: tempStr=aiHao;break;
}
try
{
info.writeChars(tempStr+' '); //以空格为隔断
}
catch(Exception e){}
}
}
public void WriteTotalRecNum() //写入记录总数
{
try
{
info.seek(0);
info.writeInt(totalRecNum);
}
catch(Exception e){}
}
}
}
这个是文件操作类的源码,是自动生成文本数据库的,我想改用SQL做数据库,怎么改,如何连接数据库,如何写对表的操作。
13 个解决方案
#1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.JOptionPane;
class main
{
public static void main(String args[])
{
new MainForm("同学录v1.0");
}
}
class MainForm extends JFrame implements ActionListener //主窗体
{
JPanel p1,p2,p11,p12,p13;
JLabel WelcomeLabel,DBLabel,DBCreatOKLabel;
String DBConString,DBCreatOKString; //数据库状态字符串
JButton AppBtn,DelBtn,EditBtn,CheckBtn,QuitBtn;
JMenuBar menubar;
JMenu menu;
JMenuItem item1,item2,item3,item4,item5;
int totalRecNum=0,recNum=0;//记录总数,当前记录号
// 字段 姓名 性别 年龄 籍贯 电话 QQ号 EMAIL 爱好
String name,sex,age,jiGuan,tel,qqNum,email,aiHao;
FileOp FileOp;//文件操作类声明
MainForm(String s)
{
super(s);
menubar=new JMenuBar(); // -----菜单设计
menu=new JMenu("操作");
item1=new JMenuItem("查看记录");
item2=new JMenuItem("增加记录");
item3=new JMenuItem("删除记录");
item4=new JMenuItem("修改记录");
item5=new JMenuItem("退出程序");
menu.add(item1);
menu.add(item2);
menu.add(item3);
menu.add(item4);
menu.add(item5);
menubar.add(menu);
setJMenuBar(menubar);
WelcomeLabel=new JLabel("欢迎使用同学录V1.0!");
DBLabel=new JLabel("");
DBCreatOKLabel=new JLabel("");
AppBtn=new JButton("增加记录");//按钮设计
DelBtn=new JButton("删除记录");
EditBtn=new JButton("修改记录");
CheckBtn=new JButton("查看记录");
QuitBtn=new JButton("退出程序");
p1=new JPanel();
p2=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p11.add(WelcomeLabel);
p12.add(DBLabel);
p13.add(DBCreatOKLabel);
p1.add(p11);
p1.add(p12);
p1.add(p13);
p2.add(CheckBtn);
p2.add(AppBtn);
p2.add(DelBtn);
p2.add(EditBtn);
p2.add(QuitBtn);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
p11.setBackground(Color.white);
p12.setBackground(Color.white);
p13.setBackground(Color.white);
p1.setLayout(new GridLayout(3,1));
CheckBtn.addActionListener(this);
AppBtn.addActionListener(this);
DelBtn.addActionListener(this);
EditBtn.addActionListener(this);
QuitBtn.addActionListener(this);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
addWindowListener(new winClose());
setSize(700,500);
setResizable(false);
setLocation(50,50);
setVisible(true);
FileOp=new FileOp();
recNum=1;
FileOp.ReadRec();
WelcomeLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,24));
DBLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,20));
DBCreatOKLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,20));
DBLabel.setText(DBConString);
DBCreatOKLabel.setText(DBCreatOKString);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==CheckBtn|e.getSource()==item1)
new CheckForm("查找记录");
if(e.getSource()==AppBtn|e.getSource()==item2)
new AppForm("增加记录");
if(e.getSource()==DelBtn|e.getSource()==item3)
new DelForm("删除记录");
if(e.getSource()==EditBtn|e.getSource()==item4)
new EditForm("修改记录");
if(e.getSource()==QuitBtn|e.getSource()==item5)
{
FileOp.CloseRecFile();
System.exit(0);
}
}
主框架。
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.JOptionPane;
class main
{
public static void main(String args[])
{
new MainForm("同学录v1.0");
}
}
class MainForm extends JFrame implements ActionListener //主窗体
{
JPanel p1,p2,p11,p12,p13;
JLabel WelcomeLabel,DBLabel,DBCreatOKLabel;
String DBConString,DBCreatOKString; //数据库状态字符串
JButton AppBtn,DelBtn,EditBtn,CheckBtn,QuitBtn;
JMenuBar menubar;
JMenu menu;
JMenuItem item1,item2,item3,item4,item5;
int totalRecNum=0,recNum=0;//记录总数,当前记录号
// 字段 姓名 性别 年龄 籍贯 电话 QQ号 EMAIL 爱好
String name,sex,age,jiGuan,tel,qqNum,email,aiHao;
FileOp FileOp;//文件操作类声明
MainForm(String s)
{
super(s);
menubar=new JMenuBar(); // -----菜单设计
menu=new JMenu("操作");
item1=new JMenuItem("查看记录");
item2=new JMenuItem("增加记录");
item3=new JMenuItem("删除记录");
item4=new JMenuItem("修改记录");
item5=new JMenuItem("退出程序");
menu.add(item1);
menu.add(item2);
menu.add(item3);
menu.add(item4);
menu.add(item5);
menubar.add(menu);
setJMenuBar(menubar);
WelcomeLabel=new JLabel("欢迎使用同学录V1.0!");
DBLabel=new JLabel("");
DBCreatOKLabel=new JLabel("");
AppBtn=new JButton("增加记录");//按钮设计
DelBtn=new JButton("删除记录");
EditBtn=new JButton("修改记录");
CheckBtn=new JButton("查看记录");
QuitBtn=new JButton("退出程序");
p1=new JPanel();
p2=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p11.add(WelcomeLabel);
p12.add(DBLabel);
p13.add(DBCreatOKLabel);
p1.add(p11);
p1.add(p12);
p1.add(p13);
p2.add(CheckBtn);
p2.add(AppBtn);
p2.add(DelBtn);
p2.add(EditBtn);
p2.add(QuitBtn);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
p11.setBackground(Color.white);
p12.setBackground(Color.white);
p13.setBackground(Color.white);
p1.setLayout(new GridLayout(3,1));
CheckBtn.addActionListener(this);
AppBtn.addActionListener(this);
DelBtn.addActionListener(this);
EditBtn.addActionListener(this);
QuitBtn.addActionListener(this);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
addWindowListener(new winClose());
setSize(700,500);
setResizable(false);
setLocation(50,50);
setVisible(true);
FileOp=new FileOp();
recNum=1;
FileOp.ReadRec();
WelcomeLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,24));
DBLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,20));
DBCreatOKLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,20));
DBLabel.setText(DBConString);
DBCreatOKLabel.setText(DBCreatOKString);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==CheckBtn|e.getSource()==item1)
new CheckForm("查找记录");
if(e.getSource()==AppBtn|e.getSource()==item2)
new AppForm("增加记录");
if(e.getSource()==DelBtn|e.getSource()==item3)
new DelForm("删除记录");
if(e.getSource()==EditBtn|e.getSource()==item4)
new EditForm("修改记录");
if(e.getSource()==QuitBtn|e.getSource()==item5)
{
FileOp.CloseRecFile();
System.exit(0);
}
}
主框架。
#2
//---------------------------------------查看窗口类
class CheckForm extends JFrame implements ActionListener
{
JPanel p1,p2,p11,p12,p13,p14,p15,p16,p17,p18,p3;
JLabel nameLabel,sexLabel,ageLabel,jiGuanLabel;
JLabel telLabel,qqNumLabel,emailLabel,aiHaoLabel;
JLabel inputRecNumLabel;
JTextField nameText,sexText,ageText,jiGuanText;
JTextField telText,qqNumText,emailText,aiHaoText;
JTextField inputRecNumText;
JButton OKBtn,StartBtn,UpBtn,DownBtn,EndBtn,CancelBtn;
CheckForm (String s)
{
super(s);
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p14=new JPanel();
p15=new JPanel();
p16=new JPanel();
p17=new JPanel();
p18=new JPanel();
nameLabel=new JLabel("姓 名:");
sexLabel=new JLabel("性 别:");
ageLabel=new JLabel("年 龄:");
jiGuanLabel=new JLabel("籍 贯:");
telLabel=new JLabel("电 话:");
qqNumLabel=new JLabel("qq 号:");
emailLabel=new JLabel("email:");
aiHaoLabel=new JLabel("爱 好:");
inputRecNumLabel=new JLabel("输入要查看的记录号:");
nameText=new JTextField("",10);
sexText=new JTextField("",10);
ageText=new JTextField("",10);
jiGuanText=new JTextField("",10);
telText=new JTextField("",10);
qqNumText=new JTextField("",10);
emailText=new JTextField("",10);
aiHaoText=new JTextField("",10);
inputRecNumText=new JTextField("",5);
nameText.enable(false);
sexText.enable(false);
ageText.enable(false);
jiGuanText.enable(false);
telText.enable(false);
qqNumText.enable(false);
emailText.enable(false);
aiHaoText.enable(false);
OKBtn=new JButton("查找");
StartBtn=new JButton("首记录");
UpBtn=new JButton("上一条");
DownBtn=new JButton("下一条");
EndBtn=new JButton("尾记录");
CancelBtn=new JButton("返回");
p11.add(nameLabel);
p11.add(nameText);
p12.add(sexLabel);
p12.add(sexText);
p13.add(ageLabel);
p13.add(ageText);
p14.add(jiGuanLabel);
p14.add(jiGuanText);
p15.add(telLabel);
p15.add(telText);
p16.add(qqNumLabel);
p16.add(qqNumText);
p17.add(emailLabel);
p17.add(emailText);
p18.add(aiHaoLabel);
p18.add(aiHaoText);
p1.setLayout(new GridLayout(8,1));
p1.add(p11);
p1.add(p12);
p1.add(p13);
p1.add(p14);
p1.add(p15);
p1.add(p16);
p1.add(p17);
p1.add(p18);
p2.add(StartBtn);
p2.add(UpBtn);
p2.add(DownBtn);
p2.add(EndBtn);
p2.add(CancelBtn);
p3.add(inputRecNumLabel);
p3.add(inputRecNumText);
p3.add(OKBtn);
getContentPane().add(p3,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
OKBtn.addActionListener(this);
StartBtn.addActionListener(this);
UpBtn.addActionListener(this);
DownBtn.addActionListener(this);
EndBtn.addActionListener(this);
CancelBtn.addActionListener(this);
setSize(400,350);
setLocation(200,130);
setVisible(true);
recNum=1;
inputRecNumText.setText(String.valueOf(recNum));
ShowRecInfo();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==OKBtn)
if(Integer.parseInt(inputRecNumText.getText())>=1)
if(Integer.parseInt(inputRecNumText.getText())<=totalRecNum)
recNum=Integer.parseInt(inputRecNumText.getText());
if(e.getSource()==StartBtn)
recNum=1;
if(e.getSource()==UpBtn)
if(recNum>1)
recNum--;
if(e.getSource()==DownBtn)
if(recNum<totalRecNum)
recNum++;
if(e.getSource()==EndBtn)
recNum=totalRecNum;
if(e.getSource()==CancelBtn)
this.dispose();
ShowRecInfo();
}
void ShowRecInfo() //显示记录操作函数
{
FileOp.ReadRec();
inputRecNumText.setText(String.valueOf(recNum));
nameText.setText(name);
sexText.setText(sex);
ageText.setText(age);
jiGuanText.setText(jiGuan);
telText.setText(tel);
qqNumText.setText(qqNum);
emailText.setText(email);
aiHaoText.setText(aiHao);
}
}
查看窗口类
class CheckForm extends JFrame implements ActionListener
{
JPanel p1,p2,p11,p12,p13,p14,p15,p16,p17,p18,p3;
JLabel nameLabel,sexLabel,ageLabel,jiGuanLabel;
JLabel telLabel,qqNumLabel,emailLabel,aiHaoLabel;
JLabel inputRecNumLabel;
JTextField nameText,sexText,ageText,jiGuanText;
JTextField telText,qqNumText,emailText,aiHaoText;
JTextField inputRecNumText;
JButton OKBtn,StartBtn,UpBtn,DownBtn,EndBtn,CancelBtn;
CheckForm (String s)
{
super(s);
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p14=new JPanel();
p15=new JPanel();
p16=new JPanel();
p17=new JPanel();
p18=new JPanel();
nameLabel=new JLabel("姓 名:");
sexLabel=new JLabel("性 别:");
ageLabel=new JLabel("年 龄:");
jiGuanLabel=new JLabel("籍 贯:");
telLabel=new JLabel("电 话:");
qqNumLabel=new JLabel("qq 号:");
emailLabel=new JLabel("email:");
aiHaoLabel=new JLabel("爱 好:");
inputRecNumLabel=new JLabel("输入要查看的记录号:");
nameText=new JTextField("",10);
sexText=new JTextField("",10);
ageText=new JTextField("",10);
jiGuanText=new JTextField("",10);
telText=new JTextField("",10);
qqNumText=new JTextField("",10);
emailText=new JTextField("",10);
aiHaoText=new JTextField("",10);
inputRecNumText=new JTextField("",5);
nameText.enable(false);
sexText.enable(false);
ageText.enable(false);
jiGuanText.enable(false);
telText.enable(false);
qqNumText.enable(false);
emailText.enable(false);
aiHaoText.enable(false);
OKBtn=new JButton("查找");
StartBtn=new JButton("首记录");
UpBtn=new JButton("上一条");
DownBtn=new JButton("下一条");
EndBtn=new JButton("尾记录");
CancelBtn=new JButton("返回");
p11.add(nameLabel);
p11.add(nameText);
p12.add(sexLabel);
p12.add(sexText);
p13.add(ageLabel);
p13.add(ageText);
p14.add(jiGuanLabel);
p14.add(jiGuanText);
p15.add(telLabel);
p15.add(telText);
p16.add(qqNumLabel);
p16.add(qqNumText);
p17.add(emailLabel);
p17.add(emailText);
p18.add(aiHaoLabel);
p18.add(aiHaoText);
p1.setLayout(new GridLayout(8,1));
p1.add(p11);
p1.add(p12);
p1.add(p13);
p1.add(p14);
p1.add(p15);
p1.add(p16);
p1.add(p17);
p1.add(p18);
p2.add(StartBtn);
p2.add(UpBtn);
p2.add(DownBtn);
p2.add(EndBtn);
p2.add(CancelBtn);
p3.add(inputRecNumLabel);
p3.add(inputRecNumText);
p3.add(OKBtn);
getContentPane().add(p3,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
OKBtn.addActionListener(this);
StartBtn.addActionListener(this);
UpBtn.addActionListener(this);
DownBtn.addActionListener(this);
EndBtn.addActionListener(this);
CancelBtn.addActionListener(this);
setSize(400,350);
setLocation(200,130);
setVisible(true);
recNum=1;
inputRecNumText.setText(String.valueOf(recNum));
ShowRecInfo();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==OKBtn)
if(Integer.parseInt(inputRecNumText.getText())>=1)
if(Integer.parseInt(inputRecNumText.getText())<=totalRecNum)
recNum=Integer.parseInt(inputRecNumText.getText());
if(e.getSource()==StartBtn)
recNum=1;
if(e.getSource()==UpBtn)
if(recNum>1)
recNum--;
if(e.getSource()==DownBtn)
if(recNum<totalRecNum)
recNum++;
if(e.getSource()==EndBtn)
recNum=totalRecNum;
if(e.getSource()==CancelBtn)
this.dispose();
ShowRecInfo();
}
void ShowRecInfo() //显示记录操作函数
{
FileOp.ReadRec();
inputRecNumText.setText(String.valueOf(recNum));
nameText.setText(name);
sexText.setText(sex);
ageText.setText(age);
jiGuanText.setText(jiGuan);
telText.setText(tel);
qqNumText.setText(qqNum);
emailText.setText(email);
aiHaoText.setText(aiHao);
}
}
查看窗口类
#3
//---------------------------------------增加窗口类
class AppForm extends JFrame implements ActionListener
{
JPanel p1,p2,p11,p12,p13,p14,p15,p16,p17,p18;
JLabel nameLabel,sexLabel,ageLabel,jiGuanLabel;
JLabel telLabel,qqNumLabel,emailLabel,aiHaoLabel;
JTextField nameText,sexText,ageText,jiGuanText;
JTextField telText,qqNumText,emailText,aiHaoText;
JButton OKBtn,ClearBtn,CancelBtn;
AppForm(String s)
{
super(s);
p1=new JPanel();
p2=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p14=new JPanel();
p15=new JPanel();
p16=new JPanel();
p17=new JPanel();
p18=new JPanel();
nameLabel=new JLabel("姓 名:");
sexLabel=new JLabel("性 别:");
ageLabel=new JLabel("年 龄:");
jiGuanLabel=new JLabel("籍 贯:");
telLabel=new JLabel("电 话:");
qqNumLabel=new JLabel("qq 号:");
emailLabel=new JLabel("email:");
aiHaoLabel=new JLabel("爱 好:");
nameText=new JTextField("",10);
sexText=new JTextField("",10);
ageText=new JTextField("",10);
jiGuanText=new JTextField("",10);
telText=new JTextField("",10);
qqNumText=new JTextField("",10);
emailText=new JTextField("",10);
aiHaoText=new JTextField("",10);
OKBtn=new JButton("增加");
ClearBtn=new JButton("清空");
CancelBtn=new JButton("返回");
p11.add(nameLabel);
p11.add(nameText);
p12.add(sexLabel);
p12.add(sexText);
p13.add(ageLabel);
p13.add(ageText);
p14.add(jiGuanLabel);
p14.add(jiGuanText);
p15.add(telLabel);
p15.add(telText);
p16.add(qqNumLabel);
p16.add(qqNumText);
p17.add(emailLabel);
p17.add(emailText);
p18.add(aiHaoLabel);
p18.add(aiHaoText);
p1.setLayout(new GridLayout(8,1));
p1.add(p11);
p1.add(p12);
p1.add(p13);
p1.add(p14);
p1.add(p15);
p1.add(p16);
p1.add(p17);
p1.add(p18);
p2.add(OKBtn);
p2.add(ClearBtn);
p2.add(CancelBtn);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
OKBtn.addActionListener(this);
ClearBtn.addActionListener(this);
CancelBtn.addActionListener(this);
setSize(300,300);
setLocation(250,130);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==OKBtn)
{
totalRecNum=totalRecNum+1;
recNum=totalRecNum;
name=nameText.getText();
sex=sexText.getText();
age=ageText.getText();
jiGuan=jiGuanText.getText();
tel=telText.getText();
qqNum=qqNumText.getText();
email=emailText.getText();
aiHao=aiHaoText.getText();
FileOp.WriteRec();
JOptionPane.showMessageDialog(this,"记录增加成功!","成功",JOptionPane.WARNING_MESSAGE);
this.dispose();
}
if(e.getSource()==ClearBtn) //清除输入框
{
nameText.setText("");
sexText.setText("");
ageText.setText("");
jiGuanText.setText("");
telText.setText("");
qqNumText.setText("");
emailText.setText("");
aiHaoText.setText("");
}
if(e.getSource()==CancelBtn)
{
this.dispose();
}
}
}
class AppForm extends JFrame implements ActionListener
{
JPanel p1,p2,p11,p12,p13,p14,p15,p16,p17,p18;
JLabel nameLabel,sexLabel,ageLabel,jiGuanLabel;
JLabel telLabel,qqNumLabel,emailLabel,aiHaoLabel;
JTextField nameText,sexText,ageText,jiGuanText;
JTextField telText,qqNumText,emailText,aiHaoText;
JButton OKBtn,ClearBtn,CancelBtn;
AppForm(String s)
{
super(s);
p1=new JPanel();
p2=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p14=new JPanel();
p15=new JPanel();
p16=new JPanel();
p17=new JPanel();
p18=new JPanel();
nameLabel=new JLabel("姓 名:");
sexLabel=new JLabel("性 别:");
ageLabel=new JLabel("年 龄:");
jiGuanLabel=new JLabel("籍 贯:");
telLabel=new JLabel("电 话:");
qqNumLabel=new JLabel("qq 号:");
emailLabel=new JLabel("email:");
aiHaoLabel=new JLabel("爱 好:");
nameText=new JTextField("",10);
sexText=new JTextField("",10);
ageText=new JTextField("",10);
jiGuanText=new JTextField("",10);
telText=new JTextField("",10);
qqNumText=new JTextField("",10);
emailText=new JTextField("",10);
aiHaoText=new JTextField("",10);
OKBtn=new JButton("增加");
ClearBtn=new JButton("清空");
CancelBtn=new JButton("返回");
p11.add(nameLabel);
p11.add(nameText);
p12.add(sexLabel);
p12.add(sexText);
p13.add(ageLabel);
p13.add(ageText);
p14.add(jiGuanLabel);
p14.add(jiGuanText);
p15.add(telLabel);
p15.add(telText);
p16.add(qqNumLabel);
p16.add(qqNumText);
p17.add(emailLabel);
p17.add(emailText);
p18.add(aiHaoLabel);
p18.add(aiHaoText);
p1.setLayout(new GridLayout(8,1));
p1.add(p11);
p1.add(p12);
p1.add(p13);
p1.add(p14);
p1.add(p15);
p1.add(p16);
p1.add(p17);
p1.add(p18);
p2.add(OKBtn);
p2.add(ClearBtn);
p2.add(CancelBtn);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
OKBtn.addActionListener(this);
ClearBtn.addActionListener(this);
CancelBtn.addActionListener(this);
setSize(300,300);
setLocation(250,130);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==OKBtn)
{
totalRecNum=totalRecNum+1;
recNum=totalRecNum;
name=nameText.getText();
sex=sexText.getText();
age=ageText.getText();
jiGuan=jiGuanText.getText();
tel=telText.getText();
qqNum=qqNumText.getText();
email=emailText.getText();
aiHao=aiHaoText.getText();
FileOp.WriteRec();
JOptionPane.showMessageDialog(this,"记录增加成功!","成功",JOptionPane.WARNING_MESSAGE);
this.dispose();
}
if(e.getSource()==ClearBtn) //清除输入框
{
nameText.setText("");
sexText.setText("");
ageText.setText("");
jiGuanText.setText("");
telText.setText("");
qqNumText.setText("");
emailText.setText("");
aiHaoText.setText("");
}
if(e.getSource()==CancelBtn)
{
this.dispose();
}
}
}
#4
真长....
你想连接数据库(Sql Server7.0/2000)可以用下面代码连接,至于操作数据库,你可以参看JDK文档,Statement,PrepareStatement,....
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
你想连接数据库(Sql Server7.0/2000)可以用下面代码连接,至于操作数据库,你可以参看JDK文档,Statement,PrepareStatement,....
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
#5
连接语句该放在文件操作类的哪个地方哦?
#6
什么时候需要作数据库的操作什么时候连接就行了 数据操作完操作完使用conn.close()关闭连接
如果你要去连接sql server数据库 还得取下载安装驱动,先从odbc练起吧
连接数据库
import java.sql.*;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = java.sql.DriverManager.getConnection(jdbc:microsoft:数据源名,Login_Name,Login_PassWord);
Statement sta = conn.createStatement();
}catch(Exception e){
}
查询
String Query = "select * from xx .."sql查询语句
try {
pSta = conn.prepareStatement(Query);
pSta.executeUpdate();
} catch (SQLException ex) {
}
增加 修改 删除 可以用下面的方式, 举例是增加记录
String Query = "insert into 表名(字段1,字段2) values(?,?)";
try {
PreparedStatement pSta = conn.prepareStatement(Query);
pSta.setString(1, 字符串值1);
pSta.setString(2, 字符串值2);
pSta.executeUpdate();
} catch (SQLException ex) {
}
今时间不多了,写得可能乱点,凑合看吧,不懂得去查查api,
如果你要去连接sql server数据库 还得取下载安装驱动,先从odbc练起吧
连接数据库
import java.sql.*;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = java.sql.DriverManager.getConnection(jdbc:microsoft:数据源名,Login_Name,Login_PassWord);
Statement sta = conn.createStatement();
}catch(Exception e){
}
查询
String Query = "select * from xx .."sql查询语句
try {
pSta = conn.prepareStatement(Query);
pSta.executeUpdate();
} catch (SQLException ex) {
}
增加 修改 删除 可以用下面的方式, 举例是增加记录
String Query = "insert into 表名(字段1,字段2) values(?,?)";
try {
PreparedStatement pSta = conn.prepareStatement(Query);
pSta.setString(1, 字符串值1);
pSta.setString(2, 字符串值2);
pSta.executeUpdate();
} catch (SQLException ex) {
}
今时间不多了,写得可能乱点,凑合看吧,不懂得去查查api,
#7
Connection conn = java.sql.DriverManager.getConnection(jdbc:odbc:数据源名,Login_Name,Login_PassWord);
把上面这行改了
把上面这行改了
#8
JDBC操作任何数据都是一样的。只要把数据源驱动字符改一下就OK了
#9
To wind44(心如死灰)
连接语句该放在文件操作类的哪个地方哦?
--------
在操作数据库之前需要用这些语句连接
连接语句该放在文件操作类的哪个地方哦?
--------
在操作数据库之前需要用这些语句连接
#10
连接数据库的,单独写一下,然后其他的都只要调用就可以了。
例如:
<jsp:useBean id="p_showinfo" scope="page" class="firm.firm"/>
其中firm.firm就是连接数据库程序所放的地方
操作数据库
package firm;
import java.sql.*;
public class firm
{
String strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String strDBUrl="jdbc:odbc:firm";
private Connection conn=null;
private Statement stmt=null;
ResultSet rs=null;
//<!--注册数据库驱动程序-->
public firm()
{
try
{
Class.forName(strDBDriver);
}
//异常处理
catch(java.lang.ClassNotFoundException e)
{
System.err.println("firm():"+e.getMessage());
}
}
//<!--建立数据库连接及定义数据查询-->
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,"sa","");//创建数据库连接对象
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
//<!--定义数据操作-->
public void executeUpdate(String sql)
{
stmt=null;
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,"sa","");
stmt=conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
}
//<!--关闭数据库连接-->
public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void closeConn()
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
例如:
<jsp:useBean id="p_showinfo" scope="page" class="firm.firm"/>
其中firm.firm就是连接数据库程序所放的地方
操作数据库
package firm;
import java.sql.*;
public class firm
{
String strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String strDBUrl="jdbc:odbc:firm";
private Connection conn=null;
private Statement stmt=null;
ResultSet rs=null;
//<!--注册数据库驱动程序-->
public firm()
{
try
{
Class.forName(strDBDriver);
}
//异常处理
catch(java.lang.ClassNotFoundException e)
{
System.err.println("firm():"+e.getMessage());
}
}
//<!--建立数据库连接及定义数据查询-->
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,"sa","");//创建数据库连接对象
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
//<!--定义数据操作-->
public void executeUpdate(String sql)
{
stmt=null;
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,"sa","");
stmt=conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
}
//<!--关闭数据库连接-->
public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void closeConn()
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
#11
用hibernate吧,把操作数据库的事交给它,你只管调用就行了
#12
来晚了。
楼上正解
楼上正解
#13
mark
#1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.JOptionPane;
class main
{
public static void main(String args[])
{
new MainForm("同学录v1.0");
}
}
class MainForm extends JFrame implements ActionListener //主窗体
{
JPanel p1,p2,p11,p12,p13;
JLabel WelcomeLabel,DBLabel,DBCreatOKLabel;
String DBConString,DBCreatOKString; //数据库状态字符串
JButton AppBtn,DelBtn,EditBtn,CheckBtn,QuitBtn;
JMenuBar menubar;
JMenu menu;
JMenuItem item1,item2,item3,item4,item5;
int totalRecNum=0,recNum=0;//记录总数,当前记录号
// 字段 姓名 性别 年龄 籍贯 电话 QQ号 EMAIL 爱好
String name,sex,age,jiGuan,tel,qqNum,email,aiHao;
FileOp FileOp;//文件操作类声明
MainForm(String s)
{
super(s);
menubar=new JMenuBar(); // -----菜单设计
menu=new JMenu("操作");
item1=new JMenuItem("查看记录");
item2=new JMenuItem("增加记录");
item3=new JMenuItem("删除记录");
item4=new JMenuItem("修改记录");
item5=new JMenuItem("退出程序");
menu.add(item1);
menu.add(item2);
menu.add(item3);
menu.add(item4);
menu.add(item5);
menubar.add(menu);
setJMenuBar(menubar);
WelcomeLabel=new JLabel("欢迎使用同学录V1.0!");
DBLabel=new JLabel("");
DBCreatOKLabel=new JLabel("");
AppBtn=new JButton("增加记录");//按钮设计
DelBtn=new JButton("删除记录");
EditBtn=new JButton("修改记录");
CheckBtn=new JButton("查看记录");
QuitBtn=new JButton("退出程序");
p1=new JPanel();
p2=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p11.add(WelcomeLabel);
p12.add(DBLabel);
p13.add(DBCreatOKLabel);
p1.add(p11);
p1.add(p12);
p1.add(p13);
p2.add(CheckBtn);
p2.add(AppBtn);
p2.add(DelBtn);
p2.add(EditBtn);
p2.add(QuitBtn);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
p11.setBackground(Color.white);
p12.setBackground(Color.white);
p13.setBackground(Color.white);
p1.setLayout(new GridLayout(3,1));
CheckBtn.addActionListener(this);
AppBtn.addActionListener(this);
DelBtn.addActionListener(this);
EditBtn.addActionListener(this);
QuitBtn.addActionListener(this);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
addWindowListener(new winClose());
setSize(700,500);
setResizable(false);
setLocation(50,50);
setVisible(true);
FileOp=new FileOp();
recNum=1;
FileOp.ReadRec();
WelcomeLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,24));
DBLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,20));
DBCreatOKLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,20));
DBLabel.setText(DBConString);
DBCreatOKLabel.setText(DBCreatOKString);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==CheckBtn|e.getSource()==item1)
new CheckForm("查找记录");
if(e.getSource()==AppBtn|e.getSource()==item2)
new AppForm("增加记录");
if(e.getSource()==DelBtn|e.getSource()==item3)
new DelForm("删除记录");
if(e.getSource()==EditBtn|e.getSource()==item4)
new EditForm("修改记录");
if(e.getSource()==QuitBtn|e.getSource()==item5)
{
FileOp.CloseRecFile();
System.exit(0);
}
}
主框架。
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.JOptionPane;
class main
{
public static void main(String args[])
{
new MainForm("同学录v1.0");
}
}
class MainForm extends JFrame implements ActionListener //主窗体
{
JPanel p1,p2,p11,p12,p13;
JLabel WelcomeLabel,DBLabel,DBCreatOKLabel;
String DBConString,DBCreatOKString; //数据库状态字符串
JButton AppBtn,DelBtn,EditBtn,CheckBtn,QuitBtn;
JMenuBar menubar;
JMenu menu;
JMenuItem item1,item2,item3,item4,item5;
int totalRecNum=0,recNum=0;//记录总数,当前记录号
// 字段 姓名 性别 年龄 籍贯 电话 QQ号 EMAIL 爱好
String name,sex,age,jiGuan,tel,qqNum,email,aiHao;
FileOp FileOp;//文件操作类声明
MainForm(String s)
{
super(s);
menubar=new JMenuBar(); // -----菜单设计
menu=new JMenu("操作");
item1=new JMenuItem("查看记录");
item2=new JMenuItem("增加记录");
item3=new JMenuItem("删除记录");
item4=new JMenuItem("修改记录");
item5=new JMenuItem("退出程序");
menu.add(item1);
menu.add(item2);
menu.add(item3);
menu.add(item4);
menu.add(item5);
menubar.add(menu);
setJMenuBar(menubar);
WelcomeLabel=new JLabel("欢迎使用同学录V1.0!");
DBLabel=new JLabel("");
DBCreatOKLabel=new JLabel("");
AppBtn=new JButton("增加记录");//按钮设计
DelBtn=new JButton("删除记录");
EditBtn=new JButton("修改记录");
CheckBtn=new JButton("查看记录");
QuitBtn=new JButton("退出程序");
p1=new JPanel();
p2=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p11.add(WelcomeLabel);
p12.add(DBLabel);
p13.add(DBCreatOKLabel);
p1.add(p11);
p1.add(p12);
p1.add(p13);
p2.add(CheckBtn);
p2.add(AppBtn);
p2.add(DelBtn);
p2.add(EditBtn);
p2.add(QuitBtn);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
p11.setBackground(Color.white);
p12.setBackground(Color.white);
p13.setBackground(Color.white);
p1.setLayout(new GridLayout(3,1));
CheckBtn.addActionListener(this);
AppBtn.addActionListener(this);
DelBtn.addActionListener(this);
EditBtn.addActionListener(this);
QuitBtn.addActionListener(this);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
addWindowListener(new winClose());
setSize(700,500);
setResizable(false);
setLocation(50,50);
setVisible(true);
FileOp=new FileOp();
recNum=1;
FileOp.ReadRec();
WelcomeLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,24));
DBLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,20));
DBCreatOKLabel.setFont(new Font(this.getFont().getName(),Font.BOLD,20));
DBLabel.setText(DBConString);
DBCreatOKLabel.setText(DBCreatOKString);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==CheckBtn|e.getSource()==item1)
new CheckForm("查找记录");
if(e.getSource()==AppBtn|e.getSource()==item2)
new AppForm("增加记录");
if(e.getSource()==DelBtn|e.getSource()==item3)
new DelForm("删除记录");
if(e.getSource()==EditBtn|e.getSource()==item4)
new EditForm("修改记录");
if(e.getSource()==QuitBtn|e.getSource()==item5)
{
FileOp.CloseRecFile();
System.exit(0);
}
}
主框架。
#2
//---------------------------------------查看窗口类
class CheckForm extends JFrame implements ActionListener
{
JPanel p1,p2,p11,p12,p13,p14,p15,p16,p17,p18,p3;
JLabel nameLabel,sexLabel,ageLabel,jiGuanLabel;
JLabel telLabel,qqNumLabel,emailLabel,aiHaoLabel;
JLabel inputRecNumLabel;
JTextField nameText,sexText,ageText,jiGuanText;
JTextField telText,qqNumText,emailText,aiHaoText;
JTextField inputRecNumText;
JButton OKBtn,StartBtn,UpBtn,DownBtn,EndBtn,CancelBtn;
CheckForm (String s)
{
super(s);
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p14=new JPanel();
p15=new JPanel();
p16=new JPanel();
p17=new JPanel();
p18=new JPanel();
nameLabel=new JLabel("姓 名:");
sexLabel=new JLabel("性 别:");
ageLabel=new JLabel("年 龄:");
jiGuanLabel=new JLabel("籍 贯:");
telLabel=new JLabel("电 话:");
qqNumLabel=new JLabel("qq 号:");
emailLabel=new JLabel("email:");
aiHaoLabel=new JLabel("爱 好:");
inputRecNumLabel=new JLabel("输入要查看的记录号:");
nameText=new JTextField("",10);
sexText=new JTextField("",10);
ageText=new JTextField("",10);
jiGuanText=new JTextField("",10);
telText=new JTextField("",10);
qqNumText=new JTextField("",10);
emailText=new JTextField("",10);
aiHaoText=new JTextField("",10);
inputRecNumText=new JTextField("",5);
nameText.enable(false);
sexText.enable(false);
ageText.enable(false);
jiGuanText.enable(false);
telText.enable(false);
qqNumText.enable(false);
emailText.enable(false);
aiHaoText.enable(false);
OKBtn=new JButton("查找");
StartBtn=new JButton("首记录");
UpBtn=new JButton("上一条");
DownBtn=new JButton("下一条");
EndBtn=new JButton("尾记录");
CancelBtn=new JButton("返回");
p11.add(nameLabel);
p11.add(nameText);
p12.add(sexLabel);
p12.add(sexText);
p13.add(ageLabel);
p13.add(ageText);
p14.add(jiGuanLabel);
p14.add(jiGuanText);
p15.add(telLabel);
p15.add(telText);
p16.add(qqNumLabel);
p16.add(qqNumText);
p17.add(emailLabel);
p17.add(emailText);
p18.add(aiHaoLabel);
p18.add(aiHaoText);
p1.setLayout(new GridLayout(8,1));
p1.add(p11);
p1.add(p12);
p1.add(p13);
p1.add(p14);
p1.add(p15);
p1.add(p16);
p1.add(p17);
p1.add(p18);
p2.add(StartBtn);
p2.add(UpBtn);
p2.add(DownBtn);
p2.add(EndBtn);
p2.add(CancelBtn);
p3.add(inputRecNumLabel);
p3.add(inputRecNumText);
p3.add(OKBtn);
getContentPane().add(p3,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
OKBtn.addActionListener(this);
StartBtn.addActionListener(this);
UpBtn.addActionListener(this);
DownBtn.addActionListener(this);
EndBtn.addActionListener(this);
CancelBtn.addActionListener(this);
setSize(400,350);
setLocation(200,130);
setVisible(true);
recNum=1;
inputRecNumText.setText(String.valueOf(recNum));
ShowRecInfo();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==OKBtn)
if(Integer.parseInt(inputRecNumText.getText())>=1)
if(Integer.parseInt(inputRecNumText.getText())<=totalRecNum)
recNum=Integer.parseInt(inputRecNumText.getText());
if(e.getSource()==StartBtn)
recNum=1;
if(e.getSource()==UpBtn)
if(recNum>1)
recNum--;
if(e.getSource()==DownBtn)
if(recNum<totalRecNum)
recNum++;
if(e.getSource()==EndBtn)
recNum=totalRecNum;
if(e.getSource()==CancelBtn)
this.dispose();
ShowRecInfo();
}
void ShowRecInfo() //显示记录操作函数
{
FileOp.ReadRec();
inputRecNumText.setText(String.valueOf(recNum));
nameText.setText(name);
sexText.setText(sex);
ageText.setText(age);
jiGuanText.setText(jiGuan);
telText.setText(tel);
qqNumText.setText(qqNum);
emailText.setText(email);
aiHaoText.setText(aiHao);
}
}
查看窗口类
class CheckForm extends JFrame implements ActionListener
{
JPanel p1,p2,p11,p12,p13,p14,p15,p16,p17,p18,p3;
JLabel nameLabel,sexLabel,ageLabel,jiGuanLabel;
JLabel telLabel,qqNumLabel,emailLabel,aiHaoLabel;
JLabel inputRecNumLabel;
JTextField nameText,sexText,ageText,jiGuanText;
JTextField telText,qqNumText,emailText,aiHaoText;
JTextField inputRecNumText;
JButton OKBtn,StartBtn,UpBtn,DownBtn,EndBtn,CancelBtn;
CheckForm (String s)
{
super(s);
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p14=new JPanel();
p15=new JPanel();
p16=new JPanel();
p17=new JPanel();
p18=new JPanel();
nameLabel=new JLabel("姓 名:");
sexLabel=new JLabel("性 别:");
ageLabel=new JLabel("年 龄:");
jiGuanLabel=new JLabel("籍 贯:");
telLabel=new JLabel("电 话:");
qqNumLabel=new JLabel("qq 号:");
emailLabel=new JLabel("email:");
aiHaoLabel=new JLabel("爱 好:");
inputRecNumLabel=new JLabel("输入要查看的记录号:");
nameText=new JTextField("",10);
sexText=new JTextField("",10);
ageText=new JTextField("",10);
jiGuanText=new JTextField("",10);
telText=new JTextField("",10);
qqNumText=new JTextField("",10);
emailText=new JTextField("",10);
aiHaoText=new JTextField("",10);
inputRecNumText=new JTextField("",5);
nameText.enable(false);
sexText.enable(false);
ageText.enable(false);
jiGuanText.enable(false);
telText.enable(false);
qqNumText.enable(false);
emailText.enable(false);
aiHaoText.enable(false);
OKBtn=new JButton("查找");
StartBtn=new JButton("首记录");
UpBtn=new JButton("上一条");
DownBtn=new JButton("下一条");
EndBtn=new JButton("尾记录");
CancelBtn=new JButton("返回");
p11.add(nameLabel);
p11.add(nameText);
p12.add(sexLabel);
p12.add(sexText);
p13.add(ageLabel);
p13.add(ageText);
p14.add(jiGuanLabel);
p14.add(jiGuanText);
p15.add(telLabel);
p15.add(telText);
p16.add(qqNumLabel);
p16.add(qqNumText);
p17.add(emailLabel);
p17.add(emailText);
p18.add(aiHaoLabel);
p18.add(aiHaoText);
p1.setLayout(new GridLayout(8,1));
p1.add(p11);
p1.add(p12);
p1.add(p13);
p1.add(p14);
p1.add(p15);
p1.add(p16);
p1.add(p17);
p1.add(p18);
p2.add(StartBtn);
p2.add(UpBtn);
p2.add(DownBtn);
p2.add(EndBtn);
p2.add(CancelBtn);
p3.add(inputRecNumLabel);
p3.add(inputRecNumText);
p3.add(OKBtn);
getContentPane().add(p3,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
OKBtn.addActionListener(this);
StartBtn.addActionListener(this);
UpBtn.addActionListener(this);
DownBtn.addActionListener(this);
EndBtn.addActionListener(this);
CancelBtn.addActionListener(this);
setSize(400,350);
setLocation(200,130);
setVisible(true);
recNum=1;
inputRecNumText.setText(String.valueOf(recNum));
ShowRecInfo();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==OKBtn)
if(Integer.parseInt(inputRecNumText.getText())>=1)
if(Integer.parseInt(inputRecNumText.getText())<=totalRecNum)
recNum=Integer.parseInt(inputRecNumText.getText());
if(e.getSource()==StartBtn)
recNum=1;
if(e.getSource()==UpBtn)
if(recNum>1)
recNum--;
if(e.getSource()==DownBtn)
if(recNum<totalRecNum)
recNum++;
if(e.getSource()==EndBtn)
recNum=totalRecNum;
if(e.getSource()==CancelBtn)
this.dispose();
ShowRecInfo();
}
void ShowRecInfo() //显示记录操作函数
{
FileOp.ReadRec();
inputRecNumText.setText(String.valueOf(recNum));
nameText.setText(name);
sexText.setText(sex);
ageText.setText(age);
jiGuanText.setText(jiGuan);
telText.setText(tel);
qqNumText.setText(qqNum);
emailText.setText(email);
aiHaoText.setText(aiHao);
}
}
查看窗口类
#3
//---------------------------------------增加窗口类
class AppForm extends JFrame implements ActionListener
{
JPanel p1,p2,p11,p12,p13,p14,p15,p16,p17,p18;
JLabel nameLabel,sexLabel,ageLabel,jiGuanLabel;
JLabel telLabel,qqNumLabel,emailLabel,aiHaoLabel;
JTextField nameText,sexText,ageText,jiGuanText;
JTextField telText,qqNumText,emailText,aiHaoText;
JButton OKBtn,ClearBtn,CancelBtn;
AppForm(String s)
{
super(s);
p1=new JPanel();
p2=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p14=new JPanel();
p15=new JPanel();
p16=new JPanel();
p17=new JPanel();
p18=new JPanel();
nameLabel=new JLabel("姓 名:");
sexLabel=new JLabel("性 别:");
ageLabel=new JLabel("年 龄:");
jiGuanLabel=new JLabel("籍 贯:");
telLabel=new JLabel("电 话:");
qqNumLabel=new JLabel("qq 号:");
emailLabel=new JLabel("email:");
aiHaoLabel=new JLabel("爱 好:");
nameText=new JTextField("",10);
sexText=new JTextField("",10);
ageText=new JTextField("",10);
jiGuanText=new JTextField("",10);
telText=new JTextField("",10);
qqNumText=new JTextField("",10);
emailText=new JTextField("",10);
aiHaoText=new JTextField("",10);
OKBtn=new JButton("增加");
ClearBtn=new JButton("清空");
CancelBtn=new JButton("返回");
p11.add(nameLabel);
p11.add(nameText);
p12.add(sexLabel);
p12.add(sexText);
p13.add(ageLabel);
p13.add(ageText);
p14.add(jiGuanLabel);
p14.add(jiGuanText);
p15.add(telLabel);
p15.add(telText);
p16.add(qqNumLabel);
p16.add(qqNumText);
p17.add(emailLabel);
p17.add(emailText);
p18.add(aiHaoLabel);
p18.add(aiHaoText);
p1.setLayout(new GridLayout(8,1));
p1.add(p11);
p1.add(p12);
p1.add(p13);
p1.add(p14);
p1.add(p15);
p1.add(p16);
p1.add(p17);
p1.add(p18);
p2.add(OKBtn);
p2.add(ClearBtn);
p2.add(CancelBtn);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
OKBtn.addActionListener(this);
ClearBtn.addActionListener(this);
CancelBtn.addActionListener(this);
setSize(300,300);
setLocation(250,130);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==OKBtn)
{
totalRecNum=totalRecNum+1;
recNum=totalRecNum;
name=nameText.getText();
sex=sexText.getText();
age=ageText.getText();
jiGuan=jiGuanText.getText();
tel=telText.getText();
qqNum=qqNumText.getText();
email=emailText.getText();
aiHao=aiHaoText.getText();
FileOp.WriteRec();
JOptionPane.showMessageDialog(this,"记录增加成功!","成功",JOptionPane.WARNING_MESSAGE);
this.dispose();
}
if(e.getSource()==ClearBtn) //清除输入框
{
nameText.setText("");
sexText.setText("");
ageText.setText("");
jiGuanText.setText("");
telText.setText("");
qqNumText.setText("");
emailText.setText("");
aiHaoText.setText("");
}
if(e.getSource()==CancelBtn)
{
this.dispose();
}
}
}
class AppForm extends JFrame implements ActionListener
{
JPanel p1,p2,p11,p12,p13,p14,p15,p16,p17,p18;
JLabel nameLabel,sexLabel,ageLabel,jiGuanLabel;
JLabel telLabel,qqNumLabel,emailLabel,aiHaoLabel;
JTextField nameText,sexText,ageText,jiGuanText;
JTextField telText,qqNumText,emailText,aiHaoText;
JButton OKBtn,ClearBtn,CancelBtn;
AppForm(String s)
{
super(s);
p1=new JPanel();
p2=new JPanel();
p11=new JPanel();
p12=new JPanel();
p13=new JPanel();
p14=new JPanel();
p15=new JPanel();
p16=new JPanel();
p17=new JPanel();
p18=new JPanel();
nameLabel=new JLabel("姓 名:");
sexLabel=new JLabel("性 别:");
ageLabel=new JLabel("年 龄:");
jiGuanLabel=new JLabel("籍 贯:");
telLabel=new JLabel("电 话:");
qqNumLabel=new JLabel("qq 号:");
emailLabel=new JLabel("email:");
aiHaoLabel=new JLabel("爱 好:");
nameText=new JTextField("",10);
sexText=new JTextField("",10);
ageText=new JTextField("",10);
jiGuanText=new JTextField("",10);
telText=new JTextField("",10);
qqNumText=new JTextField("",10);
emailText=new JTextField("",10);
aiHaoText=new JTextField("",10);
OKBtn=new JButton("增加");
ClearBtn=new JButton("清空");
CancelBtn=new JButton("返回");
p11.add(nameLabel);
p11.add(nameText);
p12.add(sexLabel);
p12.add(sexText);
p13.add(ageLabel);
p13.add(ageText);
p14.add(jiGuanLabel);
p14.add(jiGuanText);
p15.add(telLabel);
p15.add(telText);
p16.add(qqNumLabel);
p16.add(qqNumText);
p17.add(emailLabel);
p17.add(emailText);
p18.add(aiHaoLabel);
p18.add(aiHaoText);
p1.setLayout(new GridLayout(8,1));
p1.add(p11);
p1.add(p12);
p1.add(p13);
p1.add(p14);
p1.add(p15);
p1.add(p16);
p1.add(p17);
p1.add(p18);
p2.add(OKBtn);
p2.add(ClearBtn);
p2.add(CancelBtn);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
OKBtn.addActionListener(this);
ClearBtn.addActionListener(this);
CancelBtn.addActionListener(this);
setSize(300,300);
setLocation(250,130);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==OKBtn)
{
totalRecNum=totalRecNum+1;
recNum=totalRecNum;
name=nameText.getText();
sex=sexText.getText();
age=ageText.getText();
jiGuan=jiGuanText.getText();
tel=telText.getText();
qqNum=qqNumText.getText();
email=emailText.getText();
aiHao=aiHaoText.getText();
FileOp.WriteRec();
JOptionPane.showMessageDialog(this,"记录增加成功!","成功",JOptionPane.WARNING_MESSAGE);
this.dispose();
}
if(e.getSource()==ClearBtn) //清除输入框
{
nameText.setText("");
sexText.setText("");
ageText.setText("");
jiGuanText.setText("");
telText.setText("");
qqNumText.setText("");
emailText.setText("");
aiHaoText.setText("");
}
if(e.getSource()==CancelBtn)
{
this.dispose();
}
}
}
#4
真长....
你想连接数据库(Sql Server7.0/2000)可以用下面代码连接,至于操作数据库,你可以参看JDK文档,Statement,PrepareStatement,....
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
你想连接数据库(Sql Server7.0/2000)可以用下面代码连接,至于操作数据库,你可以参看JDK文档,Statement,PrepareStatement,....
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
#5
连接语句该放在文件操作类的哪个地方哦?
#6
什么时候需要作数据库的操作什么时候连接就行了 数据操作完操作完使用conn.close()关闭连接
如果你要去连接sql server数据库 还得取下载安装驱动,先从odbc练起吧
连接数据库
import java.sql.*;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = java.sql.DriverManager.getConnection(jdbc:microsoft:数据源名,Login_Name,Login_PassWord);
Statement sta = conn.createStatement();
}catch(Exception e){
}
查询
String Query = "select * from xx .."sql查询语句
try {
pSta = conn.prepareStatement(Query);
pSta.executeUpdate();
} catch (SQLException ex) {
}
增加 修改 删除 可以用下面的方式, 举例是增加记录
String Query = "insert into 表名(字段1,字段2) values(?,?)";
try {
PreparedStatement pSta = conn.prepareStatement(Query);
pSta.setString(1, 字符串值1);
pSta.setString(2, 字符串值2);
pSta.executeUpdate();
} catch (SQLException ex) {
}
今时间不多了,写得可能乱点,凑合看吧,不懂得去查查api,
如果你要去连接sql server数据库 还得取下载安装驱动,先从odbc练起吧
连接数据库
import java.sql.*;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = java.sql.DriverManager.getConnection(jdbc:microsoft:数据源名,Login_Name,Login_PassWord);
Statement sta = conn.createStatement();
}catch(Exception e){
}
查询
String Query = "select * from xx .."sql查询语句
try {
pSta = conn.prepareStatement(Query);
pSta.executeUpdate();
} catch (SQLException ex) {
}
增加 修改 删除 可以用下面的方式, 举例是增加记录
String Query = "insert into 表名(字段1,字段2) values(?,?)";
try {
PreparedStatement pSta = conn.prepareStatement(Query);
pSta.setString(1, 字符串值1);
pSta.setString(2, 字符串值2);
pSta.executeUpdate();
} catch (SQLException ex) {
}
今时间不多了,写得可能乱点,凑合看吧,不懂得去查查api,
#7
Connection conn = java.sql.DriverManager.getConnection(jdbc:odbc:数据源名,Login_Name,Login_PassWord);
把上面这行改了
把上面这行改了
#8
JDBC操作任何数据都是一样的。只要把数据源驱动字符改一下就OK了
#9
To wind44(心如死灰)
连接语句该放在文件操作类的哪个地方哦?
--------
在操作数据库之前需要用这些语句连接
连接语句该放在文件操作类的哪个地方哦?
--------
在操作数据库之前需要用这些语句连接
#10
连接数据库的,单独写一下,然后其他的都只要调用就可以了。
例如:
<jsp:useBean id="p_showinfo" scope="page" class="firm.firm"/>
其中firm.firm就是连接数据库程序所放的地方
操作数据库
package firm;
import java.sql.*;
public class firm
{
String strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String strDBUrl="jdbc:odbc:firm";
private Connection conn=null;
private Statement stmt=null;
ResultSet rs=null;
//<!--注册数据库驱动程序-->
public firm()
{
try
{
Class.forName(strDBDriver);
}
//异常处理
catch(java.lang.ClassNotFoundException e)
{
System.err.println("firm():"+e.getMessage());
}
}
//<!--建立数据库连接及定义数据查询-->
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,"sa","");//创建数据库连接对象
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
//<!--定义数据操作-->
public void executeUpdate(String sql)
{
stmt=null;
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,"sa","");
stmt=conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
}
//<!--关闭数据库连接-->
public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void closeConn()
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
例如:
<jsp:useBean id="p_showinfo" scope="page" class="firm.firm"/>
其中firm.firm就是连接数据库程序所放的地方
操作数据库
package firm;
import java.sql.*;
public class firm
{
String strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String strDBUrl="jdbc:odbc:firm";
private Connection conn=null;
private Statement stmt=null;
ResultSet rs=null;
//<!--注册数据库驱动程序-->
public firm()
{
try
{
Class.forName(strDBDriver);
}
//异常处理
catch(java.lang.ClassNotFoundException e)
{
System.err.println("firm():"+e.getMessage());
}
}
//<!--建立数据库连接及定义数据查询-->
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,"sa","");//创建数据库连接对象
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
//<!--定义数据操作-->
public void executeUpdate(String sql)
{
stmt=null;
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,"sa","");
stmt=conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
}
//<!--关闭数据库连接-->
public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void closeConn()
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
#11
用hibernate吧,把操作数据库的事交给它,你只管调用就行了
#12
来晚了。
楼上正解
楼上正解
#13
mark