JDBC 如何取出数据库里的数据~菜鸟求救!!!!!
static Main m = new Main();
TextField tf = new TextField(10);
TextArea ta = new TextArea(10,15);
public static void main(String[] args) {
m.launchFrame();
}
public void launchFrame(){
private class ConfirmButton implements ActionListener{
private static final int INFORMATION_MESSAGE = 0;
public void actionPerformed(ActionEvent e) {
if(tf.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "请输入问题!","Warning",INFORMATION_MESSAGE);
return;
}
else
{
String str = connect.getDeptName();
ta.setText(str);
}
}
}
}
}
------------------------------------------------------------------------------
public class Connect{
Main m = new Main();
private String deptName;
public String getDeptName(){
String deptNo1 = m.tf.getText();
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
PreparedStatement ps = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
stmt = conn.createStatement();
ps = conn.prepareStatement("select DNAME from dept where deptno = ?");
ps.setString(1,deptNo1);
rs = ps.executeQuery();
while(rs.next()){
deptName = rs.getString(1);
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs = null;
}
if(ps!=null){
ps.close();
ps = null;
}
if(conn!=null){
conn.close();
conn = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
return deptName;//返回对应的DANME;
}
}
两个类的关键代码就是这些,为什么还是读不出数据来,我想实现的就是tf中输入10,然后把数据库里10对应的A输入到ta中........如何实现...
DEPTNO DNAME
10 A 展开
注意:你需要在项目中引入“ojdbc6.jar”才可以在java中访问Oracle。
修改后的Java程序:
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class Main extends JFrame implements ActionListener {
TextField tf;
TextArea ta;
JButton btn;
public Main(){
super();
tf = new TextField(10);
ta = new TextArea(10,15);
btn = new JButton("确定");
this.setLayout(new FlowLayout());
this.add(tf);
this.add(btn);
this.add(ta);
btn.addActionListener(this);
this.setSize(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Main();
}
@Override
public void actionPerformed(ActionEvent e) {
if(tf.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请输入问题!","Warning", 0);
return;
}
else {
String str = Connect.getDeptName(tf.getText());
ta.setText(str);
}
}
}
class Connect{
public static String getDeptName(String deptNo1){
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
PreparedStatement ps = null;
String deptName = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
stmt = conn.createStatement();
ps = conn.prepareStatement("select DNAME from dept where deptno = ?");
ps.setString(1,deptNo1);
rs = ps.executeQuery();
if(rs.next()){
deptName = rs.getString(1);
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
finally{
try{
if(rs!=null){
rs.close();
rs = null;
}
if(ps!=null){
ps.close();
ps = null;
}
if(conn!=null){
conn.close();
conn = null;
}
}
catch(SQLException e){
e.printStackTrace();
}
}
return deptName;//返回对应的DANME;
}
}
运行测试:
我另建了一个表mytbs 插入了一条数据
QUESTION ANSWER
天气 小雪
语句改为
ps = conn.prepareStatement("select ANSWER from mytbs where QUESTION = ?");
为什么就输出不了了,不解...
你插入数据后,要加
commit;
这样才能将当前会话中的更改影响到数据库。