当前位置: 技术问答>java相关
请问高手们,数据库的基础问题,
来源: 互联网 发布时间:2015-05-23
本文导语: 源代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; /** * Title: newman的作品 * Description: neman的版权 * Copyright: Copyright (c) 2002 * Company: shu * @author nch@peoplemail.com.cn * @version 1.0 */...
源代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/**
*
*
*
*
* @author nch@peoplemail.com.cn
* @version 1.0
*/
public class JTabel_database_test {
public JTabel_database_test() {
}
public static void main(String[] args)
{ResultWin rw=new ResultWin();
rw.show() ;
}
}
class ResultWin extends JFrame implements ActionListener
{Object a[][];
Object columnName[]={"学号","姓名","出生日期","数学","物理","英语"};
JTable table;
JButton button;
Container container;
String name,xuehao;
Date date;
int math,physics,english;
Connection con;Statement sql;ResultSet rs;
JProgressBar p_bar;
ResultWin()
{super("数据查询");
a=new Object[30][6];
table=new JTable(a,columnName);
this.setSize(300,300) ;
this.setVisible(true) ;
button=new JButton("OK");
addWindowListener (new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit (0);}});
button.addActionListener(this) ;
p_bar=new JProgressBar(JProgressBar.VERTICAL ,0,50);
p_bar.setStringPainted(true) ;
container=this.getContentPane() ;
container.add(button,BorderLayout.SOUTH ) ;
container.add(new JScrollPane(table),BorderLayout.CENTER ) ;
container.add(p_bar,BorderLayout.WEST ) ;
}
public void actionPerformed(ActionEvent e)
//断点
{if(e.getSource ()==button)
{int i=0;
try
{Class.forName("sun.jdbc.odbc.jdbcOdbcDriver") ;}
catch(ClassNotFoundException e2){}
try
{con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk") ;
sql=con.createStatement() ;
rs=sql.executeQuery("SELECT*FROM chengjibiao") ;
while(rs.next() )
{xuehao=rs.getString(1) ;
name=rs.getString(2) ;
date=rs.getDate(3) ;
math=rs.getInt("数学") ;
physics=rs.getInt("物理") ;
english=rs.getInt("英语") ;
a[i][0]=xuehao;a[i][i]=name;a[i][2]=String.valueOf(math) ;
a[i][4]=String.valueOf(physics) ;a[i][5]=String.valueOf(english) ;
i++;
p_bar.setValue(i) ;
p_bar.setString("查询了"+i+"条记录") ;
}
pack();
con.close() ;
}
catch(SQLException e1){}
}
}
}
我的代码应该没有问题,但是我debug时,
从con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk") ;
一句就跳入异常了。
是不是连接出问题?
不知怎么解决?
数据库我一次也没有连接成功,
是不是你们在做之前安装了别的软件驱动?
在设置上我都做了,应该不会有问题(我是照着书做的)。
请高手们帮我提些看法,如何解决。
谢谢
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/**
*
Title: newman的作品
*
Description: neman的版权
*
Copyright: Copyright (c) 2002
*
Company: shu
* @author nch@peoplemail.com.cn
* @version 1.0
*/
public class JTabel_database_test {
public JTabel_database_test() {
}
public static void main(String[] args)
{ResultWin rw=new ResultWin();
rw.show() ;
}
}
class ResultWin extends JFrame implements ActionListener
{Object a[][];
Object columnName[]={"学号","姓名","出生日期","数学","物理","英语"};
JTable table;
JButton button;
Container container;
String name,xuehao;
Date date;
int math,physics,english;
Connection con;Statement sql;ResultSet rs;
JProgressBar p_bar;
ResultWin()
{super("数据查询");
a=new Object[30][6];
table=new JTable(a,columnName);
this.setSize(300,300) ;
this.setVisible(true) ;
button=new JButton("OK");
addWindowListener (new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit (0);}});
button.addActionListener(this) ;
p_bar=new JProgressBar(JProgressBar.VERTICAL ,0,50);
p_bar.setStringPainted(true) ;
container=this.getContentPane() ;
container.add(button,BorderLayout.SOUTH ) ;
container.add(new JScrollPane(table),BorderLayout.CENTER ) ;
container.add(p_bar,BorderLayout.WEST ) ;
}
public void actionPerformed(ActionEvent e)
//断点
{if(e.getSource ()==button)
{int i=0;
try
{Class.forName("sun.jdbc.odbc.jdbcOdbcDriver") ;}
catch(ClassNotFoundException e2){}
try
{con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk") ;
sql=con.createStatement() ;
rs=sql.executeQuery("SELECT*FROM chengjibiao") ;
while(rs.next() )
{xuehao=rs.getString(1) ;
name=rs.getString(2) ;
date=rs.getDate(3) ;
math=rs.getInt("数学") ;
physics=rs.getInt("物理") ;
english=rs.getInt("英语") ;
a[i][0]=xuehao;a[i][i]=name;a[i][2]=String.valueOf(math) ;
a[i][4]=String.valueOf(physics) ;a[i][5]=String.valueOf(english) ;
i++;
p_bar.setValue(i) ;
p_bar.setString("查询了"+i+"条记录") ;
}
pack();
con.close() ;
}
catch(SQLException e1){}
}
}
}
我的代码应该没有问题,但是我debug时,
从con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk") ;
一句就跳入异常了。
是不是连接出问题?
不知怎么解决?
数据库我一次也没有连接成功,
是不是你们在做之前安装了别的软件驱动?
在设置上我都做了,应该不会有问题(我是照着书做的)。
请高手们帮我提些看法,如何解决。
谢谢
|
你的Odbc数据源配好了没有??
|
ODBC 设置了吗
|
帖出报错信息!