以下是我修改以后的程序,import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Search extends Frame{
Statement smt;
public Statement getStatement()
{
return smt;
} Button button1=new Button("搜索");
TextField textfield1=new TextField("",10);
Choice choice1=new Choice(); public static void main(String args[]){ Search f1=new Search();
f1.setTitle("查找记录");//窗口标题
f1.setSize(100,150);//窗口大小
f1.setLayout(new FlowLayout()); f1.setVisible(true);
}
public Search(){
add(textfield1); String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=java";
String user = "sa";
String password = "icncn1218"; try {
Class.forName(driver);
} catch(Exception ee) {
System.out.println("无法加载驱动程序:"+driver);
ee.printStackTrace();
} try {
Connection con = DriverManager.getConnection(url, user, password);
smt=con.createStatement(); HashMap map = new HashMap();
map.put("请选择类型", new Integer(1));
ResultSet Obj=smt.executeQuery("select id,newstype from cnnewsType");//从表里面读取下拉菜弹
if (Obj.next())
while(Obj.next())
{
map.put(Obj.getString(2),new Integer(Obj.getInt(1)));
Iterator itr = map.keySet().iterator();
while (itr.hasNext()) {
choice1.add(itr.next().toString());
} }
else
System.out.println("没有找到符合条件的记录");
add(choice1);
add(button1);
Obj.close();
button1.addActionListener(new ActionLis());
}
catch(SQLException SEe)
{
System.out.println("打开数据库连接失败");
SEe.printStackTrace();
}
}
class ActionLis implements ActionListener{
public void actionPerformed(ActionEvent e){ try{
Search instance = new Search(); ResultSet rs=instance.getStatement().executeQuery("select id,subject from cnnews where subject like '%"+textfield1.getText()+"%' order by id");//这儿的判断条件没有写好 System.out.println("准备显示记录......");
if (rs.next())
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
else
System.out.println("没有找到符合条件的记录"); }
catch(SQLException SE)
{
System.out.println("打开数据库连接失败");
SE.printStackTrace();
}}
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Search extends Frame{
Statement smt;
public Statement getStatement()
{
return smt;
} Button button1=new Button("搜索");
TextField textfield1=new TextField("",10);
Choice choice1=new Choice(); public static void main(String args[]){ Search f1=new Search();
f1.setTitle("查找记录");//窗口标题
f1.setSize(100,150);//窗口大小
f1.setLayout(new FlowLayout()); f1.setVisible(true);
}
public Search(){
add(textfield1); String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=java";
String user = "sa";
String password = "icncn1218"; try {
Class.forName(driver);
} catch(Exception ee) {
System.out.println("无法加载驱动程序:"+driver);
ee.printStackTrace();
} try {
Connection con = DriverManager.getConnection(url, user, password);
smt=con.createStatement(); HashMap map = new HashMap();
map.put("请选择类型", new Integer(1));
ResultSet Obj=smt.executeQuery("select id,newstype from cnnewsType");//从表里面读取下拉菜弹
if (Obj.next())
while(Obj.next())
{
map.put(Obj.getString(2),new Integer(Obj.getInt(1)));
Iterator itr = map.keySet().iterator();
while (itr.hasNext()) {
choice1.add(itr.next().toString());
} }
else
System.out.println("没有找到符合条件的记录");
add(choice1);
add(button1);
Obj.close();
button1.addActionListener(new ActionLis());
}
catch(SQLException SEe)
{
System.out.println("打开数据库连接失败");
SEe.printStackTrace();
}
}
class ActionLis implements ActionListener{
public void actionPerformed(ActionEvent e){ try{
Search instance = new Search(); ResultSet rs=instance.getStatement().executeQuery("select id,subject from cnnews where subject like '%"+textfield1.getText()+"%' order by id");//这儿的判断条件没有写好 System.out.println("准备显示记录......");
if (rs.next())
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
else
System.out.println("没有找到符合条件的记录"); }
catch(SQLException SE)
{
System.out.println("打开数据库连接失败");
SE.printStackTrace();
}}
}
}
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Search extends Frame{
java.sql.Statement smt; // pls. add this line.
Button button1=new Button("搜索");
TextField textfield1=new TextField("",10);
Choice choice1=new Choice();
...
...
public class Search extends Frame implements ActionListener{
... ... ... ...public void actionPerformed(ActionEvent e){
.. .. ..
}}