免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1077 | 回复: 0
打印 上一主题 下一主题

JAVAl连接MSsql [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-10 12:00 |只看该作者 |倒序浏览
package com.cheney.microsoft.sql;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class QueryFrame extends JFrame  implements ActionListener{
JTextField jf = new JTextField(20);
String[] value = {"姓名","部门","薪水"};
JComboBox box = new JComboBox(value);
JButton find = new JButton("查询");
JPanel panel_find = new JPanel();
JPanel panel_content = new JPanel();
Statement stmt = null;

public QueryFrame() {
  this.setTitle("查询系统");
  Container c = this.getContentPane();
  
  //完成了查找界面的设置
  jf.addActionListener(this);
  find.addActionListener(this);
  panel_find.add(jf);
  panel_find.add(box);
  panel_find.add(find);  
  
  c.add(panel_find,"North");
  c.add(panel_content,"Center");  
  
  this.setSize(400,400);
  this.setLocation(200,100);
  //连接SQL Server数据库
  try{
   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
   Connection conn =
    DriverManager.getConnection("jdbc:microsoft:sqlserver://10.10.20.43:1433;databasename=JAVATest","pap","321654");
   stmt = conn.createStatement();
  }catch(Exception e){
   e.printStackTrace();
  }
  this.setVisible(true);
}

public void actionPerformed(ActionEvent e){
  panel_content.removeAll();
  String item = (String)box.getSelectedItem();
  //确定用户所选择的查询依据
  if(item.equals("姓名")){
   item = "name";
  }
  if(item.equals("部门")){
   item = "department";
  }
  if(item.equals("薪水")){
   item = "salary";
  }
  //获取用户输入的查询关键字
  String content = jf.getText();
  int i = 0;
  try{
   String sql = "select * from employee";
   if(!content.equals("")){   
    sql = sql +" where "+item+" like '%"+content+"%'"; //组装查询语句
   }
   
   //执行查询语句,并显示查询结果
   ResultSet rs = stmt.executeQuery(sql);   
   
   while(rs.next()){
    int id = rs.getInt(1);
    String name = rs.getString(2);
    String department = rs.getString(3);
    int salary = rs.getInt(4);
   
    JLabel label = new JLabel(id + "  " + name + "  " + department + "  " + salary);      
   
    JPanel panel = new JPanel();
    panel.add(label);        
    panel_content.add(panel);   
   
    i++;
   }
  }catch(Exception e1){
   e1.printStackTrace();
  }
  panel_content.setLayout(new GridLayout(i,1));
  panel_content.validate();
   
}
public static void main(String[] args) {
  new QueryFrame();
}
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/21219/showart_439356.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP