| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 574 人关注过本帖
标题:java访问sql数据库问题。。
只看楼主 加入收藏
lyb151
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2010-2-6
结帖率:80%
收藏
 问题点数:0 回复次数:0 
java访问sql数据库问题。。
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class DBApplication extends Frame implements ActionListener
{
       Connection con;
       Statement stmt;
       ResultSet rs;
       Button Query,Update,Delete,increase,show,exit;
       TextField T1,T2;
       Label L1,L2;
       String bookname,booknum;
       String str1,str2;
       Panel p1,p2;

      
    public DBApplication() {
        super("简单数据库应用程序");
        Query=new Button("查询");
        Update=new Button("更新");
        Delete=new Button("删除");
        increase=new Button("添加");
        show=new Button("显示");
        exit=new Button("退出");
        T1=new  TextField();      
        T2=new  TextField();
        T1.setBackground(Color.GREEN);
        T2.setBackground(Color.GREEN);
        L1=new Label("图书号");
        L2=new Label("书名");
        p1=new Panel();
        p2=new Panel();
        p1.setLayout(new GridLayout(2,2,40,10));
        p2.setLayout(new GridLayout(2,3,40,10));
        this.setLayout(new GridLayout(2,1));
        p1.add(L1);
        p1.add(T1);
        p1.add(L2);
        p1.add(T2);
        p2.add(show);
        p2.add(Query);
        p2.add(Update);
        p2.add(Delete);
        p2.add(increase);
        p2.add(exit);
        this.add(p1);
        this.add(p2);
        Query.addActionListener(this);
        Update.addActionListener(this);
        increase.addActionListener(this);
        Delete.addActionListener(this);
        show.addActionListener(this);
        exit.addActionListener(this);
        pack();
        this.setBackground(Color.BLUE);
        this.setVisible(true);
        try
        {
          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
          con=DriverManager.getConnection("jdbc:sqlserver://lyb-PC:1433;DatabaseName=LIB_M","","");
          stmt=con.createStatement();
        }
        catch(Exception e){}
    }
   
public void actionPerformed(ActionEvent e)
{
 if(e.getSource()==show)
 {
   try
   {
   rs=stmt.executeQuery("select 图书号,书名  from bookdata");
   while(rs.next())
   {
   bookname=rs.getString("书名");
   booknum =rs.getString("图书号");      
   T1.setText(booknum);
   T2.setText(bookname);
   }
     System.out.println("已访问完");
     rs.close();     
   }
   catch(Exception e1){}
 }
 if(e.getSource()==Query)
 {
 try{
    int flag;
    rs=stmt.executeQuery("select 图书号,书名 from bookdata");
    while(rs.next())
    {
        bookname=rs.getString("书名");
        booknum =rs.getString("图书号");      
        T1.setText(booknum);
        T2.setText(bookname);      
    }
 }
  catch(Exception e2){}
 }
 if(e.getSource()==Update)
 {
 try{
  str1=T1.getText().trim();
  str2=T2.getText().trim();
  String strupd="update bookdata set 图书号='"+str1+" 'where 书名='"+str2+"'";
  stmt.executeUpdate(strupd);        
 }
 catch(Exception e3){}
 }
 if(e.getSource()==increase)
 {
  try{
  str1=T1.getText();
  str2=T2.getText();
  String strinc="insert into bookdata(图书号,书名) values(str1,str2)";
  stmt.executeUpdate(strinc);
  }
  catch(Exception e4){}
 }
 if(e.getSource()==Delete)
 {
 try
 {
 str1=T1.getText();
 String strdel="delete * from bookdata where 图书号=str1";
 stmt.executeUpdate(strdel);
 }
 catch(Exception e5){}
 }
 if(e.getSource()==exit)
 {
     try
     {
     rs.close();
     stmt.close();
     con.close();
     }
     catch(Exception e6){}
     System.exit(0);
 }
}
public static void main(String[] args)
{
  new DBApplication();
}
}
搜索更多相关主题的帖子: java 数据库 访问 sql 
2010-05-17 14:06
快速回复:java访问sql数据库问题。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014775 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved