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());
}
}
}
----------------解决方案--------------------------------------------------------
有异常抛出吗
有异常抛出的话,根据异常的信息就可以知道了
----------------解决方案--------------------------------------------------------
Note: readquestion.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
这是编译后显示的内容
----------------解决方案--------------------------------------------------------
这说明你的程序使用了过时的API
show();这就是你用的过时的API
建议使用setVisible(true)来代替
----------------解决方案--------------------------------------------------------
应该不是这个问题吧,不知道
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());
}
程序是否有问题呢?
----------------解决方案--------------------------------------------------------
应该不是这个问题吧,不知道大家明白我的意思没有?我是说这个程序已经可以运行,但是按了“读取题目”这个按钮后,文本区域里显示不了数据库表中的字段的内容。
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());
}
这段程序是否能否寻到数据库表中的内容呢?请各位高手给给意见!
[此贴子已经被作者于2006-6-9 21:28:28编辑过]
----------------解决方案--------------------------------------------------------
这是改过的程序:
import java.sql.*;
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);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="读取题目");
{
String str,s;
int x;
x=(int)Math.round(Math.random()*10);
str="select question from question1 where question_id='"+x+"'";
rs=mb.getResult(str);
StringBuffer buf=new StringBuffer();
try{
while(rs.next())
{
s=rs.getString(3);
//s=rs.getString(2);
buf.append(s);
test.setText(buf.toString());
mb.closeConnection();
this.dispose();
}
}catch(SQLException sql){
System.out.println(sql.toString());
}
}
}
public static void main(String args[])
{
new readquestion();
}
}
而出现的问题是:
“java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 标准表达式中数
据类型不匹配”。其中question_id字段的数据类型是数字,而question字段的数据类型是备注型。请高手帮忙找找哪个表达式出了问题了?谢谢
[此贴子已经被作者于2006-6-9 22:28:24编辑过]
----------------解决方案--------------------------------------------------------
希望各位高手能给出你宝贵的意见,谢谢!
----------------解决方案--------------------------------------------------------
你自己不都说了?
而出现的问题是:
“java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 标准表达式中数
据类型不匹配”。其中question_id字段的数据类型是数字,而question字段的数据类型是备注型。请高手帮忙找找哪个表达式出了问题了?
----------------解决方案--------------------------------------------------------
s=rs.getString(3);
改成s=rs.getString("字段名称");看看,还有没有这种问题
----------------解决方案--------------------------------------------------------