急需!!!在文本框输入内容然后点击查询按钮调出sql数据库数据并在GridView中显示所有数据、
点击查询在本届面出现 展开
首先你应该能获得这个号码的值对吧;然后你在写一个专门根据这个号码来现实所有信息的方法;
然后在页面通过"Eval("你的字段")"在GridView绑定一下,然后再在你的按钮的单击事件中调用这个你绑定的方法,就可以了;祝你好运!
代码写在按钮单击事件中
string upcmd = "select ......."
SqlConnection conn = new SqlConnection("连接字符串");
SqlCommand cmd = new SqlCommand(upcmd, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable("user");
dt.Load(reader);
dG.DataSource = dt;
reader.Close();
conn.Close();
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.JTableHeader;
public class ppp extends JFrame implements ActionListener{
private JScrollPane js;
private JTableHeader jth;
private JTable jtable;
private JButton jb,jb1;
private JTextField jt;
public ppp(){
this.js=new JScrollPane();
this.jt=new JTextField();
this.jb=new JButton("查询");
this.jt.setBounds(90, 10, 200, 30);
this.js.setBounds(10, 50, 300, 270);
this.jb.setBounds(10,10,70,30);
jb.addActionListener(this);
add(this.js);
add(this.jb);
add(this.jt);
this.setLayout(null);
this.setTitle("选课+抢课");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(100,50);
this.setSize(370,400);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ppp();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String search=this.jt.getText();
//String sql="select* from course where coursename='"+search+"'";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/school","root","root");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select* from course where coursename='"+search+"'");
int count=0;
while(rs.next()){
count++;
}
rs=stmt.executeQuery("select* from course where coursename='"+search+"'");
Object[][] info=new Object[count][5];
count=0;
while(rs.next()){
info[count][0] = rs.getString("coursename");
info[count][1] = rs.getString("teacher");
info[count][2]=Integer.valueOf( rs.getInt("credit"));
info[count][3]=Integer.valueOf( rs.getInt("max"));
info[count][4]=Integer.valueOf( rs.getInt("min"));
count++;
}
String[] Title={"课程名称","授课老师","学分","选课人数","已选人数"};
this.jtable = new JTable(info,Title);
this.jth = this.jtable.getTableHeader();
this.js.getViewport().add(jtable);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
2012-11-07
C# .NET代码都行 只要能实现查询 调用数据库数据 在GridView显示
我这里有一个用VB.NET写的,只是里面有一些自己写的方法什么的。