呵呵,你的Listxiaohua();函数里有一个while循环啊,如果要一个一个显示的话,怎么可以一直循环到没有next()呢?这样当然像你说的,点一下就到了最后了。
可以把while去掉,用if代替。当然这样的话,需要在没一次执行Listxiaohua()的时候保留rs,而不是没次都重新建立一个rs.可以把rs定义成static吧。
还有就是你没有一系列的关于数据库的close()啊,这样是很不好的习惯。要一条一条显示的话,就像上面我说的那样啊,不需要知道行数呢。
可以把while去掉,用if代替。当然这样的话,需要在没一次执行Listxiaohua()的时候保留rs,而不是没次都重新建立一个rs.可以把rs定义成static吧。
还有就是你没有一系列的关于数据库的close()啊,这样是很不好的习惯。要一条一条显示的话,就像上面我说的那样啊,不需要知道行数呢。
像 close() 这些书上都没什么详细说明的。 而且 我是自学。老师说的 VB+数据库。
然后我比较喜欢 JAVA 所以 就想用他来实现老师的那个 小程序了!!! 我现在照你说的试试看!! 再次感谢
用的是 ACCESS 数据库!
然后这样定义:
static ResultSet rs=sql.executeQuery("SELECT*FROM xh");
lianxi.java:55: illegal start of expression
static ResultSet rs=sql.executeQuery("SELECT*FROM xh");
^
lianxi.java:56: cannot resolve symbol
symbol : variable rs
location: class Mywindow
if(rs.next())
^
lianxi.java:58: cannot resolve symbol
symbol : variable rs
location: class Mywindow
biaoti=rs.getString(2);
^
lianxi.java:60: cannot resolve symbol
symbol : variable rs
location: class Mywindow
neirong=rs.getString(3);
^
4 errors输出完成 (耗时 2 秒) - 正常终止
你把连接得到rs的过程写到初始化里面去了么 不会还在Listxiaohua()里面吧
你可以在程序开始 就连接数据库 把数据读到一个vector里面 把vector定义成static 然后关闭数据库
每按一次按钮 就从vector里读一个数据出来
static Vector v = new Vector();
Mywindow()
{
……
……
initDataBase();
}
public initDataBase()
{
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }
catch(ClassNotFoundException e){}
Connection con=DriverManager.getConnection("jdbc:odbc:xh","luo","luo");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("SELECT*FROM xh");
while(rs.next())
{
//把数据读到Vector里面
}
}
……
……
public void Listxiaohua() throws SQLException
{ String biaoti=new String();
String neirong=new String();
if(//判断Vector读完没)
{ //给biaoti,neirong赋值
}
}
while(rs.next())
{ biaoti=rs.getString(2);
label_1.setText(biaoti);
neirong=rs.getString(3);
buffer.append(neirong + "\n");
}
if(buffer != null && buffer.length() != 0)//不知道是不是length(),或者是length
text.setText(buffer.toString());
{ String biaoti=new String();
String neirong=new String();
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }
catch(ClassNotFoundException e){}
Connection con=DriverManager.getConnection("jdbc:odbc:xh","luo","luo");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("SELECT*FROM xh");
if(rs.next())
{
biaoti=rs.getString(2);
label_1.setText(biaoti);
neirong=rs.getString(3);
text.setText(neirong);
}
con.close();
} ggimm(01234) 你说的那个Vector我还不会用!!
对了你的代码有问题这些:
Connection con=DriverManager.getConnection("jdbc:odbc:xh","luo","luo");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("SELECT*FROM xh");
while(rs.next())
{ biaoti=rs.getString(2);
label_1.setText(biaoti);
neirong=rs.getString(3);
text.setText(neirong);
}
也应该放到try、catch块中,异常捕捉SQLException。
如果想要写成static,我想要把con、sql、rs都提出来写成static才行吧。(个人看法,尽供参考)
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.net.*;class Mywindow extends JFrame implements ActionListener//建立自己的窗体
{
JLabel label_1,label_2;
JPanel JPanel_1;
JTextArea text;
JButton button;
JScrollPane scrollpane;
static ResultSet rs; // <---------
Mywindow()
{ //setResizable(false);
setSize(600,600);//窗体大小是100*100
setVisible(true);//窗体可见
Container contentpane=getContentPane();
contentpane.setLayout(new BorderLayout());//窗体的布局是 3行
label_1=new JLabel("笑话标题",JLabel.CENTER);
label_2=new JLabel("本程序由 JAVA 编写",JLabel.CENTER);
button=new JButton("下一个");//按纽 下一个
text=new JTextArea(10,40);//文本框
scrollpane=new JScrollPane(text);//滚动窗口,并把 文本框 添加 到 滚动窗口中
JPanel_1=new JPanel();
//JPanel_1.setLayout(new GridLayout(0,1));
JPanel_1.add(scrollpane,new GridLayout(0,1));
JPanel_1.add(button,new GridLayout(0,1));
button.addActionListener(this);
contentpane.add(label_1,BorderLayout.NORTH);
contentpane.add(JPanel_1,BorderLayout.CENTER);
contentpane.add(label_2,BorderLayout.SOUTH);
//pack();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button)
try{ Listxiaohua(); }
catch(SQLException ee){}
}
public void Listxiaohua() throws SQLException
{ String biaoti=new String();
String neirong=new String();
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }
catch(ClassNotFoundException e){}
Connection con=DriverManager.getConnection("jdbc:odbc:xh","luo","luo");
Statement sql=con.createStatement();
rs=sql.executeQuery("SELECT*FROM xh"); // <---------
if(rs.next())
{
biaoti=rs.getString(2);
label_1.setText(biaoti);
neirong=rs.getString(3);
text.setText(neirong);
}
con.close();
sql.close();
rs.close();
}
}
public class lianxi
{
public static void main(String args[])
{ Mywindow win=new Mywindow();
//win.setSize(100,100);
win.pack(); }
}; 改成这样后 数据就在 第一条。 按 下一个 这个按纽不能 走到 下一条啊!!按他没有反应!!
to: noratong(诺拉) 你说的一样是这个问题 得到的总是第一个啊
class Mywindow extends JFrame implements ActionListener//建立自己的窗体
{
JLabel label_1,label_2;
JPanel JPanel_1;
JTextArea text;
JButton button;
JScrollPane scrollpane;
ResultSet rs; // <---------
Mywindow()
{ //setResizable(false); OpenDataBase() //打开数据库 setSize(600,600);//窗体大小是100*100
setVisible(true);//窗体可见
Container contentpane=getContentPane();
contentpane.setLayout(new BorderLayout());//窗体的布局是 3行
label_1=new JLabel("笑话标题",JLabel.CENTER);
label_2=new JLabel("本程序由 JAVA 编写",JLabel.CENTER);
button=new JButton("下一个");//按纽 下一个
text=new JTextArea(10,40);//文本框
scrollpane=new JScrollPane(text);//滚动窗口,并把 文本框 添加 到 滚动窗口中
JPanel_1=new JPanel();
//JPanel_1.setLayout(new GridLayout(0,1));
JPanel_1.add(scrollpane,new GridLayout(0,1));
JPanel_1.add(button,new GridLayout(0,1));
button.addActionListener(this);
contentpane.add(label_1,BorderLayout.NORTH);
contentpane.add(JPanel_1,BorderLayout.CENTER);
contentpane.add(label_2,BorderLayout.SOUTH);
//pack();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
}
void OpenDataBase()
{
try()
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:xh","luo","luo");
Statement sql=con.createStatement();
rs=sql.executeQuery("SELECT*FROM xh");
}
catch(SQLException){}
catch(Exception){}
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button)
try{ Listxiaohua(); }
catch(SQLException ee){}
}
public void Listxiaohua() throws SQLException
{ String biaoti=new String();
String neirong=new String();
if(rs!=null)
{
if(rs.next())
{
biaoti=rs.getString(2);
label_1.setText(biaoti);
neirong=rs.getString(3);
text.setText(neirong);
}
else
{
rs.close();
rs = null;
con.close();
sql.close();
}
}
else
{
OpenDataBase();
}
}
}
public class lianxi
{
public static void main(String args[])
{ Mywindow win=new Mywindow();
//win.setSize(100,100);
win.pack(); }
};
在你的整个程序过程中数据库总是处于打开状态的 是否对系统资源的占用太大呢??其实可以写的更好的 哈哈:)