import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class readquestion extends JFrame implements ActionListener
{
databasemanager mb=new databasemanager();
JTextArea test=new JTextArea(100,100);
JButton read;
//JCheckBox box[];
Container c;
ResultSet rs;
public readquestion()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,500);
read=new JButton("读取题目");
//read.addActionListener(this);
c=getContentPane();
c.setLayout(new BorderLayout());
JPanel pane=new JPanel();
//pane.setLayout(new GridLayout());
pane.add(test);
JPanel pane2=new JPanel();
//pane.setLayout(new GridLayout());
pane2.add(read);
c.add(pane,BorderLayout.CENTER);
c.add(pane2,BorderLayout.SOUTH);
show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="读取题目");
{
String str,s;
int x=2;
//x=(int)Math.round(Math.random()*10/3);
str="select question from question1 where question='"+x+"'";
rs=mb.getResult(str);
try{
while(rs.next())
{
s=rs.getString(2);
//s=rs.getString(2);
test.append(s);
mb.closeConnection();
this.dispose();
}
}catch(SQLException sql){
System.out.println(sql.toString());
}
}
}
public static void main(String args[])
{
new readquestion();
}
}
这个程序的最终目的是将数据库表的一个字段的内容显示出来,但是现在却无法实现这个目的,希望各位高手能给指点指点,谢谢!下面这个程序是上面那个程序所用到的datamanager类:
import java.sql.*;
public class databasemanager
{
Connection con;
ResultSet rs;
Statement stmt;
public databasemanager()
{
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
con=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=data.mdb");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch ( ClassNotFoundException cnfex ) {
System.err.println("Failed to load JDBC/ODBC driver." );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
}
}
public ResultSet getResult(String strSQL)
{
try{
rs=stmt.executeQuery(strSQL);
return rs;
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
return null;
}
}
public boolean updateSql(String strSQL)
{
try{
stmt.executeUpdate(strSQL);
con.commit();
return true;
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
return false;
}
}
public void closeConnection()
{
try
{
con.close();
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
}
}
}