新手问题...关于JB返回SQL数据的问题!
返回比tt= "ygp ";
返回的也是rs.getString(1)= "ygp ";
运行结果为:
ygp
ygp
java.sql.SQLException: No data found
为什么这个语句不能成立:if(rs.getString(1).equals(tt))
package mythsoftcorporation;
import java.awt.BorderLayout;
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.sql.*;
/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2007 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame2 extends JFrame {
JPanel contentPane;
Icon j2=new ImageIcon( "../图片/tp1.JPG ");
JLabel d1 = new JLabel(j2);
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JPasswordField t2 = new JPasswordField();
JComboBox t1 = new JComboBox();
String tt= " ";
String yy= " ";
public Frame2() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(330, 270));
setTitle( "资产用户登录 ");
d1.setBounds(new Rectangle(1, 2, 327, 62));
jPanel1.setBackground(new Color(209, 234, 255));
jPanel1.setBounds(new Rectangle( 0, 67, 330, 200));
jPanel1.setLayout(null);
jPanel2.setBackground(new Color(243, 255, 255));
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(5, 8, 313, 100));
jPanel2.setLayout(null);
jButton1.setBounds(new Rectangle(60, 120, 80, 30));
jButton1.setFont(new java.awt.Font( "宋体 ", Font.PLAIN, 15));
jButton1.setForeground(UIManager.getColor(
"InternalFrame.activeTitleGradient "));
jButton1.setText( "登 录 ");
jButton1.addMouseListener(new Frame2_jButton1_mouseAdapter(this));
jButton2.setBounds(new Rectangle(170, 120, 80, 30));
jButton2.setFont(new java.awt.Font( "宋体 ", Font.PLAIN, 15));
jButton2.setForeground(UIManager.getColor(
"InternalFrame.activeTitleGradient "));
jButton2.setText( "退 出 ");
jButton2.addMouseListener(new Frame2_jButton2_mouseAdapter(this));
jLabel1.setFont(new java.awt.Font( "宋体 ", Font.PLAIN, 13));
jLabel1.setText( "用户名 ");
jLabel1.setBounds(new Rectangle(40, 15, 70, 20));
jLabel2.setFont(new java.awt.Font( "宋体 ", Font.PLAIN, 13));
jLabel2.setText( "密 码 ");
jLabel2.setBounds(new Rectangle(40, 50, 70, 20));
t2.setDoubleBuffered(true);
t2.setEchoChar( '嘿 ');
t2.setBounds(new Rectangle(110, 50, 160, 20));
t1.setEditable(true);
t1.setBounds(new Rectangle(110, 15, 160, 20));
contentPane.add(d1);
contentPane.add(jPanel1);
jPanel1.add(jPanel2);
jPanel2.add(jLabel1);
jPanel2.add(t2);
jPanel2.add(jLabel2);
jPanel2.add(t1);
jPanel1.add(jButton1);
jPanel1.add(jButton2);
}
public void jButton2_mouseClicked(MouseEvent e) {
System.exit(0);
}
public void jButton1_mouseClicked(MouseEvent e) {
Object ty=t1.getSelectedItem();
tt=ty.toString();
char y[]=t2.getPassword();
for(int i=0;i <y.length;i++)
yy=yy+y[i];
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
}catch(ClassNotFoundException yu){
System.out.println(yu);
}
try{
String url= "jdbc:odbc:test ";
Connection con=DriverManager.getConnection(url);
Statement s=con.createStatement();
ResultSet rs=s.executeQuery( "select * from kw ");
while(rs.next())
{
System.out.println(rs.getString(1));
System.out.println(tt);
if(rs.getString(1).equals(tt)) //语句在这里
System.out.println( "123 ");
}
rs.close();
s.close();
con.close();
}
catch(SQLException yu){
System.out.println(yu);
}
}
}
class Frame2_jButton1_mouseAdapter extends MouseAdapter {
private Frame2 adaptee;
Frame2_jButton1_mouseAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jButton1_mouseClicked(e);
}
}
class Frame2_jButton2_mouseAdapter extends MouseAdapter {
private Frame2 adaptee;
Frame2_jButton2_mouseAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jButton2_mouseClicked(e);
}
}
[解决办法]
if(rs.getString(1).equals(tt))
你toString()一下再试,如下
if(rs.getString(1).toString().equals(tt))