import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class student extends Applet implements Runnable,ActionListener
{ String str_name;//student name
String str_pwd;//student password
String age;// student age
String id;
Box b1,b2,b3,b4;
Label l_id=new Label("id:");
Label l_name=new Label("name:");
Label l_pwd=new Label("password:");
Label l_age=new Label("age:");
TextField tf_id;
TextField tf_name;
TextField tf_pwd;
TextField tf_age;
Button btn_add=new Button("Add");
Button btn_find=new Button("Find");
TextArea ta_disp=new TextArea(10,10);
public Connection con=null;
public Statement stm=null;
public ResultSet rs=null;
public void init()
{
this.setBackground(Color.BLACK);
tf_id=new TextField(10);
tf_name=new TextField(10);
tf_pwd=new TextField(10);
tf_age=new TextField(10);
b1=Box.createVerticalBox();
b1.add(l_id);
b1.add(l_name);
b1.add(l_pwd);
b1.add(l_age);
b1.setBackground(Color.red);
add(b1);
b2=Box.createVerticalBox();
b2.add(tf_id);
b2.add(tf_name);
b2.add(tf_pwd);
b2.add(tf_age);
b2.setBackground(Color.CYAN);
add(b2);

b3=Box.createHorizontalBox();
btn_add.setFont(new Font(null,Font.ITALIC|Font.BOLD,20));
btn_find.setFont(new Font(null,Font.ITALIC|Font.BOLD,20));
btn_add.setBackground(Color.GRAY);
btn_find.setBackground(Color.darkGray);
b3.add(btn_add);
b3.add(btn_find);
add(b3);
this.setLayout(new FlowLayout(FlowLayout.RIGHT));
add(ta_disp);
btn_add.addActionListener(this);
btn_find.addActionListener(this);
try
{
System.out.println("connecting...");
Thread.sleep(400);
Class.forName("com.mysql.jdbc.Driver");
con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306" +
"/test?useUnicode=true&characterEncoding=GBK" ,"root",null);
stm=con.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_UPDATABLE);
System.out.println("connected");


}
catch(Exception e)
{
e.printStackTrace();
}
try
{
id=tf_id.getText().trim();
str_name=tf_name.getText().trim();
str_pwd=tf_pwd.getText().trim();
age=tf_age.getText().trim();

}
catch(Exception e)
{
e.printStackTrace();
}

}

public void actionPerformed(ActionEvent e)
{

if(e.getSource()==btn_add)
{
try
{
System.out.println("inserting...");
stm.executeUpdate("insert into student(id,name,password,age)" +
"values('"+id+"','"+str_name+"','"+str_pwd+"','"+age+"')");

System.out.println("inserted");
}
catch(Exception ae)
{
ae.toString();
}

}

if(e.getSource()==btn_find)
{
try
{
rs=stm.executeQuery("select * from student");
System.out.println("in");
while(rs.next())
{
System.out.println("id:"+rs.getString(1));
}


System.out.println("out");
}
catch(Exception ae)
{
ae.toString();
}
finally
{
try
{
con.close();
stm.close();
rs.close();
}
catch(SQLException se)
{
se.toString();
}

}
}
}
public void run()
{

}

}数据库mysql能连接上,插入增加能实现,但是为什么只能读第一行的数据id呢?

解决方案 »

  1.   


    while(rs.next()) 

    System.out.println("id:"+rs.getString(1)); 
    } 你这里只读了rs.getString(1).所以就只能出来ID.你完全可以多读几列啊while(rs.next()) 

    System.out.println("id:"+rs.getString(1)); 
    System.out.println("id:"+rs.getString(2)); 
    System.out.println("id:"+rs.getString(3)); 

      

  2.   

    唉.你的代码真让人不愿意看.
    你看看是不是你的Statement共用造成的.
    一般插入用一个Statement ,查询用一个Statement .别混着用.否则就会出现你这种诡异的现象