java初学者的困惑~~~~~~~~高手来~~~~ ( 积分: 100 )

  • 主题发起人 主题发起人 yunxi126
  • 开始时间 开始时间
Y

yunxi126

Unregistered / Unconfirmed
GUEST, unregistred user!
执行其它类后找到不本类的信息
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class wangfeiyue1 extends Frame implements ActionListener
{
static JFrame f1;
JPanel jp;
JButton jb,jb1,jb2,jb3;
TextField tx1,tx2;
TextArea ta1;
String str1,str2,str3;
JLabel l1,l2;
Choice j1;
//-----------------SQL-----

static void msg(String info)
{
JOptionPane msg = new JOptionPane();
msg.showMessageDialog(null, info);//捕获异常
}

wangfeiyue1()//构部部件
{
jp=new JPanel();
jp.setLayout(null);
f1.getContentPane().add(jp);
jb=new JButton("修改");
jb1 = new JButton("查询");
jb2 = new JButton("增加");
jb3 = new JButton("删除");
l1 = new JLabel("出生地:");
l2 = new JLabel("名称:");
j1 = new Choice();
tx1 = new TextField();
tx2 = new TextField();
ta1 = new TextArea();

j1.setBounds(400,60,100,20);
l1.setBounds(1,55,100,30);
l2.setBounds(200,55,100,30);
ta1.setBounds(1,100,550,250);
tx2.setBounds(250,60,100,20);
tx1.setBounds(50,60,100,20);
jb1.setBounds(200, 10, 70, 30);
jb.setBounds(100,10,70,30);
jb2.setBounds(1,10,70,30);
jb3.setBounds(300,10,70,30);
jb.addActionListener(this);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jp.add(j1);
jp.add(tx2);
jp.add(l2);
jp.add(l1);
jp.add(jb2);
jp.add(jb3);
jp.add(ta1);
jp.add(tx1);
jp.add(jb1);
jp.add(jb);
}
public void actionPerformed(ActionEvent e)//事件监听
{
Object ob = e.getSource();
ta1.setText("");
if (ob == jb)//修改
{
Adoconnection abcd = new Adoconnection();
abcd.AdoCommand("update base02 set xh = 8 where xh = 6");
}
if (ob == jb1) //查询
{
Adoconnection abcd1 =new Adoconnection();
abcd1.AdoQuery("select AreaName,Workname from base02");
}
if (ob == jb2) //新增
{
msg(this.tx1.getText()+this.tx2.getText());//这里执行adoconnection里的adoquery以后就弹不出正确的值了。

/* Adoconnection abcd = new Adoconnection();
abcd.AdoCommand( "insert into Base02(AreaName,Workname) values('"+tx1.getText()+"','"+tx2.getText()+"')");
msg( "insert into Base02(AreaName,Workname) values('"+tx1.getText()+"','"+tx2.getText()+"')");
tx1.setText("");
tx1.setText("");
*/
}
if (ob == jb3) //删除
{
Adoconnection abcd = new Adoconnection();
abcd.AdoCommand("delete base02 where Areaname='"+j1.getItem(2)+"'");
msg("删除成功");
}
}
void Choiseadd()//添加下拉框内容
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:master","sa","");
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery("select AreaName,Workname from base02");
j1.addItem("");
while(rs.next())
{
str1 = ta1.getText();
j1.addItem(rs.getString(1));
}
stm.close();
conn.close();
}
catch(Exception ex)
{
msg(ex.toString());
}

}
public static void main(String[] args)
{
f1=new JFrame("Hello");
wangfeiyue1 obj=new wangfeiyue1();
obj.Choiseadd();
f1.setBounds(200,200,560,380);
f1.setVisible(true);

}

}
class Adoconnection // 数据库操作类
{
String str1,str2,str3;
static Connection String;
static Connection conn1;
static Statement stm1;
static ResultSet rs1;
void AdoConnection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn1 = DriverManager.getConnection("jdbc:odbc:master","sa","");
stm1 = conn1.createStatement();

}
catch(Exception ex)
{
wangfeiyue1.msg(ex.toString());//异常
}
}
void AdoCommand(String SQL)
{
try
{
Adoconnection abc1 = new Adoconnection();
abc1.AdoConnection();
stm1.executeUpdate(SQL);
//wangfeiyue1.msg("AdoCommand方法执行了");
stm1.close();
conn1.close();
}
catch(Exception ex)
{
wangfeiyue1.msg(ex.toString());
}
}
void AdoQuery(String SQL)
{
try
{
wangfeiyue1 obj1 = new wangfeiyue1();
Adoconnection abc2 = new Adoconnection();
abc2.AdoConnection();
rs1 = stm1.executeQuery(SQL);
while(rs1.next())
{
str1 = obj1.ta1.getText();
str2 = str1+"出生地:"+rs1.getString(1)+"
姓名:"+rs1.getString(2)+"/n";
obj1.ta1.setText(str2);
}
stm1.close();
conn1.close();
}
catch(Exception ex)
{
wangfeiyue1.msg(ex.toString());
}
}

void Choiseadd()//添加下拉框内容
{
wangfeiyue1 w = new wangfeiyue1();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:master","sa","");
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery("select AreaName,Workname from base02");
w.j1.addItem("");
while(rs.next())
{
str1 = w.ta1.getText();
w.j1.addItem(rs.getString(1));
}
stm.close();
conn.close();
}
catch(Exception ex)
{
wangfeiyue1.msg(ex.toString());
}

}
public static void main(String[] args)
{
}
}
 
我也是初学者,但“执行其它类后找到不本类的信息”是什么意思?没看懂。。。
 
我已经搞定了。
我不明白是的为什么主函数中wangfeiyue1 obj=new wangfeiyue1();
定义一个新对象的时候会重新实例化构造函数。这样把所有的组件内容都清空了。
 
接受答案了.
 
后退
顶部