//---------------------------------------文件操作类
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做数据库,怎么改,如何连接数据库,如何写对表的操作。
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做数据库,怎么改,如何连接数据库,如何写对表的操作。
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 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();
}
}
}
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
如果你要去连接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,
把上面这行改了
在操作数据库之前需要用这些语句连接
例如:<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();
}
}
}